diff --git a/backend/src/controllers/course.js b/backend/src/controllers/course.js index d07e6664440c99e452009e7f2d82a19a1e032780..244862233f5834897629f6e03e92c0108eb71fd9 100644 --- a/backend/src/controllers/course.js +++ b/backend/src/controllers/course.js @@ -6,7 +6,10 @@ const { handleRequestWithInternalServerError } = require('../util/common'); -const { NotExistError } = require('../util/error'); +const { + NotExistError, + CourseExistError +} = require('../util/error'); const { createCourse, @@ -55,6 +58,8 @@ exports.createCourseData = async (req, res) => { } catch (error) { if (error instanceof NotExistError) { handleRequestWithResourceItemNotFound(res, error); + } else if (error instanceof CourseExistError) { + handleRequestWithInvalidRequestBody(res, error); } else { handleRequestWithInternalServerError(res, error); } @@ -68,6 +73,10 @@ exports.updateCourseData = async (req, res) => { const updatedCourse = await updateCourse(course, changeset); res.json(updatedCourse); } catch (error) { - handleRequestWithInternalServerError(res, error); + if (error instanceof CourseExistError) { + handleRequestWithInvalidRequestBody(res, error); + } else { + handleRequestWithInternalServerError(res, error); + } } }; diff --git a/backend/src/hooks/course.js b/backend/src/hooks/course.js new file mode 100644 index 0000000000000000000000000000000000000000..4f724848b1fb53654234025a2930268ca5275233 --- /dev/null +++ b/backend/src/hooks/course.js @@ -0,0 +1,13 @@ +const { Course } = require('../models/index'); + +const { + checkCourseExist +} = require('../util/hook'); + +Course.beforeCreate(async (course) => { + await checkCourseExist(course, true); +}); + +Course.beforeUpdate(async (course) => { + await checkCourseExist(course, false); +}); \ No newline at end of file diff --git a/backend/src/index.js b/backend/src/index.js index 486743c347395c7d78ce3212825ae09a9bc7acb2..15ed41f75ff5b990b6d3c48ba2b658713cb65fb9 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -22,6 +22,7 @@ const optionRouter = require('./routes/option'); require('./hooks/study-plan'); require('./hooks/course-class'); +require('./hooks/course'); app.use(cors()); app.use(express.json()); diff --git a/backend/src/util/error.js b/backend/src/util/error.js index 4b5b8a21aa4081b31ffe982f5cba58b975d955f6..e562a8dffc8c4ae90ff5d3ef44d578e5bb0c5e2e 100644 --- a/backend/src/util/error.js +++ b/backend/src/util/error.js @@ -42,12 +42,19 @@ class StudyPlanExistError extends Error { } } +class CourseExistError extends Error { + constructor() { + super("Course's already exist"); + } +} + class LecturerNotFoundError extends NotExistError { constructor() { super('Lecturer not found!'); } } + module.exports = { PasswordDoesntMatchError, EmailNotFoundError, @@ -57,4 +64,5 @@ module.exports = { RequiredParameterUndefinedError, StudyPlanExistError, LecturerNotFoundError, + CourseExistError }; diff --git a/backend/src/util/hook.js b/backend/src/util/hook.js index b46aaeb0cd56b5eeb80fe13b5251986290d1a5d1..a4df6ce03bb38b28880d63ee11acc9004a61e152 100644 --- a/backend/src/util/hook.js +++ b/backend/src/util/hook.js @@ -12,6 +12,7 @@ const { CourseClassStatusEnum } = require('../enums/index'); const { ClassUnavailableError, + CourseExistError } = require('./error'); const calculateTotalCredits = async (studyPlanCourses) => { @@ -84,9 +85,24 @@ const checkClassAvailability = async (semester, startYear, courses) => { } }; +const checkCourseExist = async (course, isCreate) => { + + const foundCourse = await Course.findOne({ + where: { + code: course.code, + curriculumYear: course.curriculumYear + }, + }); + + if (isCreate && foundCourse) throw new CourseExistError(); + + if (!isCreate && foundCourse.id !== course.id) throw new CourseExistError(); +}; + module.exports = { calculateTotalCredits, deleteCurrentMeetings, deleteCurrentMeetingAttendances, - checkClassAvailability + checkClassAvailability, + checkCourseExist };