-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
137 lines (120 loc) · 2.94 KB
/
server.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
129
130
131
132
133
134
135
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const app = express();
const Teacher = require("./mongo/Schema");
const PORT = process.env.PORT ||3001;
app.use(express.json());
app.use(cors());
const dbConnect = async () => {
try {
console.log("Connecting to DB...");
await mongoose.connect(
"Monguri",
{ useNewUrlParser: true, useUnifiedTopology: true }
);
console.log("DB connected");
} catch (err) {
console.log(err);
}
};
dbConnect();
// Get API
app.get("/api/getstaff", (req, res) => {
const getStaff = async () => {
try {
const teachers = await Teacher.find();
const result = await teachers;
return res.send(await result);
} catch (err) {
console.log(err);
}
};
getStaff();
});
//POST API
app.post("/api/newstaff", (req, res) => {
const newteacher = new Teacher({
name: req.body.name,
cntrct: req.body.cntrct,
school: req.body.school,
compApps: [],
type: req.body.type,
manager: req.body.manager,
band: req.body.band
});
newteacher.save();
res.send("created");
});
// UPDATE HISTORY
app.put("/api/history/:id/:type/:date", (req, res) => {
var app = {
date: req.params.date, type: req.params.type
}
const upHistory = async () => {
console.log(app)
try {
await Teacher.findByIdAndUpdate(
{ _id: req.params.id },
{
$push : {
"compApps": {
date: app.date, type: app.type, status: "Completed"
}
}
},
{ useFindAndModify: false }
);
return await res.send("succesfully added to array")
} catch (err) {
console.log(err.message);
}
};
upHistory();
});
//EDIT API
app.put("/api/edit/:id/:name/:date/:school/:manager/:type/:band", (req, res) => {
console.log(req.params.name);
const edit = async () => {
try {
await Teacher.findByIdAndUpdate(
{ _id: req.params.id },
{
name: req.params.name,
cntrct: req.params.date,
school: req.params.school,
manager: req.params.manager,
type: req.params.type,
band: req.params.band
},
{ useFindAndModify: false }
);
return await res.send("updated");
} catch (err) {
console.log(err);
res.send(err.message);
}
};
edit();
});
//Delete API
app.delete("/api/delete/:id", (req, res) => {
const delTeacher = async () => {
try {
await Teacher.findOneAndDelete({ _id: req.params.id });
return res.send("Teacher deleted");
} catch (err) {
console.log(err.message);
}
};
delTeacher();
});
if(process.env.NODE_ENV === 'production'){
app.use(express.static('appraisalapp/build'));
app.get('*', (req,res)=>{
res.sendFile(path.resolve(__dirname, 'appraisalapp', 'build', 'index.html'))
})
}
app.listen(PORT, (req, res) => {
console.log("listning on port" + PORT);
});