Skip to content
Snippets Groups Projects
Commit c875f796 authored by Ranindya Paramitha's avatar Ranindya Paramitha
Browse files

course exist error

parent a893e3e5
1 merge request!115Hotfix Course exist error
......@@ -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);
}
}
};
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
......@@ -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());
......
......@@ -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
};
......@@ -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
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment