Skip to content

Commit 5fea4e1

Browse files
committed
✨ 스케쥴 수정 기능 추가
1 parent 31b0e22 commit 5fea4e1

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/routes/calendar/router.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import validatorErrorChecker from '../../middlewares/validatorErrorChecker';
33
import { body, param, query } from 'express-validator';
44
import HTTPError from '../../utils/HTTPError';
55
import jwtVerifier from '../../middlewares/jwtVerifier';
6-
import { createCalendar, deleteSchedule, getCalendar } from './service';
6+
import { createCalendar, deleteSchedule, getCalendar, patchSchedule } from './service';
77

88
const router = Router();
99
router.use(express.urlencoded({ extended: false }));
@@ -79,4 +79,32 @@ router.delete('/:scheduleid',
7979
}
8080
)
8181

82+
router.patch('/:scheduleid',
83+
jwtVerifier,
84+
param("scheduleid").isNumeric().notEmpty(),
85+
body("color").optional().isString(),
86+
body("memo").optional().isString(),
87+
body("users").optional(),
88+
body("users.*").toInt(),
89+
body("start").optional().toInt(),
90+
body("end").optional().toInt(),
91+
validatorErrorChecker,
92+
async (req, res, next) => {
93+
try {
94+
const uid = req.uid!;
95+
const scheduleid = parseInt(req.params.scheduleid);
96+
const color: string | undefined = req.body.color;
97+
const memo: string | undefined = req.body.memo;
98+
const userList: number[] | undefined = req.body.users;
99+
const start: number | undefined = req.body.start;
100+
const end: number | undefined = req.body.end;
101+
102+
await patchSchedule(uid, scheduleid, color, memo, userList, start, end);
103+
}
104+
catch(e) {
105+
next(e);
106+
}
107+
}
108+
)
109+
82110
export default router;

src/routes/calendar/service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,33 @@ export const deleteSchedule = async (uid: number, scheduleid: number) => {
9595
eq(usersSchedules.uid, uid),
9696
eq(usersSchedules.scheduleid, scheduleid)
9797
));
98+
}
99+
100+
export const patchSchedule = async (
101+
uid: number,
102+
scheduleid: number,
103+
color: string | undefined,
104+
memo: string | undefined,
105+
userList: number[] | undefined,
106+
start: number | undefined,
107+
end: number | undefined) => {
108+
let result1 = await db.select({ count: count() }).from(usersSchedules).where(and(
109+
eq(usersSchedules.uid, uid),
110+
eq(usersSchedules.scheduleid, scheduleid)
111+
));
112+
if (result1[0].count == 0)
113+
throw new HTTPError(403, "Forbidden");
114+
115+
await db.update(schedules).set({
116+
color,
117+
note: memo,
118+
start: start ? new Date(start * 1000) : undefined,
119+
end: end ? new Date(end * 1000) : undefined
120+
});
121+
122+
if (userList)
123+
await db.insert(usersSchedules).ignore().values(userList.map(uid => ({
124+
uid,
125+
scheduleid
126+
})));
98127
}

0 commit comments

Comments
 (0)