Skip to content
Snippets Groups Projects
Commit b00d1dc8 authored by 조대희's avatar 조대희
Browse files

refactor: req body, param 유효성 체크

parent fcf95cd3
Branches
No related tags found
1 merge request!42[#25] 배포코드 master브랜치로 이동
...@@ -22,13 +22,12 @@ class scheduleController { ...@@ -22,13 +22,12 @@ class scheduleController {
async createSchedule(req, res) { async createSchedule(req, res) {
try { try {
const userId = req.user.id; 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({ const schedule = await ScheduleService.createSchedules({
userId, userId,
title, ...validatedData
is_fixed,
time_indices
}); });
return res.status(201).json({ return res.status(201).json({
...@@ -63,15 +62,11 @@ class scheduleController { ...@@ -63,15 +62,11 @@ class scheduleController {
async updateSchedules(req, res) { async updateSchedules(req, res) {
try { try {
const userId = req.user.id; const userId = req.user.id;
const { originalTitle, title, is_fixed, time_indices } = req.body; const scheduleRequestDTO = new ScheduleRequestDTO(req.body);
const validatedData = scheduleRequestDTO.validate('bulk_update');
const updatedSchedule = await ScheduleService.updateSchedules(userId, {
originalTitle,
title,
is_fixed,
time_indices
});
const updatedSchedule = await ScheduleService.updateSchedules(userId, validatedData);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
data: { data: {
...@@ -110,9 +105,9 @@ class scheduleController { ...@@ -110,9 +105,9 @@ class scheduleController {
async deleteSchedules(req, res) { async deleteSchedules(req, res) {
try { try {
const userId = req.user.id; const userId = req.user.id;
const { title } = req.body; const scheduleRequestDTO = new ScheduleRequestDTO(req.body);
const validatedData = scheduleRequestDTO.validate('bulk_delete');
const result = await ScheduleService.deleteSchedules(userId, title); const result = await ScheduleService.deleteSchedules(userId, validatedData.title);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
...@@ -167,7 +162,9 @@ class scheduleController { ...@@ -167,7 +162,9 @@ class scheduleController {
const { time_idx } = req.params; const { time_idx } = req.params;
const userId = req.user.id; 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({ return res.status(200).json({
success: true, success: true,
......
...@@ -13,27 +13,28 @@ class ScheduleRequestDTO { ...@@ -13,27 +13,28 @@ class ScheduleRequestDTO {
schema = Joi.object({ schema = Joi.object({
title: Joi.string().min(1).max(255).required(), title: Joi.string().min(1).max(255).required(),
is_fixed: Joi.boolean().required(), is_fixed: Joi.boolean().required(),
events: Joi.array().items( time_indices: Joi.array()
Joi.object({ .items(Joi.number().integer().min(0).max(671))
time_idx: Joi.number().integer().min(0).max(671).required(), .min(1)
}) .required()
).min(1).required()
}); });
} else if (type === 'bulk_update') { } else if (type === 'bulk_update') {
schema = Joi.object({ schema = Joi.object({
updates: Joi.array().items( originalTitle: Joi.string().min(1).max(255).required(),
Joi.object({ title: Joi.string().min(1).max(255).required(),
time_idx: Joi.number().integer().min(0).max(671).required(), is_fixed: Joi.boolean().required(),
title: Joi.string().min(1).max(255).optional(), time_indices: Joi.array()
is_fixed: Joi.boolean().optional(), .items(Joi.number().integer().min(0).max(671))
}) .min(1)
).min(1).required() .required()
}); });
} else if (type === 'bulk_delete') { } else if (type === 'bulk_delete') {
schema = Joi.object({ schema = Joi.object({
time_idxs: Joi.array().items( title: Joi.string().min(1).max(255).required()
Joi.number().integer().min(0).max(671).required() });
).min(1).required() } else if (type === 'get_by_time_idx') {
schema = Joi.object({
time_idx: Joi.number().integer().min(0).max(671).required()
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment