From b00d1dc8681a0c61d6fb2925e0354052917d6ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr> Date: Wed, 4 Dec 2024 19:18:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20req=20body,=20param=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/scheduleController.js | 29 +++++++++++++---------------- dtos/ScheduleRequestDTO.js | 31 ++++++++++++++++--------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/controllers/scheduleController.js b/controllers/scheduleController.js index e2c7122..0f45d89 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 854bd3f..218f9e0 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() }); } -- GitLab