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 course controller #27

Open
wants to merge 3 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
};
73 changes: 73 additions & 0 deletions backend/controllers/userCourse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const UserCourses = require('../models/UserCourses');

const createCourseForUser = async (req, res) => {
const { user_id, course_id } = req.body;

// Logic to check if the user_id and course_id exist in your database, omitted for brevity

if (!user_id) {
return res.status(404).json({ error: "User not found" });
}

if (!course_id) {
return res.status(404).json({ error: "Course not found" });
}

try {
// Logic to create a new course for a user in your database, omitted for brevity
// Assume the UserCourses model and creation logic
const newCourseForUser = await UserCourses.create({ user_id, course_id });

res.status(201).json(newCourseForUser);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const getUserCourses = async (req, res) => {
const { user_id } = req.params;

try {
// Get all courses for a specific user from your database based on user_id
const coursesForUser = await UserCourses.find({ user_id });

res.status(200).json(coursesForUser);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const getCoursesUser = async (req, res) => {
const { user_id, course_id } = req.params;

try {
// Get all users for a particular course with a particular course_id
// Logic to fetch users for a course based on course_id, omitted for brevity
const usersForCourse = await UserCourses.find({ course_id });

res.status(200).json(usersForCourse);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const deleteUserCourse = async (req, res) => {
const { user_id, course_id } = req.params;

try {
// Check if the user and course exist and delete the course for the user
// Logic to check and delete the course for the user, omitted for brevity

// Assuming successful deletion
res.status(200).json({ message: `Course ${course_id} for user ${user_id} deleted successfully` });
} catch (error) {
res.status(500).json({ error: error.message });
}
};

module.exports = {
createCourseForUser,
getUserCourses,
getCoursesUser,
deleteUserCourse
};
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;
11 changes: 11 additions & 0 deletions backend/routes/useCourse.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const userCoursesController = require('../controllers/user_courses');

const router = express.Router();

router.post('/:user_id/courses/', userCoursesController.createCourseForUser);
router.delete('/:user_id/courses/:course_id', userCoursesController.deleteUserCourse);
router.get('/:user_id', userCoursesController.getUserCourses);
router.get('/:course_id/users/', userCoursesController.getCoursesUser);

module.exports = router;
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;
3 changes: 2 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ 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.use("/api/user_course" ,require("routes/userCourse.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.