Last active 1749834487

lolpee69 revised this gist 1749834487. Go to revision

1 file changed, 84 insertions, 46 deletions

users,js

@@ -1,49 +1,87 @@
1 + const mongoose = require("mongoose");
1 2 const express = require("express");
2 3 const Course = require("../mongoose/models/courses");
3 - //setting up the student router
4 +
4 5 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
6 +
7 + // Get all courses
8 + usersRouter.get("/courses/get", async (req, res) => {
9 + try {
10 + const courses = await Course.find({});
11 + res.status(200).send(courses);
12 + } catch (error) {
13 + res.status(400).send({ error: "Failed to fetch courses" });
14 + }
15 + });
16 +
17 + // Enroll in a course
18 + usersRouter.post("/courses/enroll/:id", async (req, res) => {
19 + try {
20 + const course = await Course.findById(req.params.id);
21 + if (!course) {
22 + return res.status(404).send({ error: "Course not found" });
23 + }
24 + if (course.isApplied) {
25 + return res.status(403).send({ error: "Already enrolled in this course" });
26 + }
27 + course.isApplied = true;
28 + await course.save();
29 + res.status(200).send({ message: "Enrolled successfully" });
30 + } catch (error) {
31 + res.status(400).send({ error: "Failed to enroll in the course" });
32 + }
33 + });
34 +
35 + // Drop a course
36 + usersRouter.delete("/courses/drop/:id", async (req, res) => {
37 + try {
38 + const course = await Course.findById(req.params.id);
39 + if (!course) {
40 + return res.status(404).send({ error: "Course not found" });
41 + }
42 + if (!course.isApplied) {
43 + return res.status(403).send({ error: "Cannot drop an unenrolled course" });
44 + }
45 + course.isApplied = false;
46 + await course.save();
47 + res.status(200).send({ message: "Dropped the course successfully" });
48 + } catch (error) {
49 + res.status(400).send({ error: "Failed to drop the course" });
50 + }
51 + });
52 +
53 + // Rate a course
54 + usersRouter.patch("/courses/rating/:id", async (req, res) => {
55 + try {
56 + const { rating } = req.body;
57 + if (!rating || rating < 1 || rating > 5) {
58 + return res.status(400).send({ error: "Invalid rating" });
59 + }
60 +
61 + const course = await Course.findById(req.params.id);
62 + if (!course) {
63 + return res.status(404).send({ error: "Course not found" });
64 + }
65 + if (!course.isApplied) {
66 + return res.status(403).send({ error: "Cannot rate an unenrolled course" });
67 + }
68 +
69 + if (course.isRated) {
70 + return res.status(403).send({ error: "Course has already been rated" });
71 + }
72 +
73 + // Update course rating
74 + const totalRating = course.rating * course.noOfRatings;
75 + const newTotalRating = totalRating + rating;
76 + course.noOfRatings += 1;
77 + course.rating = (newTotalRating / course.noOfRatings).toFixed(1);
78 + course.isRated = true;
79 + await course.save();
80 +
81 + res.status(200).send({ message: "Rating added successfully" });
82 + } catch (error) {
83 + res.status(400).send({ error: "Failed to rate the course" });
84 + }
85 + });
86 +
87 + module.exports = usersRouter;

lolpee69 revised this gist 1749831777. Go to revision

1 file changed, 49 insertions

users,js(file created)

@@ -0,0 +1,49 @@
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
Newer Older