-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (87 loc) · 2.96 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
"use strict";
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
// Connect to MongoDB...
let mongoDB = process.env.MONGODB_URL; // || dev_db_url;
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on("error", () => {
console.log("> MongoDB Connection Error...");
});
db.once("open", () => {
console.log("> Successfully Connected the database");
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
const Student = require("./api/students/model/Student");
/** *********************************************************** */
app.get("/", (request, response) => {
response.status(200).json({ message: "You successfully connected." });
});
/** *********************************************************** */
app.get("/api", (request, response) => {
response.status(200).json({ message: "Welcome to API Home." });
});
/** *********************************************************** */
/** GET ALL STUDENTS INFO */
app.use("/api/students", require("./api/students/routes/get_students"));
/** *********************************************************** */
/** POST SINGLE STUDENT INFO */
app.post("/api/student", (req, res) => {
const student = new Student(req.body);
student.save((err, student) => {
if (err) {
res.status(400).json(err);
}
res.status(200).json(student);
});
});
/** *********************************************************** */
// app.use('/api/students', require('./api/students/routes/get_student'));
/** *********************************************************** */
// /** GET SINGLE STUDENT INFO */
app.get("/api/student/:id", (req, res) => {
const _id = req.params.id;
Student.findOne({ _id }, (err, student) => {
if (err) {
res.status(400).json(err);
}
if (!student) {
res.status(404).json({ message: "No student found" });
}
res.status(200).json(student);
});
});
/** *********************************************************** */
/** DELETE SINGLE STUDENT */
app.delete("/api/student/:id", (req, res) => {
const _id = req.params.id;
Student.findOneAndRemove({ _id }, (err, student) => {
if (err) {
res.status(400).json(err);
}
if (!student) {
res.status(404).json({ message: "Student record not found" });
}
res.status(200).json({ message: `Student ${student.id} has been deleted` });
});
});
/** *********************************************************** */
/** EDIT A STUDENT */
app.put("/api/student/:id", (req, res) => {
const _id = req.params.id;
Student.findOneAndUpdate({ _id }, req.body, { new: true }, (err, student) => {
if (err) {
res.status(400).json(err);
}
res.status(200).json(student);
});
});
/** *********************************************************** */
module.exports = app;