Commit 163c73e9 authored by janithgamage1.ed's avatar janithgamage1.ed

fix: update

Desc : update project
parent 9d40242e
...@@ -26,17 +26,39 @@ export const createCurriculum = async (req, res) => { ...@@ -26,17 +26,39 @@ export const createCurriculum = async (req, res) => {
const curriculumData = req.body; const curriculumData = req.body;
try { try {
const newCurriculum = new Curriculum(curriculumData); const newCurriculum = new Curriculum(curriculumData);
// Calculate total tutorial mark
let totalTutorialMark = 0;
for (const tutorialId of curriculumData.tutorials) {
const tutorial = await Tutorial.findById(tutorialId);
totalTutorialMark += tutorial.tutorialMark;
}
newCurriculum.curriculumMark = totalTutorialMark;
await newCurriculum.save(); await newCurriculum.save();
res.status(201).json(newCurriculum); res.status(201).json(newCurriculum);
} catch (error) { } catch (error) {
res.status(400).json({ message: error.message }); res.status(400).json({ message: error.message });
} }
} }
export const updateCurriculum = async (req, res) => { export const updateCurriculum = async (req, res) => {
const { id } = req.params; const { id } = req.params;
const updatedCurriculum = req.body; const updatedCurriculum = req.body;
try { try {
const curriculum = await Curriculum.findById(id);
// Calculate total tutorial mark
let totalTutorialMark = 0;
for (const tutorialId of updatedCurriculum.tutorials) {
const tutorial = await Tutorial.findById(tutorialId);
totalTutorialMark += tutorial.tutorialMark;
}
updatedCurriculum.curriculumMark = totalTutorialMark;
const result = await Curriculum.findByIdAndUpdate(id, updatedCurriculum, { new: true }); const result = await Curriculum.findByIdAndUpdate(id, updatedCurriculum, { new: true });
res.status(200).json(result); res.status(200).json(result);
} catch (error) { } catch (error) {
......
...@@ -21,6 +21,11 @@ export const getTutorialById = async (req, res) => { ...@@ -21,6 +21,11 @@ export const getTutorialById = async (req, res) => {
export const createTutorial = async (req, res) => { export const createTutorial = async (req, res) => {
const tutorialData = req.body; const tutorialData = req.body;
// Calculate total tutorial marks based on task item marks
const totalTaskMarks = tutorialData.taskItems.reduce((total, task) => total + (task.taskItemMark || 0), 0);
tutorialData.tutorialMarks = totalTaskMarks;
try { try {
const newTutorial = new Tutorial(tutorialData); const newTutorial = new Tutorial(tutorialData);
await newTutorial.save(); await newTutorial.save();
...@@ -32,9 +37,14 @@ export const createTutorial = async (req, res) => { ...@@ -32,9 +37,14 @@ export const createTutorial = async (req, res) => {
export const updateTutorial = async (req, res) => { export const updateTutorial = async (req, res) => {
const { id } = req.params; const { id } = req.params;
const updatedTutorial = req.body; const updatedTutorialData = req.body;
// Calculate total tutorial marks based on updated task item marks
const totalTaskMarks = updatedTutorialData.taskItems.reduce((total, task) => total + (task.taskItemMark || 0), 0);
updatedTutorialData.tutorialMarks = totalTaskMarks;
try { try {
const result = await Tutorial.findByIdAndUpdate(id, updatedTutorial, { new: true }); const result = await Tutorial.findByIdAndUpdate(id, updatedTutorialData, { new: true });
res.status(200).json(result); res.status(200).json(result);
} catch (error) { } catch (error) {
res.status(404).json({ message: 'Tutorial not found' }); res.status(404).json({ message: 'Tutorial not found' });
......
import UserProgress from '../models/userProgress.model.js'; import UserProgress from "../models/userProgress.model.js";
export const getUserProgress = async (req, res) => { export const getUserSubscribedCurriculums = async (req, res) => {
const userId = req.params.userId; const userId = req.params.userId;
try { try {
const userProgress = await UserProgress.findOne({ userId }).populate('curriculumId tutorialProgress.tutorialId'); const userProgress = await UserProgress.find({ userId })
.populate('curriculumProgress.curriculumId')
.populate('curriculumProgress.tutorialProgress.tutorialId')
.populate('curriculumProgress.tutorialProgress.taskProgress.taskId');
res.status(200).json(userProgress); res.status(200).json(userProgress);
} catch (error) { } catch (error) {
res.status(500).json({ message: error.message }); res.status(500).json({ message: "Error fetching user progress", error });
} }
} };
export const updateUserProgress = async (req, res) => { export const subscribeToCurriculum = async (req, res) => {
const userId = req.params.userId; const { userId, curriculumId } = req.body;
const { curriculumId, tutorialId, completed, marks } = req.body;
try { try {
let userProgress = await UserProgress.findOne({ userId }); let userProgress = await UserProgress.findOne({ userId });
if (!userProgress) { if (!userProgress) {
userProgress = new UserProgress({ userId }); userProgress = new UserProgress({
userId,
curriculumProgress: [],
totalMarks: 0
});
} }
const curriculumProgress = userProgress.curriculumId.find(prog => prog.curriculumId.equals(curriculumId)); const curriculumProgressIndex = userProgress.curriculumProgress.findIndex(
progress => progress.curriculumId.toString() === curriculumId
);
if (!curriculumProgress) { if (curriculumProgressIndex === -1) {
userProgress.curriculumId.push({ curriculumId }); userProgress.curriculumProgress.push({
curriculumId,
tutorialProgress: []
});
} }
const tutorialProgress = curriculumProgress.tutorialProgress.find(prog => prog.tutorialId.equals(tutorialId)); await userProgress.save();
res.status(201).json({ message: "User subscribed to curriculum", userProgress });
if (!tutorialProgress) { } catch (error) {
curriculumProgress.tutorialProgress.push({ tutorialId, completed }); res.status(500).json({ message: "Error subscribing to curriculum", error });
} else { }
tutorialProgress.completed = completed; };
export const updateUserProgress = async (req, res) => {
const { userId, curriculumId, tutorialId, taskId, taskMarks } = req.body;
try {
const userProgress = await UserProgress.findOne({ userId });
if (!userProgress) {
return res.status(404).json({ message: "User progress not found" });
} }
userProgress.marks = marks; const curriculumProgressIndex = userProgress.curriculumProgress.findIndex(
progress => progress.curriculumId.toString() === curriculumId
);
if (curriculumProgressIndex !== -1) {
const curriculumProgress = userProgress.curriculumProgress[curriculumProgressIndex];
const tutorialProgressIndex = curriculumProgress.tutorialProgress.findIndex(
tutorialProgress => tutorialProgress.tutorialId.toString() === tutorialId
);
if (tutorialProgressIndex !== -1) {
const tutorialProgress = curriculumProgress.tutorialProgress[tutorialProgressIndex];
const taskProgressIndex = tutorialProgress.taskProgress.findIndex(
taskProgress => taskProgress.taskId.toString() === taskId
);
if (taskProgressIndex !== -1) {
const taskProgress = tutorialProgress.taskProgress[taskProgressIndex];
taskProgress.completed = true;
taskProgress.marks = taskMarks;
// Update tutorial completion status
const tutorialTotalMarks = tutorialProgress.tutorialId.totalTutorialMarks; // You'll need to populate this field
if (tutorialTotalMarks > 0) {
tutorialProgress.completed = (tutorialProgress.marks / tutorialTotalMarks) > 0.85;
}
// Update curriculum completion status
const curriculumTotalMarks = curriculumProgress.curriculumId.totalCurriculumMarks; // You'll need to populate this field
if (curriculumTotalMarks > 0) {
curriculumProgress.completed = (curriculumProgress.marks / curriculumTotalMarks) > 0.85;
}
// Update user total marks
userProgress.totalMarks = userProgress.curriculumProgress.reduce(
(total, curriculumProgress) => total + curriculumProgress.marks,
0
);
}
}
}
await userProgress.save(); await userProgress.save();
res.status(200).json(userProgress); res.status(200).json({ message: "User progress updated", userProgress });
} catch (error) { } catch (error) {
res.status(500).json({ message: error.message }); res.status(500).json({ message: "Error updating user progress", error });
} }
} };
\ No newline at end of file
...@@ -11,9 +11,9 @@ const commonFields = { ...@@ -11,9 +11,9 @@ const commonFields = {
type: Date, type: Date,
default: new Date(), default: new Date(),
}, },
}; };
const curriculumSchema = new mongoose.Schema({ const curriculumSchema = new mongoose.Schema({
curriculumCode: { curriculumCode: {
type: String, type: String,
unique: true, // Ensures unique values for curriculumCode unique: true, // Ensures unique values for curriculumCode
...@@ -22,6 +22,10 @@ const curriculumSchema = new mongoose.Schema({ ...@@ -22,6 +22,10 @@ const curriculumSchema = new mongoose.Schema({
curriculumTitle: String, curriculumTitle: String,
curriculumDescription: String, curriculumDescription: String,
curriculumImage: String, curriculumImage: String,
curriculumMark: {
type: Number,
default: 0
},
tutorials: [{ tutorials: [{
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Tutorial", ref: "Tutorial",
......
...@@ -19,6 +19,10 @@ const taskItemSchema = new mongoose.Schema({ ...@@ -19,6 +19,10 @@ const taskItemSchema = new mongoose.Schema({
howToDo: String, howToDo: String,
referenceImage: String, referenceImage: String,
referenceVideo: String, referenceVideo: String,
taskItemMark : {
type: Number,
default: 0
}
// Additional fields for task items // Additional fields for task items
}); });
...@@ -30,6 +34,10 @@ const tutorialSchema = new mongoose.Schema({ ...@@ -30,6 +34,10 @@ const tutorialSchema = new mongoose.Schema({
tutorialTitle: String, tutorialTitle: String,
tutorialDescription: String, tutorialDescription: String,
tutorialImage: String, tutorialImage: String,
tutorialMarks: {
type: Number,
default: 0
},
taskItems: [taskItemSchema], // Embed task items as subdocuments taskItems: [taskItemSchema], // Embed task items as subdocuments
// Additional fields for tutorial details // Additional fields for tutorial details
status: { status: {
......
import mongoose from "mongoose"; import mongoose from "mongoose";
const userProgressSchema = new mongoose.Schema({ const commonFields = {
userId: { createdBy: String,
updatedBy: String,
createdAt: {
type: Date,
default: new Date(),
},
updatedAt: {
type: Date,
default: new Date(),
},
};
const taskProgressSchema = new mongoose.Schema({
taskId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
required: true, ref: 'Task'
ref: 'User' // Reference to the User model },
completed: {
type: Boolean,
default: false
},
taskMarkUser: {
type: Number,
default: 0
}
});
const tutorialProgressSchema = new mongoose.Schema({
tutorialId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Tutorial'
}, },
taskProgress: [taskProgressSchema],
completed: {
type: Boolean,
default: false
},
tutorialMarkUser: {
type: Number,
default: 0
}
});
const curriculumProgressSchema = new mongoose.Schema({
curriculumId: { curriculumId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
required: true, ref: 'Curriculum'
ref: 'Curriculum' // Reference to the Curriculum model },
}, tutorialProgress: [tutorialProgressSchema],
tutorialProgress: [ completed: {
{ type: Boolean,
tutorialId: { default: false
type: mongoose.Schema.Types.ObjectId, },
ref: 'Tutorial' // Reference to the Tutorial model curriculumMarkUser: {
},
completed: {
type: Boolean,
default: false
}
}
],
marks: {
type: Number, type: Number,
default: 0 default: 0
} }
}); });
const userProgressSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
curriculumProgress: [curriculumProgressSchema],
totalCurriculumsMarks: {
type: Number,
default: 0
},
totalCurriculumsMarksUser: {
type: Number,
default: 0
},
...commonFields
});
const UserProgress = mongoose.model("UserProgress", userProgressSchema); const UserProgress = mongoose.model("UserProgress", userProgressSchema);
export default UserProgress; export default UserProgress;
import express from "express"; import express from "express";
import { getUserProgress, updateUserProgress } from "../controllers/userProgress.controller.js"; import { getUserSubscribedCurriculums, subscribeToCurriculum, updateUserProgress } from "../controllers/userProgress.controller.js";
const router = express.Router(); const router = express.Router();
router.get('/:userId', getUserProgress); router.get('/:userId', getUserSubscribedCurriculums);
router.post('/:userId', updateUserProgress); router.post('/:userId', subscribeToCurriculum);
router.put('/:userId', updateUserProgress);
export default router; export default router;
...@@ -231,11 +231,17 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumCardProps }) => ...@@ -231,11 +231,17 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumCardProps }) =>
<Typography variant="caption" color="secondary"> <Typography variant="caption" color="secondary">
Updated in {curriculum.createdAt?.toLocaleTimeString()} Updated in {curriculum.createdAt?.toLocaleTimeString()}
</Typography> </Typography>
<Button variant="outlined" size="small" onClick={() => { <Button variant="outlined" size="small" onClick={() => {
handleClickOpen() handleClickOpen()
}}> }}>
Preview Preview
</Button> </Button>
<Button variant="outlined" size="small" color='success' onClick={() => {
// handleClickOpen()
}}>
Subscribe
</Button>
</Stack> </Stack>
</MainCard> </MainCard>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment