diff --git a/controllers/scheduleController.js b/controllers/scheduleController.js index e2c7122dfc0eb40b8d395a6003cad9845bc6fa6c..0f45d899cbe545b45cfe2e003a9e7ba707041c1b 100644 --- a/controllers/scheduleController.js +++ b/controllers/scheduleController.js @@ -22,13 +22,12 @@ class scheduleController { async createSchedule(req, res) { try { const userId = req.user.id; - const { title, is_fixed, time_indices } = req.body; + const scheduleRequestDTO = new ScheduleRequestDTO(req.body); + const validatedData = scheduleRequestDTO.validate('create'); const schedule = await ScheduleService.createSchedules({ userId, - title, - is_fixed, - time_indices + ...validatedData }); return res.status(201).json({ @@ -63,15 +62,11 @@ class scheduleController { async updateSchedules(req, res) { try { const userId = req.user.id; - const { originalTitle, title, is_fixed, time_indices } = req.body; - - const updatedSchedule = await ScheduleService.updateSchedules(userId, { - originalTitle, - title, - is_fixed, - time_indices - }); + const scheduleRequestDTO = new ScheduleRequestDTO(req.body); + const validatedData = scheduleRequestDTO.validate('bulk_update'); + const updatedSchedule = await ScheduleService.updateSchedules(userId, validatedData); + return res.status(200).json({ success: true, data: { @@ -110,9 +105,9 @@ class scheduleController { async deleteSchedules(req, res) { try { const userId = req.user.id; - const { title } = req.body; - - const result = await ScheduleService.deleteSchedules(userId, title); + const scheduleRequestDTO = new ScheduleRequestDTO(req.body); + const validatedData = scheduleRequestDTO.validate('bulk_delete'); + const result = await ScheduleService.deleteSchedules(userId, validatedData.title); return res.status(200).json({ success: true, @@ -167,7 +162,9 @@ class scheduleController { const { time_idx } = req.params; const userId = req.user.id; - const schedule = await ScheduleService.getScheduleByTimeIdx(userId, parseInt(time_idx, 10)); + const scheduleRequestDTO = new ScheduleRequestDTO({ time_idx: parseInt(time_idx, 10) }); + const validatedData = scheduleRequestDTO.validate('get_by_time_idx'); + const schedule = await ScheduleService.getScheduleByTimeIdx(userId, validatedData.time_idx); return res.status(200).json({ success: true, diff --git a/dtos/ScheduleRequestDTO.js b/dtos/ScheduleRequestDTO.js index 854bd3f124ebe293fe781924683ed0d9b0a4c168..218f9e040e224a1b7b00f144e510a26e640cb4e3 100644 --- a/dtos/ScheduleRequestDTO.js +++ b/dtos/ScheduleRequestDTO.js @@ -13,27 +13,28 @@ class ScheduleRequestDTO { schema = Joi.object({ title: Joi.string().min(1).max(255).required(), is_fixed: Joi.boolean().required(), - events: Joi.array().items( - Joi.object({ - time_idx: Joi.number().integer().min(0).max(671).required(), - }) - ).min(1).required() + time_indices: Joi.array() + .items(Joi.number().integer().min(0).max(671)) + .min(1) + .required() }); } else if (type === 'bulk_update') { schema = Joi.object({ - updates: Joi.array().items( - Joi.object({ - time_idx: Joi.number().integer().min(0).max(671).required(), - title: Joi.string().min(1).max(255).optional(), - is_fixed: Joi.boolean().optional(), - }) - ).min(1).required() + originalTitle: Joi.string().min(1).max(255).required(), + title: Joi.string().min(1).max(255).required(), + is_fixed: Joi.boolean().required(), + time_indices: Joi.array() + .items(Joi.number().integer().min(0).max(671)) + .min(1) + .required() }); } else if (type === 'bulk_delete') { schema = Joi.object({ - time_idxs: Joi.array().items( - Joi.number().integer().min(0).max(671).required() - ).min(1).required() + title: Joi.string().min(1).max(255).required() + }); + } else if (type === 'get_by_time_idx') { + schema = Joi.object({ + time_idx: Joi.number().integer().min(0).max(671).required() }); }