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) => {
const curriculumData = req.body;
try {
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();
res.status(201).json(newCurriculum);
} catch (error) {
res.status(400).json({ message: error.message });
}
}
export const updateCurriculum = async (req, res) => {
const { id } = req.params;
const updatedCurriculum = req.body;
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 });
res.status(200).json(result);
} catch (error) {
......
......@@ -21,6 +21,11 @@ export const getTutorialById = async (req, res) => {
export const createTutorial = async (req, res) => {
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 {
const newTutorial = new Tutorial(tutorialData);
await newTutorial.save();
......@@ -32,9 +37,14 @@ export const createTutorial = async (req, res) => {
export const updateTutorial = async (req, res) => {
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 {
const result = await Tutorial.findByIdAndUpdate(id, updatedTutorial, { new: true });
const result = await Tutorial.findByIdAndUpdate(id, updatedTutorialData, { new: true });
res.status(200).json(result);
} catch (error) {
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;
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);
} 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) => {
const userId = req.params.userId;
const { curriculumId, tutorialId, completed, marks } = req.body;
export const subscribeToCurriculum = async (req, res) => {
const { userId, curriculumId } = req.body;
try {
let userProgress = await UserProgress.findOne({ userId });
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) {
userProgress.curriculumId.push({ curriculumId });
if (curriculumProgressIndex === -1) {
userProgress.curriculumProgress.push({
curriculumId,
tutorialProgress: []
});
}
const tutorialProgress = curriculumProgress.tutorialProgress.find(prog => prog.tutorialId.equals(tutorialId));
if (!tutorialProgress) {
curriculumProgress.tutorialProgress.push({ tutorialId, completed });
} else {
tutorialProgress.completed = completed;
await userProgress.save();
res.status(201).json({ message: "User subscribed to curriculum", userProgress });
} catch (error) {
res.status(500).json({ message: "Error subscribing to curriculum", error });
}
};
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();
res.status(200).json(userProgress);
res.status(200).json({ message: "User progress updated", userProgress });
} 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 = {
type: Date,
default: new Date(),
},
};
const curriculumSchema = new mongoose.Schema({
};
const curriculumSchema = new mongoose.Schema({
curriculumCode: {
type: String,
unique: true, // Ensures unique values for curriculumCode
......@@ -22,6 +22,10 @@ const curriculumSchema = new mongoose.Schema({
curriculumTitle: String,
curriculumDescription: String,
curriculumImage: String,
curriculumMark: {
type: Number,
default: 0
},
tutorials: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Tutorial",
......
......@@ -19,6 +19,10 @@ const taskItemSchema = new mongoose.Schema({
howToDo: String,
referenceImage: String,
referenceVideo: String,
taskItemMark : {
type: Number,
default: 0
}
// Additional fields for task items
});
......@@ -30,6 +34,10 @@ const tutorialSchema = new mongoose.Schema({
tutorialTitle: String,
tutorialDescription: String,
tutorialImage: String,
tutorialMarks: {
type: Number,
default: 0
},
taskItems: [taskItemSchema], // Embed task items as subdocuments
// Additional fields for tutorial details
status: {
......
import mongoose from "mongoose";
const userProgressSchema = new mongoose.Schema({
userId: {
const commonFields = {
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,
required: true,
ref: 'User' // Reference to the User model
ref: 'Task'
},
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: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Curriculum' // Reference to the Curriculum model
},
tutorialProgress: [
{
tutorialId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Tutorial' // Reference to the Tutorial model
},
completed: {
type: Boolean,
default: false
}
}
],
marks: {
ref: 'Curriculum'
},
tutorialProgress: [tutorialProgressSchema],
completed: {
type: Boolean,
default: false
},
curriculumMarkUser: {
type: Number,
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);
export default UserProgress;
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();
router.get('/:userId', getUserProgress);
router.post('/:userId', updateUserProgress);
router.get('/:userId', getUserSubscribedCurriculums);
router.post('/:userId', subscribeToCurriculum);
router.put('/:userId', updateUserProgress);
export default router;
......@@ -231,11 +231,17 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumCardProps }) =>
<Typography variant="caption" color="secondary">
Updated in {curriculum.createdAt?.toLocaleTimeString()}
</Typography>
<Button variant="outlined" size="small" onClick={() => {
handleClickOpen()
}}>
Preview
</Button>
<Button variant="outlined" size="small" color='success' onClick={() => {
// handleClickOpen()
}}>
Subscribe
</Button>
</Stack>
</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