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

refactor: 개별 조회/생성 -> 단일 쿼리/벌크 처리 (#23)

parent 04fd4e44
Branches
No related tags found
2 merge requests!42[#25] 배포코드 master브랜치로 이동,!36[#23] 스케줄 로직 리팩토링
...@@ -10,24 +10,33 @@ class ScheduleService { ...@@ -10,24 +10,33 @@ class ScheduleService {
* @param {object} [transaction] - Sequelize 트랜잭션 객체 -> 미팅방에서 쓰기위해 트랜잭션을 넘겨받는걸 추가 * @param {object} [transaction] - Sequelize 트랜잭션 객체 -> 미팅방에서 쓰기위해 트랜잭션을 넘겨받는걸 추가
*/ */
async createSchedules({ userId, title, is_fixed, time_indices }, transaction = null) { async createSchedules({ userId, title, is_fixed, time_indices }, transaction = null) {
// 중복 검사 const overlaps = await Schedule.findAll({
for (const time_idx of time_indices) { where: {
const overlap = await this.checkScheduleOverlap(userId, time_idx, transaction); user_id: userId,
if (overlap) { time_idx: {
throw new Error(`Schedule overlaps at time_idx ${time_idx}`); [Op.in]: time_indices
} }
},
transaction
});
if (overlaps.length > 0) {
throw new Error(`Schedule overlaps at time_idx ${overlaps[0].time_idx}`);
} }
const createdSchedules = await Promise.all( const scheduleData = time_indices.map(time_idx => ({
time_indices.map(time_idx =>
Schedule.create({
user_id: userId, user_id: userId,
title, title,
time_idx, time_idx,
is_fixed is_fixed
}, { transaction }) }));
)
); try {
const createdSchedules = await Schedule.bulkCreate(scheduleData, {
transaction,
returning: true,
validate: true
});
return { return {
id: createdSchedules[0].id, id: createdSchedules[0].id,
...@@ -38,6 +47,9 @@ class ScheduleService { ...@@ -38,6 +47,9 @@ class ScheduleService {
createdAt: createdSchedules[0].createdAt, createdAt: createdSchedules[0].createdAt,
updatedAt: createdSchedules[0].updatedAt updatedAt: createdSchedules[0].updatedAt
}; };
} catch (error) {
throw new Error(`Failed to bulk create schedules: ${error.message}`);
}
} }
async getAllSchedules(userId) { async getAllSchedules(userId) {
...@@ -157,25 +169,19 @@ class ScheduleService { ...@@ -157,25 +169,19 @@ class ScheduleService {
*/ */
async getScheduleByTimeIdx(userId, time_idx) { async getScheduleByTimeIdx(userId, time_idx) {
// 해당 time_idx의 스케줄 찾기 // 해당 time_idx의 스케줄 찾기
const schedule = await Schedule.findOne({ const schedules = await Schedule.findAll({
where: { user_id: userId, time_idx }
});
if (!schedule) {
throw new Error('Schedule not found');
}
// 같은 제목의 모든 스케줄 찾기
const relatedSchedules = await Schedule.findAll({
where: { where: {
user_id: userId, user_id: userId,
title: schedule.title, title: {
is_fixed: schedule.is_fixed [Op.in]: sequelize.literal(
`(SELECT title FROM Schedules WHERE user_id = ${userId} AND time_idx = ${time_idx})`
)
}
}, },
order: [['time_idx', 'ASC']] order: [['time_idx', 'ASC']]
}); });
return ScheduleResponseDTO.groupSchedules(relatedSchedules)[0]; return ScheduleResponseDTO.groupSchedules(schedules)[0];
} }
async getAllSchedules(userId) { async getAllSchedules(userId) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment