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

added heatmap backend #12

Open
wants to merge 1 commit into
base: main
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
26 changes: 26 additions & 0 deletions backend/controllers/attendanceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Attendance = require('../models/attendanceModel');

exports.upsertAttendance = async (req, res) => {
try {
const { studentId, date, isPresent } = req.body;

const existingAttendance = await Attendance.findOne({ studentId, date });

if (existingAttendance) {
if (isPresent) {
existingAttendance.daysPresent += 1;
await existingAttendance.save();
}
} else {
await Attendance.create({
studentId,
date,
daysPresent: isPresent ? 1 : 0
});
}

res.status(200).json({ message: 'Attendance updated successfully', success: true });
} catch (error) {
res.status(400).json({ message: 'Failed to update attendance', success: false, error: error.message });
}
};
28 changes: 28 additions & 0 deletions backend/controllers/heatmapController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Attendance = require('../models/attendanceModel');
const ParentalBehavior = require('../models/parentalBehaviorModel');

exports.getHeatMapData = async (req, res) => {
try {
const attendanceData = await Attendance.find();
const behaviorData = await ParentalBehavior.find();

const behaviorMap = behaviorData.reduce((map, behavior) => {
map[behavior.studentId] = behavior.behaviorScore;
return map;
}, {});

const aggregatedData = attendanceData.map(att => {
const behaviorScore = behaviorMap[att.studentId] || 0;
return {
studentId: att.studentId,
date: att.date,
status: att.status,
behaviorScore: behaviorScore
};
});

res.status(200).json({ success: true, data: aggregatedData });
} catch (error) {
res.status(400).json({ message: 'Failed to retrieve heat map data', success: false, error: error.message });
}
};
18 changes: 18 additions & 0 deletions backend/controllers/parentalBehaviorcontoller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports.upsertParentalBehavior = async (req, res) => {
try {
const { studentId, behaviorScore } = req.body;

const existingBehavior = await ParentalBehavior.findOne({ studentId });

if (existingBehavior) {
existingBehavior.behaviorScore = behaviorScore;
await existingBehavior.save();
} else {
await ParentalBehavior.create({ studentId, behaviorScore });
}

res.status(200).json({ message: 'Parental behavior updated successfully', success: true });
} catch (error) {
res.status(400).json({ message: 'Failed to update parental behavior', success: false, error: error.message });
}
};
23 changes: 23 additions & 0 deletions backend/models/Attendance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');

const attendanceSchema = new mongoose.Schema({
studentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Student',
required: true
},
date: {
type: Date,
required: true
},
status: {
type: Boolean,
required: true
}
}, {
timestamps: true
});

const Attendance = mongoose.model('Attendance', attendanceSchema);

module.exports = Attendance;
22 changes: 22 additions & 0 deletions backend/models/ParentalStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose=require('mongoose');


const parentalBehaviorSchema = new mongoose.Schema({
studentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Student',
required: true
},
behaviorScore: {
type: Number,
required: true,
min: 0,
max: 10
}
}, {
timestamps: true
});

const ParentalBehavior = mongoose.model('ParentalBehavior', parentalBehaviorSchema);

module.exports = ParentalBehavior;