Commit 3a2f0111 authored by janithGamage's avatar janithGamage

feat : update

Desc : leadboard and feedback backend config
parent 7fb6e5cb
{
"cSpell.words": [
"Janith",
"leaderboard",
"SLIIT"
]
}
\ No newline at end of file
import Feedback from '../models/feedback.model.js';
export const createFeedback = async (req, res) => {
const { userId, entityId, rating, comment } = req.body;
try {
const feedback = new Feedback({
userId,
entityId,
rating,
comment
});
await feedback.save();
res.status(201).json({ code: '01', message: 'Feedback submitted successfully' });
} catch (error) {
res.status(500).json({ code: '00', message: 'Error submitting feedback' });
}
}
export const getFeedbackForEntity = async (req, res) => {
const entityId = req.params.entityId;
try {
const feedback = await Feedback.find({ entityId }).populate('userId', 'firstName lastName');
res.status(200).json(feedback);
} catch (error) {
res.status(500).json({ code: '00', message: 'Error fetching feedback' });
}
}
import UserProgress from '../models/userProgress.model.js';
export const getGlobalLeaderboard = async (req, res) => {
try {
const userProgressList = await UserProgress.find({}, 'userId curriculumId tutorialProgress.marks').populate('userId', 'firstName lastName');
// Aggregate user progress data to calculate total marks and progress
const leaderboard = userProgressList.map(userProgress => {
const totalMarks = userProgress.curriculumId.reduce((total, curriculum) => {
return total + curriculum.tutorialProgress.reduce((totalTutMarks, tutorial) => {
return totalTutMarks + (tutorial.completed ? tutorial.marks : 0);
}, 0);
}, 0);
const totalTutorials = userProgress.curriculumId.reduce((totalTut, curriculum) => {
return totalTut + curriculum.tutorialProgress.length;
}, 0);
return {
user: userProgress.userId,
totalMarks,
totalTutorials
};
});
// Sort leaderboard based on total marks
leaderboard.sort((a, b) => b.totalMarks - a.totalMarks);
res.status(200).json(leaderboard);
} catch (error) {
res.status(500).json({ message: 'Error fetching global leaderboard' });
}
}
import mongoose from 'mongoose';
const feedbackSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User' // Reference to the User model
},
entityId: {
type: mongoose.Schema.Types.ObjectId,
required: true, // This could be the tutorial or curriculum ID
ref: 'Tutorial' // Reference to the Tutorial model (or Curriculum model)
},
rating: {
type: Number,
required: true
},
comment: String,
createdAt: {
type: Date,
default: new Date()
}
});
const Feedback = mongoose.model('Feedback', feedbackSchema);
export default Feedback;
import express from 'express';
import { createFeedback, getFeedbackForEntity } from '../controllers/feedback.controller.js';
const router = express.Router();
router.post('/', createFeedback);
router.get('/:entityId', getFeedbackForEntity);
export default router;
import express from 'express';
import { getGlobalLeaderboard } from '../controllers/leaderboard.controller.js';
const router = express.Router();
router.get('/global', getGlobalLeaderboard);
export default router;
......@@ -6,6 +6,8 @@ import mongoose from "mongoose";
//import routes
import curriculumRoutes from "./routes/curriculum.routes.js";
import feedbackRoutes from "./routes/feedback.routes.js";
import leaderboardRoutes from "./routes/leaderboard.routes.js";
import translateRoutes from "./routes/translate.routes.js";
import tutorialRoutes from "./routes/tutorial.routes.js";
import userRoutes from "./routes/user.routes.js";
......@@ -29,8 +31,8 @@ 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);
app.use("/rest_node/feedback", feedbackRoutes);
app.use("/rest_node/leaderboard", leaderboardRoutes);
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;
......
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