Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WebBack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
websystem
WebBack
Commits
7d66ec50
Commit
7d66ec50
authored
4 months ago
by
조대희
Browse files
Options
Downloads
Patches
Plain Diff
refactor: 개별 조회/생성 -> 단일 쿼리/벌크 처리 (
#23
)
parent
04fd4e44
Branches
Branches containing commit
No related tags found
2 merge requests
!42
[#25] 배포코드 master브랜치로 이동
,
!36
[#23] 스케줄 로직 리팩토링
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
services/scheduleService.js
+43
-37
43 additions, 37 deletions
services/scheduleService.js
with
43 additions
and
37 deletions
services/scheduleService.js
+
43
−
37
View file @
7d66ec50
...
...
@@ -10,34 +10,46 @@ class ScheduleService {
* @param {object} [transaction] - Sequelize 트랜잭션 객체 -> 미팅방에서 쓰기위해 트랜잭션을 넘겨받는걸 추가
*/
async
createSchedules
({
userId
,
title
,
is_fixed
,
time_indices
},
transaction
=
null
)
{
// 중복 검사
for
(
const
time_idx
of
time_indices
)
{
const
overlap
=
await
this
.
checkScheduleOverlap
(
userId
,
time_idx
,
transaction
);
if
(
overlap
)
{
throw
new
Error
(
`Schedule overlaps at time_idx
${
time_idx
}
`
);
}
}
const
overlaps
=
await
Schedule
.
findAll
({
where
:
{
user_id
:
userId
,
time_idx
:
{
[
Op
.
in
]:
time_indices
}
},
transaction
});
const
createdSchedules
=
await
Promise
.
all
(
time_indices
.
map
(
time_idx
=>
Schedule
.
create
({
user_id
:
userId
,
title
,
time_idx
,
is_fixed
},
{
transaction
})
)
);
if
(
overlaps
.
length
>
0
)
{
throw
new
Error
(
`Schedule overlaps at time_idx
${
overlaps
[
0
].
time_idx
}
`
);
}
return
{
id
:
createdSchedules
[
0
].
id
,
const
scheduleData
=
time_indices
.
map
(
time_idx
=>
({
user_id
:
userId
,
title
,
is_fixed
,
time_indices
,
createdAt
:
createdSchedules
[
0
].
createdAt
,
updatedAt
:
createdSchedules
[
0
].
updatedAt
};
time_idx
,
is_fixed
}));
try
{
const
createdSchedules
=
await
Schedule
.
bulkCreate
(
scheduleData
,
{
transaction
,
returning
:
true
,
validate
:
true
});
return
{
id
:
createdSchedules
[
0
].
id
,
user_id
:
userId
,
title
,
is_fixed
,
time_indices
,
createdAt
:
createdSchedules
[
0
].
createdAt
,
updatedAt
:
createdSchedules
[
0
].
updatedAt
};
}
catch
(
error
)
{
throw
new
Error
(
`Failed to bulk create schedules:
${
error
.
message
}
`
);
}
}
async
getAllSchedules
(
userId
)
{
...
...
@@ -157,25 +169,19 @@ class ScheduleService {
*/
async
getScheduleByTimeIdx
(
userId
,
time_idx
)
{
// 해당 time_idx의 스케줄 찾기
const
schedule
=
await
Schedule
.
findOne
({
where
:
{
user_id
:
userId
,
time_idx
}
});
if
(
!
schedule
)
{
throw
new
Error
(
'
Schedule not found
'
);
}
// 같은 제목의 모든 스케줄 찾기
const
relatedSchedules
=
await
Schedule
.
findAll
({
const
schedules
=
await
Schedule
.
findAll
({
where
:
{
user_id
:
userId
,
title
:
schedule
.
title
,
is_fixed
:
schedule
.
is_fixed
title
:
{
[
Op
.
in
]:
sequelize
.
literal
(
`(SELECT title FROM Schedules WHERE user_id =
${
userId
}
AND time_idx =
${
time_idx
}
)`
)
}
},
order
:
[[
'
time_idx
'
,
'
ASC
'
]]
});
return
ScheduleResponseDTO
.
groupSchedules
(
relatedS
chedules
)[
0
];
return
ScheduleResponseDTO
.
groupSchedules
(
s
chedules
)[
0
];
}
async
getAllSchedules
(
userId
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment