Skip to content

Commit d6ce031

Browse files
authored
Merge pull request #148 from rahulsingh2312/110-Refactor-Attendance-model
110-refactor-attendance-model
2 parents 6f61778 + a388f41 commit d6ce031

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ EXPOSE 3500
1212
CMD ["npm", "run", "start"]
1313

1414

15-

models/attendance.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,63 @@ import connector from "./databaseUtil";
33
connector.set("debug", true);
44

55
const attendanceSchema = {
6-
date: { type: Date, required: true },
7-
time: { type: String, required: true },
8-
absentees: { type: Array },
6+
student: { type: connector.Schema.Types.ObjectId, ref: "Student", required: "true" },
7+
course: { type: connector.Schema.Types.ObjectId, ref: "Course", required: "true" },
8+
monthlyAttended: { type: Number, default: 0 },
9+
monthlyOccured: { type: Number, default: 0 },
10+
cumulativeAttended: { type: Number, default: 0 },
11+
cumulativeOccured: { type: Number, default: 0 },
912
};
1013

1114
const Attendance = connector.model("Attendance", attendanceSchema);
1215

13-
async function create(date, time, absentees) {
14-
const attendance = new Attendance({
15-
date,
16-
time,
17-
absentees,
18-
});
19-
const newAttendence = await attendance.save();
20-
return newAttendence;
16+
async function create(studentId, courseId) {
17+
try {
18+
const attendance = new Attendance({
19+
student: studentId,
20+
course: courseId,
21+
});
22+
const createAttendance = await attendance.save();
23+
return createAttendance;
24+
} catch (error) {
25+
console.error("Error creating attendance:", error);
26+
return null;
27+
}
2128
}
2229

23-
async function remove(filter) {
24-
const res = await Attendance.findOneAndDelete(filter);
25-
return res;
30+
async function read(attendanceId) {
31+
try {
32+
const attendance = await Attendance.findById(attendanceId);
33+
return attendance;
34+
} catch (error) {
35+
console.error("error reading attendance", error);
36+
return null;
37+
}
2638
}
2739

28-
async function grantAttendance(roll, date) {
29-
const res = await Attendance.findOneAndUpdate(
30-
date,
31-
{ $pull: { absentees: roll } },
32-
{ new: true },
33-
);
34-
return res;
40+
async function update(attendanceId, updateData) {
41+
try {
42+
const updatedAttendance = await Attendance.findByIdAndUpdate(
43+
attendanceId,
44+
updateData,
45+
{ new: true },
46+
);
47+
return updatedAttendance;
48+
} catch (error) {
49+
console.error("error updating attendance:", error);
50+
return null;
51+
}
3552
}
3653

54+
async function remove(attendanceId) {
55+
try {
56+
const deletedAttendance = await Attendance.findByIdAndDelete(attendanceId);
57+
return deletedAttendance;
58+
} catch (error) {
59+
console.error("error removing attendance", error);
60+
return null;
61+
}
62+
}
3763
export default {
38-
create, remove, grantAttendance,
64+
create, remove, update, read,
3965
};

0 commit comments

Comments
 (0)