-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 1.98 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const Department = require("./models/department");
const Problem = require("./models/problem");
require("dotenv").config({ path: __dirname + "/.env" });
const app = express();
app.use(bodyParser.json());
app.listen(9000, () => {
console.log("Server started on port 9000");
});
mongoose.connect(process.env.DATABASE_URL);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Connected to MongoDB database");
});
app.use(express.json());
app.delete("/api/department/delete/:id", (req, res) => {
const departmentId = req.params.id;
try {
Department.deleteOne({ _id: departmentId }).then((data) => {
console.log(data);
});
} catch (error) {
res.json(error);
}
});
app.post("/api/department/create", async (req, res) => {
try {
const { name } = req.body;
const department = new Department({
name,
});
department.save().then((data, err) => {
if (err) {
res.json(err);
} else {
res.json(data);
}
});
} catch (error) {
res.json(error);
}
});
app.get("/api/department", async (req, res) => {
Department.find().then((data, err) => {
res.json(data);
});
});
app.post("/api/problem/create", async (req, res) => {
try {
const { statement, deptId } = req.body;
const dept = await Department.findById(deptId);
if (!dept) {
return res.status(400).send("Invalid Department");
} else {
const problem = new Problem({
statement,
deptId,
});
problem.save().then((data, err) => {
if (err) {
res.json(err);
} else {
res.json(data);
}
});
}
} catch (error) {
res.status(404).json(error);
}
});
app.get("/api/problem", async (req, res) => {
Problem.find().then((data, err) => {
res.json(data);
});
});