From 1bef444b2c127baea50216fcc73797928c5a5536 Mon Sep 17 00:00:00 2001 From: Arun-cn Date: Tue, 31 Oct 2023 16:46:09 +0000 Subject: [PATCH 1/2] fix uuid and uuidv4 import --- backend/controllers/user.js | 2 +- backend/models/course.model.js | 5 ++--- package-lock.json | 6 ++++++ 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 package-lock.json diff --git a/backend/controllers/user.js b/backend/controllers/user.js index d5ad686..1b9a990 100644 --- a/backend/controllers/user.js +++ b/backend/controllers/user.js @@ -1,7 +1,7 @@ require("dotenv").config(); const User = require('../models/user.model'); -const bcrypt = require('bcryptjs'); +const bcrypt = require('bcrypt'); const jwt = require("jsonwebtoken"); const JWT_SECRET = process.env.JWT_SECRET; diff --git a/backend/models/course.model.js b/backend/models/course.model.js index f9433a8..cc2ccc8 100644 --- a/backend/models/course.model.js +++ b/backend/models/course.model.js @@ -1,12 +1,11 @@ const mongoose = require('mongoose'); -const uuidv4 = require('uuidv4'); - +const { v4: uuidv4 } = require('uuid'); const courseSchema = new mongoose.Schema( { course_id: { type: String, required: true, - default: uuidv4.uuid().split("-").join("") + default: uuidv4().split("-").join("") }, title: { type: String, diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..c5b1c50 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "EduHub", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} From 75560fd552688c8db98088feef20cdc5aac5938c Mon Sep 17 00:00:00 2001 From: Arun-cn Date: Tue, 31 Oct 2023 18:12:37 +0000 Subject: [PATCH 2/2] add user_assessment --- backend/controllers/userAssessment.js | 81 +++++++++++++++++++++++++ backend/models/userAssessment.model.js | 28 +++++++++ backend/routes/userAssessment.routes.js | 14 +++++ backend/server.js | 2 +- 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 backend/controllers/userAssessment.js create mode 100644 backend/models/userAssessment.model.js create mode 100644 backend/routes/userAssessment.routes.js diff --git a/backend/controllers/userAssessment.js b/backend/controllers/userAssessment.js new file mode 100644 index 0000000..ce9a232 --- /dev/null +++ b/backend/controllers/userAssessment.js @@ -0,0 +1,81 @@ +const UserAssessment = require('../models/UserAssessment.model'); + +const createUserAssessment = async (req, res) => { + const { user_id, assessment_id, score } = req.body; + + if (!user_id || !assessment_id || !score) { + return res.status(400).json({ error: "Missing required fields" }); + } + + try { + const newUserAssessment = new UserAssessment({ + user_id, + assessment_id, + score + }); + + const savedUserAssessment = await newUserAssessment.save(); + res.status(201).json(savedUserAssessment); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + +const getAllUserAssessments = async (req, res) => { + try { + const allUserAssessments = await UserAssessment.find(); + res.status(200).json(allUserAssessments); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + +const getUserAssessmentById = async (req, res) => { + const { user_assessment_id } = req.params; + + try { + const userAssessment = await UserAssessment.findById(user_assessment_id); + if (!userAssessment) { + return res.status(404).json({ error: "User assessment not found" }); + } + res.status(200).json(userAssessment); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + +const updateUserAssessment = async (req, res) => { + const { user_assessment_id } = req.params; + + try { + const userAssessment = await UserAssessment.findByIdAndUpdate(user_assessment_id, req.body, { new: true }); + if (!userAssessment) { + return res.status(404).json({ error: "User assessment not found" }); + } + res.status(200).json(userAssessment); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + +const deleteUserAssessment = async (req, res) => { + const { user_assessment_id } = req.params; + + try { + const deletedUserAssessment = await UserAssessment.findByIdAndDelete(user_assessment_id); + if (!deletedUserAssessment) { + return res.status(404).json({ error: "User assessment not found" }); + } + res.status(200).json(deletedUserAssessment); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + +module.exports = { + createUserAssessment, + getAllUserAssessments, + getUserAssessmentById, + updateUserAssessment, + deleteUserAssessment +}; \ No newline at end of file diff --git a/backend/models/userAssessment.model.js b/backend/models/userAssessment.model.js new file mode 100644 index 0000000..3894fdd --- /dev/null +++ b/backend/models/userAssessment.model.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose'); +const { v4: uuidv4 } = require('uuid'); + +const UserAssessmentSchema = new mongoose.Schema({ + user_assessment_id: { + type: String, + required: true, + default: uuidv4().split("-").join("") + }, + user_id: { + type: mongoose.Schema.Types.ObjectId, + ref:"user", + required: true + }, + assessment_id: { + type: mongoose.Schema.Types.ObjectId, + ref: "assessment", + required: true + }, + score: { + type: Number, + required: true + } +}); + +const UserAssessment = mongoose.model('UserAssessment', UserAssessmentSchema); + +module.exports = UserAssessment; \ No newline at end of file diff --git a/backend/routes/userAssessment.routes.js b/backend/routes/userAssessment.routes.js new file mode 100644 index 0000000..9c56677 --- /dev/null +++ b/backend/routes/userAssessment.routes.js @@ -0,0 +1,14 @@ + +const express = require("express"); +const router = express.Router(); + +const userAssessmentController = require("../controllers/userAssessment"); + +// Routes +router.post('/create', userAssessmentController.createUserAssessment); +router.get('/get-all', userAssessmentController.getAllUserAssessments); +router.get('/get/:user_assessment_id', userAssessmentController.getUserAssessmentById); +router.post('/update/:user_assessment_id', userAssessmentController.updateUserAssessment); +router.delete('/delete/:user_assessment_id', userAssessmentController.deleteUserAssessment); + +module.exports = router; \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index 19a22c9..32152c9 100644 --- a/backend/server.js +++ b/backend/server.js @@ -20,7 +20,7 @@ app.use('/api', apiRouter) app.use("/api/auth", require("./routes/user")); app.use("/api/course", require("./routes/course")); - +app.use("/api/user_assessments",require("./routes/userAssessment.routes")); app.listen(port, () => { console.log(`Example app listening on port ${port}`)