Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/user assessment controller #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/controllers/user.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
81 changes: 81 additions & 0 deletions backend/controllers/userAssessment.js
Original file line number Diff line number Diff line change
@@ -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
};
5 changes: 2 additions & 3 deletions backend/models/course.model.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
28 changes: 28 additions & 0 deletions backend/models/userAssessment.model.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions backend/routes/userAssessment.routes.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.