-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentRoute.js
More file actions
89 lines (77 loc) · 2.44 KB
/
studentRoute.js
File metadata and controls
89 lines (77 loc) · 2.44 KB
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
import express from 'express';
import _ from 'lodash';
import student from './student.json';
import mongoose from 'mongoose';
const DB_USER = 'admin';
const DB_PASS = '123123';
const DB_URL = `mongodb://${DB_USER}:${DB_PASS}@ds133570.mlab.com:33570/mydatabase`;
let stuArray = student;
const router = express.Router();
mongoose.connect(DB_URL);
const db = mongoose.connection;
db.once('open', () => {
console.log('Database connected...');
});
const studentSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
course: String
});
const studentModel = mongoose.model('Student', studentSchema);
router.get('/', (req,res) => {
// res.json(stuArray);
studentModel.find((err,student) => {
if (err) res.status(500).send(err);
res.render('index', {
student: student
});
});
});
router.get('/:id', (req,res) => {
studentModel.findById(req.params.id, (err, student) => {
if (err) res.status(500).send(err);
if (student) {
res.json(student);
}else {
res.status(404).send(`User with Id ${req.params.id} not found!`);
}
});
});
router.post('/', (req,res) => {
// console.log('POST request...');
// console.log(req.body);
// stuArray.push(req.body);
// res.status(200).send('Success');
const id = new mongoose.Types.ObjectId();
const stuToPresist = Object.assign({
_id: id,
},req.body);
const student = new studentModel(stuToPresist);
student.save().then((err, student) => {
if(err) res.status(500).send(err);
res.json(student);
});
console.log(JSON.stringify(stuToPresist));
});
router.put('/:id', (req,res) => {
studentModel.findById(req.params.id, (err, student) => {
if (err) res.status(500).send(err);
if (student) {
student.name = req.body.name;
student.course = req.body.course;
student.save().then((err, student) => {
if (err) res.status(500).send(err);
res.json(student);
});
}else {
res.status(404).send(`User with Id ${req.params.id} not found!`);
}
})
});
router.delete('/:id', (req,res) => {
studentModel.findByIdAndRemove(req.params.id, (err, student) => {
if (err) res.status(500).send(err);
res.status(200).send(`Student with Id ${req.params.id}was successfully deleted !`);
});
});
module.exports = router;