From 96c3b22caaba7d436876de5f3209de1cc37d5da1 Mon Sep 17 00:00:00 2001 From: Wo-ogie <siwall0105@gmail.com> Date: Sat, 25 Nov 2023 12:51:31 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=82=B4=20=EC=8A=A4=EC=BC=80=EC=A4=84?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=ED=95=98=EA=B8=B0=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/schedule.js | 59 +++++++++++++++++++++++++++++------------ routes/mySchedule.js | 3 +++ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/controllers/schedule.js b/controllers/schedule.js index 2bea511..5f1886c 100644 --- a/controllers/schedule.js +++ b/controllers/schedule.js @@ -3,6 +3,25 @@ const { getLoggedInParticipantId } = require('../middlewares/auth'); const SchedulesResponse = require('../dto/response/schedulesResponse'); const { createScheduleAlreadyExistError } = require('../errors/scheduleErrors'); +async function createSchedules(participantId, schedules) { + console.log('participantId', participantId); + console.log('schedules', schedules); + return sequelize.transaction(async (transaction) => + Promise.all( + schedules.map(async (availableSchedule) => + Schedule.create( + { + availableDate: availableSchedule.availableDate, + availableTimes: availableSchedule.availableTimes, + ParticipantId: participantId, + }, + { transaction }, + ), + ), + ), + ); +} + async function validateScheduleNotExist(participantId) { const numOfSchedules = await Schedule.count({ where: { @@ -16,25 +35,11 @@ async function validateScheduleNotExist(participantId) { exports.createMySchedules = async (req, res, next) => { const participantId = getLoggedInParticipantId(req, res, next); - const { availableSchedules } = req.body; + const { schedules } = req.body; try { await validateScheduleNotExist(participantId); - - const schedules = await sequelize.transaction(async (transaction) => - Promise.all( - availableSchedules.map((availableSchedule) => - Schedule.create( - { - availableDate: availableSchedule.availableDate, - availableTimes: availableSchedule.availableTimes, - ParticipantId: participantId, - }, - { transaction }, - ), - ), - ), - ); - return res.json(SchedulesResponse.from(schedules)); + const createdSchedules = createSchedules(participantId, schedules); + return res.json(SchedulesResponse.from(createdSchedules)); } catch (error) { return next(error); } @@ -53,3 +58,23 @@ exports.getMySchedules = async (req, res, next) => { return next(error); } }; + +async function deleteAllByParticipantId(participantId) { + await Schedule.destroy({ + where: { + ParticipantId: participantId, + }, + }); +} + +exports.updateMySchedules = async (req, res, next) => { + const participantId = getLoggedInParticipantId(req, res, next); + const { schedules } = req.body; + try { + await deleteAllByParticipantId(participantId); + const createdSchedules = await createSchedules(participantId, schedules); + return res.json(SchedulesResponse.from(createdSchedules)); + } catch (error) { + return next(error); + } +}; diff --git a/routes/mySchedule.js b/routes/mySchedule.js index 6e6291d..4b3aa52 100644 --- a/routes/mySchedule.js +++ b/routes/mySchedule.js @@ -3,6 +3,7 @@ const { isAuthenticated } = require('../middlewares/auth'); const { createMySchedules, getMySchedules, + updateMySchedules, } = require('../controllers/schedule'); const router = express.Router({ mergeParams: true }); @@ -11,4 +12,6 @@ router.post('/bulk', isAuthenticated, createMySchedules); router.get('/', isAuthenticated, getMySchedules); +router.put('/bulk', isAuthenticated, updateMySchedules); + module.exports = router; -- GitLab