From 405e7a508c183bd0ce5c2714882fb022ae038159 Mon Sep 17 00:00:00 2001 From: elangptra Date: Wed, 11 Dec 2024 10:37:28 +0700 Subject: [PATCH] refactor: unique class name --- controllers/monitoringControllers/class.js | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/controllers/monitoringControllers/class.js b/controllers/monitoringControllers/class.js index 13abb40..c7294ee 100644 --- a/controllers/monitoringControllers/class.js +++ b/controllers/monitoringControllers/class.js @@ -129,6 +129,14 @@ export const createClass = async (req, res) => { } try { + const existingClass = await models.Class.findOne({ + where: { NAME_CLASS }, + }); + + if (existingClass) { + return response(400, null, "Class with this name already exists", res); + } + const newClass = await models.Class.create({ NAME_CLASS, TOTAL_STUDENT, @@ -152,8 +160,21 @@ export const updateClassById = async (req, res) => { return response(404, null, "Class not found", res); } - if (NAME_CLASS) classes.NAME_CLASS = NAME_CLASS; - if (TOTAL_STUDENT) classes.TOTAL_STUDENT = TOTAL_STUDENT; + if (NAME_CLASS && NAME_CLASS !== classes.NAME_CLASS) { + const existingClass = await models.Class.findOne({ + where: { NAME_CLASS }, + }); + + if (existingClass) { + return response(400, null, "Class with this name already exists", res); + } + + classes.NAME_CLASS = NAME_CLASS; + } + + if (TOTAL_STUDENT) { + classes.TOTAL_STUDENT = TOTAL_STUDENT; + } await classes.save();