Last active 1749834487

Revision 5e1604728ffe56d7944773964296e688baad05c9

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