Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed some deprecate warning issues and updated the PUT-Route #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions models/db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const mongoose = require('mongoose');
const mongoose = require("mongoose");
// Allow Promises
mongoose.Promise = global.Promise;
// Connection
mongoose.connect('mongodb://localhost:27017/db_test', { useNewUrlParser: true });
mongoose.connect("mongodb://localhost:27017/db_test", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
useCreateIndex: true,
});
// Validation
mongoose.connection
.once('open', () => console.log('Connected to the database!'))
.on('error', err => console.log('Error with the database!', err));
.once("open", () => console.log("Connected to the database!"))
.on("error", (err) => console.log("Error with the database!", err));
58 changes: 37 additions & 21 deletions routes/students.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,85 @@
const express = require('express');
const express = require("express");
const router = express.Router();

// Student model
const Students = require('../models/students');
const Students = require("../models/students");

// @route GET /api/students/
// @desc Get all students
// @access Public
router.get('/', async (req, res) => {
router.get("/", async (req, res) => {
try {
const students = await Students.find({});
res.send({ students })
} catch(err) {
res.send({ students });
} catch (err) {
res.status(400).send({ error: err });
}
});

// @route GET /api/students/:id
// @desc Get a specific student
// @access Public
router.get('/:id', async (req, res) => {
router.get("/:id", async (req, res) => {
try {
const student = await Students.findById(req.params.id);
res.send({ student });
} catch (err) {
res.status(404).send({ message: 'Student not found!' });
res.status(404).send({ message: "Student not found!" });
}
});

// @route POST /api/students/
// @desc Create a student
// @access Public
router.post('/', async (req, res) => {
router.post("/", async (req, res) => {
try {
const newStudent = await Students.create({ name: req.body.name, email: req.body.email, enrollnumber: req.body.enrollnumber });
res.send({ newStudent });
} catch(err) {
const newStudent = await Students.create({
name: req.body.name,
email: req.body.email,
enrollnumber: req.body.enrollnumber,
});
res.send({ newStudent });
} catch (err) {
res.status(400).send({ error: err });
}

});

// @route PUT /api/students/:id
// @desc Update a student
// @access Public
router.put('/:id', async (req, res) => {
router.put("/:id", async (req, res) => {
const updates = Object.keys(req.body);
const allowedUpdates = ["name", "email", "enrollnumber"];
const isValid = updates.every((val) => {
return allowedUpdates.includes(val);
});
if (!isValid) {
return res.status(400).send({
error:
"Invalid Operation You can Only Update [ name,email,enrollnumber ]",
});
}
try {
const updatedStudent = await Students.findByIdAndUpdate(req.params.id, req.body);
res.send({ message: 'The student was updated' });
} catch(err) {
const updatedStudent = await Students.findByIdAndUpdate(
req.params.id,
req.body
);
res.send({ message: "The student was updated" });
} catch (err) {
res.status(400).send({ error: err });
}
});

// @route DELETE /api/students/:id
// @desc Delete a student
// @access Public
router.delete('/:id', async (req, res) => {
router.delete("/:id", async (req, res) => {
try {
const removeStudent = await Students.findByIdAndRemove(req.params.id);
res.send({ message: 'The student was removed' });
} catch(err) {
res.send({ message: "The student was removed" });
} catch (err) {
res.status(400).send({ error: err });
}
});


module.exports = router;
module.exports = router;