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/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/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/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}`) 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": {} +}