users,js
· 1.5 KiB · Text
Raw
const express = require("express");
const Course = require("../mongoose/models/courses");
//setting up the student router
const usersRouter = new express.Router();
//write your code here
usersRouter.get(’/courses/get’, async (req, res) => {
const data = await Course.find()
res.status(200).json(data)
})
usersRouter•post('/courses/enroll/:id', async (req, res) => {
const id = req.params.id;
const getData = await Course.findByld(id)
if (!getData.isApplied) {
P° = {
isApplied: true
}
await Course.findByIdAndUpdate(id, po)
res.status(200).json({ message: "Sucessfully" })
} else {
res.status(403).json({ message: "not" })
}
})
usersRouter.patch('/courses/rating/:id', async (req, res) => {
const id = req.params.id;
const getData = await Course.findByld(id)
if (!getData.isRated && getData.isApplied) {
const body = req.body;
const noOfRatings = getData.noOfRatings + 1; const finalRate = (((getData.noOfRatings * getData.rating) + body.rating) / noOfRatings).toFixed(l)
pa = {
isRated: true,
rating: finalRate, noOfRatings: noOfRatings
}
await Course.findByIdAndUpdate(id, pa)
res.status(200).json({ message: "Sucessfully" }) } else {
res.status(403).json({ message: "Not" })
}
usersRouter.delete(1/courses/drop/:id1, async (req, res) => { const id = req.params.id;
const getData = await Course.findByld(id)
if (getData.isApplied) {
de = {
isApplied: false
}
await Course.findByIdAndUpdate(id, de)
res.status(200).json({ message: "Sucessfully" })
} else {
res.status(403).json({ message: "Not" })
}
})
module.exports = usersRouter
1 | const express = require("express"); |
2 | const Course = require("../mongoose/models/courses"); |
3 | //setting up the student router |
4 | const usersRouter = new express.Router(); |
5 | //write your code here |
6 | usersRouter.get(’/courses/get’, async (req, res) => { |
7 | const data = await Course.find() |
8 | res.status(200).json(data) |
9 | }) |
10 | usersRouter•post('/courses/enroll/:id', async (req, res) => { |
11 | const id = req.params.id; |
12 | const getData = await Course.findByld(id) |
13 | if (!getData.isApplied) { |
14 | P° = { |
15 | isApplied: true |
16 | } |
17 | await Course.findByIdAndUpdate(id, po) |
18 | res.status(200).json({ message: "Sucessfully" }) |
19 | } else { |
20 | res.status(403).json({ message: "not" }) |
21 | } |
22 | }) |
23 | usersRouter.patch('/courses/rating/:id', async (req, res) => { |
24 | const id = req.params.id; |
25 | const getData = await Course.findByld(id) |
26 | if (!getData.isRated && getData.isApplied) { |
27 | const body = req.body; |
28 | const noOfRatings = getData.noOfRatings + 1; const finalRate = (((getData.noOfRatings * getData.rating) + body.rating) / noOfRatings).toFixed(l) |
29 | pa = { |
30 | isRated: true, |
31 | rating: finalRate, noOfRatings: noOfRatings |
32 | } |
33 | await Course.findByIdAndUpdate(id, pa) |
34 | res.status(200).json({ message: "Sucessfully" }) } else { |
35 | res.status(403).json({ message: "Not" }) |
36 | } |
37 | usersRouter.delete(1/courses/drop/:id1, async (req, res) => { const id = req.params.id; |
38 | const getData = await Course.findByld(id) |
39 | if (getData.isApplied) { |
40 | de = { |
41 | isApplied: false |
42 | } |
43 | await Course.findByIdAndUpdate(id, de) |
44 | res.status(200).json({ message: "Sucessfully" }) |
45 | } else { |
46 | res.status(403).json({ message: "Not" }) |
47 | } |
48 | }) |
49 | module.exports = usersRouter |
50 |