-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
649 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import Homework from "../models/homework"; | ||
import User from "../models/user"; | ||
import { getClassById, getClassesByStudentId } from "./class"; | ||
|
||
async function createHomework( | ||
centreId: number, | ||
classId: number, | ||
creatorId: number, | ||
title: string, | ||
description: string, | ||
dueDate: string | ||
): Promise<Homework> { | ||
try { | ||
return await Homework.create({ | ||
centreId: centreId, | ||
classId: classId, | ||
creatorId: creatorId, | ||
title: title, | ||
description: description, | ||
dueDate: new Date(dueDate), | ||
}); | ||
} catch (error: any) { | ||
throw new Error(`Failed to create homework: ${error.message}`); | ||
} | ||
} | ||
|
||
async function deleteHomework( | ||
centreId: number, | ||
homeworkId: number | ||
): Promise<boolean> { | ||
try { | ||
const numDeletedRows = await Homework.destroy({ | ||
where: { centreId: centreId, id: homeworkId }, | ||
}); | ||
return numDeletedRows > 0; | ||
} catch (error: any) { | ||
throw new Error(`Failed to delete homework: ${error.message}`); | ||
} | ||
} | ||
|
||
async function editHomework( | ||
centreId: number, | ||
userId: number, | ||
homeworkId: number, | ||
title: string, | ||
description: string, | ||
dueDate: string | ||
): Promise<Homework> { | ||
try { | ||
const homework = await Homework.findOne({ | ||
where: { centreId: centreId, id: homeworkId }, | ||
}); | ||
if (!homework) { | ||
throw new Error(`Homework with ID ${homeworkId} not found`); | ||
} | ||
|
||
if (description) { | ||
homework.description = description; | ||
} | ||
|
||
if (title) { | ||
homework.title = title; | ||
} | ||
|
||
if (dueDate) { | ||
homework.dueDate = new Date(dueDate); | ||
} | ||
|
||
homework.creatorId = userId; | ||
|
||
await homework.save(); | ||
await homework.reload(); | ||
|
||
return homework; | ||
} catch (error: any) { | ||
throw new Error(`Failed to update homework: ${error.message}`); | ||
} | ||
} | ||
|
||
async function getHomeworkByClassId( | ||
centreId: number, | ||
classId: number | ||
): Promise<Homework[]> { | ||
try { | ||
const targetClass = await getClassById(centreId, classId); | ||
if (!targetClass) { | ||
throw new Error(`Class with ID ${classId} not found`); | ||
} | ||
|
||
return await Homework.findAll({ | ||
where: { centreId: centreId, classId: classId }, | ||
order: [["createdAt", "DESC"]], | ||
include: [ | ||
{ | ||
model: User, | ||
as: "creator", | ||
attributes: ["id", "fullName"], | ||
}, | ||
], | ||
}); | ||
} catch (error: any) { | ||
throw new Error(`Failed to get announcements: ${error.message}`); | ||
} | ||
} | ||
|
||
async function getHomeworkByStudentId( | ||
centreId: number, | ||
studentId: number | ||
): Promise<Homework[]> { | ||
try { | ||
const classes = await getClassesByStudentId(centreId, studentId); | ||
const classIds = classes.map((c) => c.id); | ||
|
||
return await Homework.findAll({ | ||
where: { centreId: centreId, classId: classIds }, | ||
order: [["createdAt", "DESC"]], | ||
include: [ | ||
{ | ||
model: User, | ||
as: "creator", | ||
attributes: ["id", "fullName"], | ||
}, | ||
], | ||
}); | ||
} catch (error: any) { | ||
throw new Error(`Failed to get note: ${error.message}`); | ||
} | ||
} | ||
|
||
export { | ||
createHomework, | ||
deleteHomework, | ||
editHomework, | ||
getHomeworkByClassId, | ||
getHomeworkByStudentId, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Request, Response } from "express"; | ||
|
||
import { createHomework } from "../../dataaccess/homework"; | ||
import { AddHomeworkParams } from "../../params/homework/addHomework"; | ||
import User from "../../models/user"; | ||
|
||
const SUCCESS_CREATED_HOMEWORK = "Homework created successfully"; | ||
|
||
const ERROR_FAILED_TO_CREATE_HOMEWORK = "Failed to create homework"; | ||
|
||
export default async function handleAddHomework( | ||
req: Request, | ||
res: Response, | ||
params: AddHomeworkParams | ||
) { | ||
try { | ||
const user = req.body.user as User; | ||
|
||
const response = await createHomework( | ||
user.centreId, | ||
params.classId, | ||
user.id, | ||
params.title, | ||
params.description, | ||
params.dueDate | ||
); | ||
|
||
res | ||
.status(201) | ||
.json({ message: SUCCESS_CREATED_HOMEWORK, newHomeWork: response }); | ||
} catch (error: any) { | ||
res.status(500).json({ | ||
message: ERROR_FAILED_TO_CREATE_HOMEWORK, | ||
error: error.message, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Request, Response } from "express"; | ||
|
||
import { DeleteHomeworkParams } from "../../params/homework/deleteHomework"; | ||
import { deleteHomework } from "../../dataaccess/homework"; | ||
import User from "../../models/user"; | ||
|
||
const SUCCESS_DELETE_HOMEWORK = "Homework deleted successfully"; | ||
|
||
const ERROR_FAILED_TO_DELETE_HOMEWORK = "Failed to delete homework"; | ||
|
||
export default async function handleDeleteHomework( | ||
req: Request, | ||
res: Response, | ||
params: DeleteHomeworkParams | ||
) { | ||
try { | ||
const user: User = req.body.user; | ||
await deleteHomework(user.centreId, params.homeworkId); | ||
|
||
res.status(201).json({ message: SUCCESS_DELETE_HOMEWORK }); | ||
} catch (error: any) { | ||
res.status(500).json({ | ||
message: ERROR_FAILED_TO_DELETE_HOMEWORK, | ||
error: error.message, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Request, Response } from "express"; | ||
|
||
import User from "../../models/user"; | ||
import { EditHomeworkParams } from "../../params/homework/editHomework"; | ||
import { editHomework } from "../../dataaccess/homework"; | ||
|
||
const SUCCESS_EDITED_HOMEWORK = "Homework edited successfully"; | ||
|
||
const ERROR_FAILED_TO_EDIT_HOMEWORK = "Failed to edit homework"; | ||
|
||
export default async function handleEditHomework( | ||
req: Request, | ||
res: Response, | ||
params: EditHomeworkParams | ||
) { | ||
try { | ||
const user: User = req.body.user; | ||
await editHomework( | ||
user.centreId, | ||
user.id, | ||
params.homeworkId, | ||
params.title, | ||
params.description, | ||
params.dueDate | ||
); | ||
|
||
res.status(201).json({ message: SUCCESS_EDITED_HOMEWORK }); | ||
} catch (error: any) { | ||
res.status(500).json({ | ||
message: ERROR_FAILED_TO_EDIT_HOMEWORK, | ||
error: error.message, | ||
}); | ||
} | ||
} |
Oops, something went wrong.