-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (111 loc) · 3.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import cors from "cors";
import AssignedAssest from "./Models/AssignedAssits.js";
import Location from "./Models/Location.js";
import path from "path";
import { fileURLToPath } from "url";
dotenv.config();
const app = express();
const port = 3000;
app.use(express.json());
app.use(cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.join(__dirname, "dist")));
app.get("/", function (req, res) {
console.log(path.join(__dirname, "dist", "index.html"));
res.sendFile(path.join(__dirname, "dist", "index.html"), (err) => {
if (err) console.log("Error line No. 56", err);
});
});
app.get("/assets/findall", async (req, res) => {
try {
console.log("Hi");
const assignedAssests = await AssignedAssest.find();
console.log(assignedAssests);
res.status(200).json(assignedAssests);
} catch (err) {
console.error("Error retrieving records:", err);
res.status(400).json("Server Error");
}
});
app.get("/assets/find-by-category/:category/:value", async (req, res) => {
const category = req.params.category;
const value = req.params.value.toUpperCase();
console.log(category, value, req.params);
let assignedAssests;
try {
if (category === "LOCATION") {
console.log("1");
assignedAssests = await AssignedAssest.find({ location: value });
} else if (category === "ASSET_CATEGORY") {
console.log("2");
assignedAssests = await AssignedAssest.find({ asset_category: value });
} else if (category === "PROJECT") {
console.log("Hi");
assignedAssests = await AssignedAssest.find({ project: value });
} else if (category === "NAME") {
console.log("4");
assignedAssests = await AssignedAssest.find({ name: value });
}
console.log(assignedAssests);
res.status(200).json(assignedAssests);
} catch (err) {
res.json("Server Error");
console.log(err);
}
});
app.post("/assets/add", async (req, res) => {
console.log(req.body.asset);
let { name, cdsid, location, assetType ,assetCategory, assetId, project } =
req.body.asset;
name = name.toUpperCase();
location = location.toUpperCase();
assetCategory = assetCategory.toUpperCase();
project = project.toUpperCase();
assetType.toUpperCase();
try {
const assignedAssest = new AssignedAssest({
name,
cdsid,
location,
asset_type : assetType,
asset_category: assetCategory,
asset_id: assetId,
project,
});
await assignedAssest.save();
res.status(201).json("Post Created");
} catch (err) {
console.error("Error creating a record:", err);
res.status(400).json("Server Error");
}
});
app.get("/location", async (req, res) => {
try {
const data = await Location.find();
console.log(data);
res.status(200).json(data);
} catch (err) {
console.log(err);
res.status(400).json("Server Error");
}
});
// app.get("/asset_category" ,async (req,res) =>{
// })
// app.get("/projects" , async (req,res)=>{
// })
mongoose
.connect(
"mongodb+srv://kush:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
)
.then(() => {
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
})
.catch((err) => {
console.log("DB Error", err);
});