Commit 7fb6e5cb authored by janithGamage's avatar janithGamage

feat: update

Desc : user progress backend config
parent f15726d6
import UserProgress from '../models/userProgress.model.js';
export const getUserProgress = async (req, res) => {
const userId = req.params.userId;
try {
const userProgress = await UserProgress.findOne({ userId }).populate('curriculumId tutorialProgress.tutorialId');
res.status(200).json(userProgress);
} catch (error) {
res.status(500).json({ message: error.message });
}
}
export const updateUserProgress = async (req, res) => {
const userId = req.params.userId;
const { curriculumId, tutorialId, completed, marks } = req.body;
try {
let userProgress = await UserProgress.findOne({ userId });
if (!userProgress) {
userProgress = new UserProgress({ userId });
}
const curriculumProgress = userProgress.curriculumId.find(prog => prog.curriculumId.equals(curriculumId));
if (!curriculumProgress) {
userProgress.curriculumId.push({ curriculumId });
}
const tutorialProgress = curriculumProgress.tutorialProgress.find(prog => prog.tutorialId.equals(tutorialId));
if (!tutorialProgress) {
curriculumProgress.tutorialProgress.push({ tutorialId, completed });
} else {
tutorialProgress.completed = completed;
}
userProgress.marks = marks;
await userProgress.save();
res.status(200).json(userProgress);
} catch (error) {
res.status(500).json({ message: error.message });
}
}
...@@ -14,6 +14,20 @@ const curriculumSchema = new mongoose.Schema({ ...@@ -14,6 +14,20 @@ const curriculumSchema = new mongoose.Schema({
curriculumImage: String, curriculumImage: String,
tutorials: [tutorialSchema], // Embed tutorials as subdocuments tutorials: [tutorialSchema], // Embed tutorials as subdocuments
// Additional fields for curriculum details // Additional fields for curriculum details
status: {
type: Number,
default: 1, // Default status as active (1)
},
createdBy: String,
updatedBy: String,
createdAt: {
type: Date,
default: new Date(),
},
updatedAt: {
type: Date,
default: new Date(),
},
}); });
const Curriculum = mongoose.model("Curriculum", curriculumSchema); const Curriculum = mongoose.model("Curriculum", curriculumSchema);
......
...@@ -15,6 +15,20 @@ const tutorialSchema = new mongoose.Schema({ ...@@ -15,6 +15,20 @@ const tutorialSchema = new mongoose.Schema({
tutorialImage: String, tutorialImage: String,
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: {
type: Number,
default: 1, // Default status as active (1)
},
createdBy: String,
updatedBy: String,
createdAt: {
type: Date,
default: new Date(),
},
updatedAt: {
type: Date,
default: new Date(),
},
}); });
const Tutorial = mongoose.model("Tutorial", tutorialSchema); const Tutorial = mongoose.model("Tutorial", tutorialSchema);
......
import mongoose from "mongoose";
const userProgressSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User' // Reference to the User model
},
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: {
type: Number,
default: 0
}
});
const UserProgress = mongoose.model("UserProgress", userProgressSchema);
export default UserProgress;
import express from "express";
import { getUserProgress, updateUserProgress } from "../controllers/userProgress.controller.js";
const router = express.Router();
router.get('/:userId', getUserProgress);
router.post('/:userId', updateUserProgress);
export default router;
...@@ -5,8 +5,11 @@ import express from "express"; ...@@ -5,8 +5,11 @@ import express from "express";
import mongoose from "mongoose"; import mongoose from "mongoose";
//import routes //import routes
import curriculumRoutes from "./routes/curriculum.routes.js";
import translateRoutes from "./routes/translate.routes.js"; import translateRoutes from "./routes/translate.routes.js";
import tutorialRoutes from "./routes/tutorial.routes.js";
import userRoutes from "./routes/user.routes.js"; import userRoutes from "./routes/user.routes.js";
import userProgressRoutes from "./routes/userProgress.routes.js";
dotenv.config(); dotenv.config();
const app = express(); const app = express();
...@@ -23,6 +26,11 @@ app.get("/", (req, res) => { ...@@ -23,6 +26,11 @@ app.get("/", (req, res) => {
//implement routes //implement routes
app.use("/rest_node/ssl", translateRoutes); app.use("/rest_node/ssl", translateRoutes);
app.use("/rest_node/user", userRoutes); app.use("/rest_node/user", userRoutes);
app.use("/rest_node/curriculum", curriculumRoutes);
app.use("/rest_node/tutorial", tutorialRoutes);
app.use("/rest_node/user-progress", userProgressRoutes);
const CONNECTION_URL = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@researchmanagement-appl.vzhn4.mongodb.net/?retryWrites=true&w=majority`; const CONNECTION_URL = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@researchmanagement-appl.vzhn4.mongodb.net/?retryWrites=true&w=majority`;
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
......
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