|
| 1 | +import { |
| 2 | + createSemester, updateSemesterById, semesterList, deleteSemesterById, |
| 3 | +} from "#services/semester"; |
| 4 | +import { logger } from "#util"; |
| 5 | + |
| 6 | +async function addSemester(req, res) { |
| 7 | + const { |
| 8 | + number, academicYear, type, startDate, endDate, |
| 9 | + } = req.body; |
| 10 | + |
| 11 | + try { |
| 12 | + const newSemester = await createSemester(number, academicYear, type, startDate, endDate); |
| 13 | + res.json({ res: `added semester ${newSemester.id} `, id: newSemester.id }); |
| 14 | + } catch (error) { |
| 15 | + logger.error("Error while inserting", error); |
| 16 | + res.status(500); |
| 17 | + res.json({ err: "Error while inserting in DB" }); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function updateSemester(req, res) { |
| 22 | + const { id } = req.params; |
| 23 | + const { |
| 24 | + ...data |
| 25 | + } = req.body; |
| 26 | + try { |
| 27 | + await updateSemesterById(id, data); |
| 28 | + res.json({ res: `Updated Semester with id ${id}` }); |
| 29 | + } catch (error) { |
| 30 | + logger.error("Error while updating", error); |
| 31 | + res.status(500); |
| 32 | + res.json({ err: "Error while updating in DB" }); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +async function getSemester(req, res) { |
| 37 | + const filter = req.query; |
| 38 | + const semlist = await semesterList(filter); |
| 39 | + res.json({ res: semlist }); |
| 40 | +} |
| 41 | + |
| 42 | +async function deleteSemester(req, res) { |
| 43 | + const { id } = req.params; |
| 44 | + try { |
| 45 | + await deleteSemesterById(id); |
| 46 | + res.json({ res: `Deleted Semster with id ${id}` }); |
| 47 | + } catch (error) { |
| 48 | + logger.error("Error while deleting", error); |
| 49 | + res.status(500).json({ error: "Error while deleting from DB" }); |
| 50 | + } |
| 51 | +} |
| 52 | +export default { |
| 53 | + addSemester, deleteSemester, getSemester, updateSemester, |
| 54 | +}; |
0 commit comments