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 {
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,
......
......@@ -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()
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment