From c875f796da18a08c5dbbf0d64fa888a5c1d94dc7 Mon Sep 17 00:00:00 2001 From: Ranindya Paramitha <23520019@std.stei.itb.ac.id> Date: Mon, 26 Apr 2021 21:45:36 +0700 Subject: [PATCH] course exist error --- backend/src/controllers/course.js | 13 +++++++++++-- backend/src/hooks/course.js | 13 +++++++++++++ backend/src/index.js | 1 + backend/src/util/error.js | 8 ++++++++ backend/src/util/hook.js | 18 +++++++++++++++++- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 backend/src/hooks/course.js diff --git a/backend/src/controllers/course.js b/backend/src/controllers/course.js index d07e6664..24486223 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 00000000..4f724848 --- /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 486743c3..15ed41f7 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 4b5b8a21..e562a8df 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 b46aaeb0..a4df6ce0 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 }; -- GitLab