Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 목 데이터 스케쥴러 구현(#725) #821

Merged
merged 24 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
40d034d
feat: 스케쥴러 등록
BGuga Mar 5, 2024
95c8bf6
feat: 앱 실행 시 학교와 축제 데이터를 초기화한다
BGuga Mar 5, 2024
b577046
chore: 클래스 네이밍 변경
BGuga Mar 5, 2024
bf97a21
Merge branch 'dev' into feat/#725
BGuga Mar 27, 2024
6a0beff
feat: MockService 구현
BGuga Mar 28, 2024
a8f348e
feat: 앱 시작시 스케쥴러 1회 실행
BGuga Mar 28, 2024
2a1f3a7
feat: 대학 축제명 변경
BGuga Mar 29, 2024
cf65c16
test: 축제 무대 검증 변경 및 스케쥴러 빈 등록
BGuga Mar 29, 2024
9c97030
chore: 축제 이름 변경
BGuga Mar 29, 2024
214dd73
refactor: Service 기반으로 mock 을 생성한다
BGuga Mar 29, 2024
fd0d2bc
feat: MockDataService cursor 방식 변경 및 queryInfo에 대한 검증 추가
BGuga Mar 29, 2024
2b5b88a
chore: 개행 추가
BGuga Mar 29, 2024
fc36678
chore: 패키지 변경
BGuga Mar 29, 2024
5a78aca
chore: MockDataService profile dev 설정
BGuga Mar 29, 2024
9e0e7e6
refactor: 만약 초기 DB를 초기화하지 않았다면 축제 생성로직은 생략한다
BGuga Mar 29, 2024
ce74234
refactor: mock 전용 서비스, 레포지토리 구분
BGuga Mar 29, 2024
78b13c1
chore: 비 상수 네이밍 적용
BGuga Mar 29, 2024
d0f203a
refactor: 멀티 쓰레드에 안전한 random 사용
BGuga Mar 29, 2024
8513006
refactor: 메서드 가독성 개선 및 주석 추가
BGuga Mar 29, 2024
db8721d
chore: 개행 제거
BGuga Mar 29, 2024
92cae38
refactor: 사용하지 않는 Repository 로직 제거
BGuga Mar 29, 2024
91fbafc
feat: 만약 artist 가 부족할 경우 예외를 발생시키거나
BGuga Mar 29, 2024
8a89543
chore: 개행 추가
BGuga Mar 29, 2024
be08c63
chore: 주석 수정
BGuga Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/src/main/java/com/festago/config/SchedulerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.festago.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@Configuration
public class SchedulerConfig {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.festival.repository;

import com.festago.festival.domain.FestivalQueryInfo;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.Repository;

Expand All @@ -11,5 +12,7 @@ public interface FestivalInfoRepository extends Repository<FestivalQueryInfo, Lo
Optional<FestivalQueryInfo> findByFestivalId(Long festivalId);

void deleteByFestivalId(Long festivalId);

List<FestivalQueryInfo> findAll();
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.festival.domain.Festival;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.Repository;

Expand All @@ -16,8 +17,10 @@ default Festival getOrThrow(Long festivalId) {
boolean existsBySchoolId(Long schoolId);

Festival save(Festival festival);

Optional<Festival> findById(Long festivalId);

void deleteById(Long festivalId);

List<Festival> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.festago.mock;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Profile({"dev"})
@Component
@RequiredArgsConstructor
public class CommandLineAppStartupRunner implements CommandLineRunner {

private final MockDataService mockDataService;
private final MockScheduler mockScheduler;

@Override
public void run(String... args) {
mockDataService.initialize();
mockScheduler.run();
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI/CD 작업으로 서버가 다시 켜지는 일이 잦을텐데, 혹시 이에 대해 고려 해보셨나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialize 여부에 따라 초기 scheduler 로직 호출 여부를 다르게 해주었습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서버가 실행되면 mockScheduler.run(); 해당 로직 호출될텐데.. 이러면 매 서버 시작마다 축제 데이터가 생길 것 같은데.. 문제가 없을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image image

방어 했습니다!

10 changes: 10 additions & 0 deletions backend/src/main/java/com/festago/mock/FestivalDateGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.festago.mock;

import java.time.LocalDate;

public interface FestivalDateGenerator {

LocalDate makeRandomStartDate(int festivalDuration, LocalDate now);

LocalDate makeRandomEndDate(int festivalDuration, LocalDate now, LocalDate startDate);
}
40 changes: 40 additions & 0 deletions backend/src/main/java/com/festago/mock/MockArtist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.festago.mock;

public enum MockArtist {
윤하("https://i.namu.wiki/i/GXgnzRzEe4tq0YmevyZdmLNTPmVK2fzRyZtiCMA-0QSO8vbau9tah4rgP3qNokmT33-El0fx2PauysBruWpTk8B6MLu73gDgVnC3tvFeBnm3Lwo9oRreHM6slID3ilDTyFjpV2eKMO80Jf5u5E920g.webp",
"https://i.namu.wiki/i/IBHythQ09eithIwC-Ix3hr0Sh-AohBVBavl0u-2WW28L6f16JM8F8Klm-0A6gDacgy4t7ATbto7wBc9xt5gwsj0IVG1y9EphSkJun-op4O9OGwvOvX8ES8aTUejOsKPFX5Iat_ubwgGWAYudo5q-Yw.webp"),
뉴진스("https://i.namu.wiki/i/l1co7IV1KFVJ9HBgzxCXkMbgMfuZp_MJhjhqgB7e76DLuokabw6CNlltqr7HGzAMFqt42JfXF94Cflw5XdDuXTS2QkvomS7WYpiiJbuAn5MAjBxOA_zT93dsgyLO-gJXtV0JN-jEQ4tQ-MWtqbHJyA.webp",
"https://i.namu.wiki/i/j5Fs-OjRcQsjfrrFFUVumAWauv-47tj5WPrfyIcCMuBrV5UeStJwaFK17HKcaKxvME2NVpo5PuxVgRpED1huULNxCYBydqsOs-HCLRD-kMztnZdaMJJvi1VefVB1RN0MnwMdxS7xKzxJa11qem0LMg.svg"),
아이유("https://i.namu.wiki/i/k-to3_lfqjjcdnXtWMu3aLtZAArBM1nDpDP6cCWz5iJYm3HjJZ3b7i2H-4-KFSkQ6HOeftXIilMOQXvkdp83hu1FdBv5GE_PyYuacNUSygQ2cnT8vfNHqQVUReYdEYY3ob1BWoyGBE6BQRaHmnGPLw.webp",
"https://i.namu.wiki/i/bJDL9DWmKsrHvvMcLOSKMlVv_E62CX6brjhiddhFuLrGPVYN6-bYcJxUHnE_KP04Ok-8PqezYob8OCepRWFBw6CTDE5Jvde2iJOZEqGgYVt6Gdbub0s9pBIOqzI1DQYZvJbAezbh-8xns_ZugPW68g.webp"),
방탄소년단(
"https://i.namu.wiki/i/EvnNG2DchyHHYmFtyWWzVHhPKkURdc6kdoiRVYisSKHE6BDE8itzfhfYvIMdoX0-6wvum0UgELIowRGR6cuwfNsR1OHrLamq-Rpg0F4XzFMSJHJ_xchPwFBBurNR45kOUYk2ueOKasd-xZ0g9Z14dg.webp",
"https://i.namu.wiki/i/PmFR4AjifhcmdLRXQUPPce9Z7BXVWc6mVX4N22fPUKOzK5ZfjNTfo9e1M7HPa2jiEmG_tuhm43aJMGWLylyQqw.svg"),
푸우("https://i.namu.wiki/i/aj4JXZR4P-ZiY6Gf0EsoPMK3XFHHsSmx0b8ysKnUDpEd0ET1BVQHZIEAGvZGHCNJrn7Nkz7N5zeYzKh3WPSTGdCdOPpj1LnlAceeLWTmMSsiXvl2fyGaEZfRjm7i6DiBTW6_7pDqIRCWfYRQFKUsdg.webp",
"https://i.namu.wiki/i/_EIBF52MTqZAj5wtmY86jsU4fs7aYsdns4guDgLKYWQGoauVdCyZZiFcxc5qI92HxTUiWRRRrK0qk4Ot0qCRGpIc1GUTjUaYz1Y20IKkDuIo9472InXbpNMxcsE8PmP-taYj-7-Ql3_P557yYOk3EA.webp"),
장범준("https://i.namu.wiki/i/6VAPyai_C0lBvsGytiMDu3moDOzS9UH9TDHqxzkjPWFymhQV-vcyH4q884nf1KKH2lVzLqMndBnCOTlUh4ZJE6QeB1oUQEH23d_FwMa3CFsyj8mkn3nG2DQMmJ31TN0cvCrxk6II8-IWq6C-d883zA.webp",
"https://i.namu.wiki/i/WPzIZvkNW_-q9UZmEHLKMtrvAQ2g2Oo7MwbrbzWnWEkRYYAdc2cyougS-n8-BwWsLuo3knt9aaHYEGiyhd7jtg.webp"),
세븐틴("https://i.namu.wiki/i/55JrvWZKaTo_Vik4Wim6-PfXiEmWqwYnAwL5_KmNg9FWiM31U5VV-lmMl76TgtON5hpGP9dIEBhub70rAQvbvOVWt7dQ6GLqvrnpbQSV3Vr1vEKRXjk_RSqCpz5a_7nupUqhB8sBJExEDHf7WGKQDw.webp",
"https://i.namu.wiki/i/l4c_545au41xOK_9XRJyDh1PoNU1k9v-T5NF1UhLHxgCBQJzahV-ra8kP87FLmVFhey_OaWJcrBDJs0RmqbBB3lziiAgbM9lkDUAkENQBv4GweS2MSglXGGno9XQfnzisf5e-Z7185_U4jTqIqTiQA.svg"),
아이들("https://i.namu.wiki/i/LTu_7r5vrTyZgjGb0pV79BoSI_CZr3hwLMnj2s1-ShPb-A07Nc0Gh_rGn8dic1_JwcJlB-pnSunyqmmIP-UhKRw33PlPO5GECFE2u4I5EtKIXN3c5u8_Wln6U22-Ofyjf90PxLLG1BLQziOoQ0d-pg.webp",
"https://i.namu.wiki/i/KwThwv_MdMM3O7mCr-WyXHXCZhfwKtLgAof5i-wIkkgp10izoSGyTKwCgMgBcoAaIP7VocBS-D36nHI6pkiPy3E8ncWJqsqrghC8bwoaM5dOEs32E9QSxk12CZUKCzGg9AM1bJivIuBBzFBpuc5JBg.webp"),
에스파("https://i.namu.wiki/i/KJ5Gpz42djU6ZUsFKnkAnpMS1zRFxUOuqzt9plzbjV_mkFlruZcDULsfEjpVw-2vxjsSKbcGflPlOThHE1DgzST-hnm9jmxPqdPMExPkqH_71ZMF6jhQVfQX6QuNZw3Bz0EZ4C1sO5vpZ-OJNfvTyg.webp",
"https://i.namu.wiki/i/yAfAHme6H-HfWWQCvNAje-KInl_XM-xzRHOUmUxvRxh-HLbzk8KbG6zmD9qQXfUAeCenhHM5whZJ2nhQk0lanzT1LVja3BEQCVk1yPWABxy4NygdaLGyNpiRZTwVFkhD_PnCcESdUQ7-oEtK0YptsQ.webp"),
Comment on lines +4 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 확장자가 .webp, .svg와 같이 끝나는데 클라이언트에서 해당 확장자 지원하는지 확인이 필요할 것 같네요!

;

private final String profileImage;
private final String backgroundImageUrl;

MockArtist(String profileImage, String backgroundImageUrl) {
this.profileImage = profileImage;
this.backgroundImageUrl = backgroundImageUrl;
}

public String getProfileImage() {
return profileImage;
}

public String getBackgroundImageUrl() {
return backgroundImageUrl;
}
}
168 changes: 168 additions & 0 deletions backend/src/main/java/com/festago/mock/MockDataService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.festago.mock;

import com.festago.artist.application.ArtistCommandService;
import com.festago.artist.domain.Artist;
import com.festago.artist.dto.command.ArtistCreateCommand;
import com.festago.artist.repository.ArtistRepository;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.festival.application.command.FestivalCommandFacadeService;
import com.festago.festival.domain.Festival;
import com.festago.festival.dto.command.FestivalCreateCommand;
import com.festago.festival.repository.FestivalRepository;
import com.festago.school.application.SchoolCommandService;
import com.festago.school.domain.School;
import com.festago.school.domain.SchoolRegion;
import com.festago.school.dto.SchoolCreateCommand;
import com.festago.school.repository.SchoolRepository;
import com.festago.stage.application.command.StageCommandFacadeService;
import com.festago.stage.dto.command.StageCreateCommand;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLong;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Profile({"local"})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로파일 잘못 입력된 것 같네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!!

@Service
@Transactional
@RequiredArgsConstructor
public class MockDataService {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 도메인 별로 별도의 서비스를 만들었다면 어땠을까 하는 생각입니다. 😂

Copy link
Member Author

@BGuga BGuga Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mock 내부에서 서비스를 나누는 방향을 말씀하신 건가요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MockFestivalDataService, MockSchoolDataService와 같이 도메인 별 데이터를 세팅해주는 서비스에 대한 얘기였어요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아무래도 이 클래스 자체가 코드가 엄청 길어지다보니 읽는데 부담이 있는데
데이터 셋팅 해주는 메소드들을 별도 클래스로 만들어주고 여기선 객체 의존 & 메소드 호출 해주는 식으로 하면 조금 더 읽기 쉬워질 것 같습니다.

너무 따로 따로 만드는게 부담 된다면 School & artsit / Festival & stage 이렇게 묶어도 괜찮지 않을까요?


private static final AtomicLong FESTIVAL_SEQUENCE = new AtomicLong();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static final long STAGE_START_HOUR = 19L;
private static final int STAGE_ARTIST_COUNT = 3;

private final FestivalDateGenerator festivalDateGenerator;
private final SchoolRepository schoolRepository;
private final ArtistRepository artistRepository;
private final FestivalRepository festivalRepository;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 Repository에 findAll 메서드가 생겨야 하는데, 해당 mock 생성 기능 때문에 도메인이 영향을 받고 있네요.
새로운 mock 전용 Repository를 만들어서 사용하게 하는게 어떤가요?

public interface ForMockSchoolRepository extends JpaRepository<School, Long> {

}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 리뷰 반영하셨다면 기존 Repository에 있는 findAll 메서드 제거할 수 있을 것 같네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제거했습니다!

private final FestivalCommandFacadeService festivalCommandFacadeService;
private final StageCommandFacadeService stageCommandFacadeService;
private final ArtistCommandService artistCommandService;
private final SchoolCommandService schoolCommandService;


public void initialize() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final SchoolCommandService schoolCommandService;
public void initialize() {
private final SchoolCommandService schoolCommandService;
public void initialize() {

if (alreadyInitialized()) {
return;
}
initializeData();
}

private boolean alreadyInitialized() {
return !schoolRepository.findAll().isEmpty();
}

private void initializeData() {
initializeSchool();
initializeArtist();
}

private void initializeSchool() {
for (SchoolRegion schoolRegion : SchoolRegion.values()) {
if (SchoolRegion.ANY.equals(schoolRegion)) {
continue;
}
makeRegionSchools(schoolRegion);
}
}

private void makeRegionSchools(SchoolRegion schoolRegion) {
for (int i = 0; i < 3; i++) {
int schoolNumber = i + 1;
String schoolName = schoolRegion.name() + schoolNumber;
schoolCommandService.createSchool(new SchoolCreateCommand(
schoolName,
schoolName + ".com",
schoolRegion,
null,
null
)
);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이슈 내용에는 나와있는데, 나중에 코드만 보면 어떤 일을 하는지 알기 힘들 것 같네요.
주석을 남겨주는게 어떤가요?

Suggested change
private void makeRegionSchools(SchoolRegion schoolRegion) {
for (int i = 0; i < 3; i++) {
int schoolNumber = i + 1;
String schoolName = schoolRegion.name() + schoolNumber;
schoolCommandService.createSchool(new SchoolCreateCommand(
schoolName,
schoolName + ".com",
schoolRegion,
null,
null
)
);
}
}
/**
* 설명
*/
private void makeRegionSchools(SchoolRegion schoolRegion) {
for (int schoolNumber = 1; schoolNumber <= 3; i++) {
String schoolName = schoolRegion.name() + schoolNumber;
schoolCommandService.createSchool(new SchoolCreateCommand(
schoolName,
schoolName + ".com",
schoolRegion,
null,
null
)
);
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가했습니다


private void initializeArtist() {
for (MockArtist artist : MockArtist.values()) {
artistCommandService.save(new ArtistCreateCommand(
artist.name(),
artist.getProfileImage(),
artist.getBackgroundImageUrl()
)
);
}
}

public void makeMockFestivals(int availableFestivalDuration) {
List<School> allSchool = schoolRepository.findAll();
List<Artist> allArtist = artistRepository.findAll();
for (School school : allSchool) {
makeFestival(availableFestivalDuration, school, allArtist);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

availableFestivalDuration 파라미터를 받지 않고, 여기서 상수로 관리해도 될 것 같은데, 파라미터로 받으신 이유가 있나요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

또한 Mock으로 세팅된 데이터 말고, 직접 사용자가 추가한 School, Artist에 영향을 받을 수 있을 것 같네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

availableFestivalDuration 파라미터를 받지 않고, 여기서 상수로 관리해도 될 것 같은데, 파라미터로 받으신 이유가 있나요?

스케쥴러 주기를 바꿀 수 있다 생각해서 파라미터로 주기를 받도록 만들었습니다!

또한 Mock으로 세팅된 데이터 말고, 직접 사용자가 추가한 School, Artist에 영향을 받을 수 있을 것 같네요!

이 부분은 dev에서 직접 데이터를 만드는 것에 편의를 주기 위해 만든 것이기 때문에 별 상관없다 판단했었습니다!


private void makeFestival(int availableFestivalDuration, School school, List<Artist> artists) {
LocalDate now = LocalDate.now();
LocalDate startDate = festivalDateGenerator.makeRandomStartDate(availableFestivalDuration, now);
LocalDate endDate = festivalDateGenerator.makeRandomEndDate(availableFestivalDuration, now, startDate);

Long newFestivalId = festivalCommandFacadeService.createFestival(new FestivalCreateCommand(
school.getName() + "대 축제" + FESTIVAL_SEQUENCE.incrementAndGet(),
startDate,
endDate,
"https://picsum.photos/536/354",
school.getId()
));

makeStages(newFestivalId, makeRandomArtists(artists));
}

private Queue<Artist> makeRandomArtists(List<Artist> artists) {
List<Artist> randomArtists = new ArrayList<>(artists);
Collections.shuffle(randomArtists);
return new ArrayDeque<>(randomArtists);
}

private void makeStages(Long festivalId, Queue<Artist> artists) {
Festival festival = festivalRepository.findById(festivalId)
.orElseThrow(() -> new NotFoundException(ErrorCode.FESTIVAL_NOT_FOUND));
LocalDate endDate = festival.getEndDate();
LocalDate dateCursor = festival.getStartDate();
while (dateCursor.isBefore(endDate) || dateCursor.equals(endDate)) {
makeStage(festival, artists, dateCursor);
dateCursor = dateCursor.plusDays(1);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LocalDate.datesUntil() 메서드 사용해보는 것도 좋겠네요!


private void makeStage(Festival festival, Queue<Artist> artists, LocalDate localDate) {
LocalDateTime startTime = localDate.atStartOfDay().plusHours(STAGE_START_HOUR);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LocalDateTime startTime = localDate.atStartOfDay().plusHours(STAGE_START_HOUR);
LocalDateTime startTime = localDate.atTime(STAGE_START_HOUR, 0);

stageCommandFacadeService.createStage(new StageCreateCommand(
festival.getId(),
startTime,
startTime.minusDays(1L),
makeStageArtists(artists)
));
}

private List<Long> makeStageArtists(Queue<Artist> artists) {
List<Artist> result = new ArrayList<>();
for (int i = 0; i < STAGE_ARTIST_COUNT; i++) {
Artist artist = artists.poll();
if (artist == null) {
throw new IllegalArgumentException("축제를 구성하기 위한 아티스트가 부족합니다");
}
result.add(artist);
}
return result.stream()
.map(Artist::getId)
.toList();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공연에 포함되는 Artist는 반드시 3으로 나눠 떨어져야 하나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그건 아니고 임의로 하나의 공연당 3개의 아티스트를 넣었습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

artists의 size가 STAGE_ARTIST_COUNT 개수보다 적다면 IllegalArgumentException 예외가 발생할 것 같은데..
size가 그 이하이면 예외를 발생시키지 않고, 그 사이즈 그대로 넣으면 어떤가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했는데 로직이 조금 복잡해졌네요

}
21 changes: 21 additions & 0 deletions backend/src/main/java/com/festago/mock/MockScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.festago.mock;

import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Profile("dev")
@Component
@RequiredArgsConstructor
public class MockScheduler {

private static final long SCHEDULER_CYCLE = 7;
private final MockDataService mockDataService;

@Scheduled(fixedDelay = SCHEDULER_CYCLE, timeUnit = TimeUnit.DAYS)
public void run() {
mockDataService.makeMockFestivals((int) SCHEDULER_CYCLE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.festago.mock;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Random;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Profile("dev")
@Component
public class RandomFestivalDateGenerator implements FestivalDateGenerator {

private static final int COUNT_FIRST_DAY_AS_DURATION_ONE = 1;
private static final int RANDOM_OFFSET = 1;
private static final int MAX_END_DATE_FROM_START_DATE = 2;
private final Random random = new Random();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final Random random = new Random();
private final Random random = ThreadLocalRandom.current();

Random 객체를 생성하는 것 보다, ThreadLocalRandom을 사용하는게 더 좋습니다!
https://www.baeldung.com/java-thread-local-random


@Override
public LocalDate makeRandomStartDate(int festivalDuration, LocalDate now) {
return now.plusDays(random.nextInt(festivalDuration));
}

@Override
public LocalDate makeRandomEndDate(int festivalDuration, LocalDate now, LocalDate startDate) {
long timeUntilFestivalStart = startDate.until(now, ChronoUnit.DAYS);
long maxAvailableEndDateFromStartDate = festivalDuration - (timeUntilFestivalStart + COUNT_FIRST_DAY_AS_DURATION_ONE);
int randomEndDate = random.nextInt((int) (maxAvailableEndDateFromStartDate + RANDOM_OFFSET));
return startDate.plusDays(Math.min(randomEndDate, MAX_END_DATE_FROM_START_DATE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface StageArtistRepository extends Repository<StageArtist, Long> {
Set<Long> findAllArtistIdByStageIdIn(@Param("stageIds") List<Long> stageIds);

void deleteByStageId(Long stageId);

List<StageArtist> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.stage.repository;

import com.festago.stage.domain.StageQueryInfo;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.Repository;

Expand All @@ -11,4 +12,6 @@ public interface StageQueryInfoRepository extends Repository<StageQueryInfo, Lon
Optional<StageQueryInfo> findByStageId(Long stageId);

void deleteByStageId(Long stageId);

List<StageQueryInfo> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface StageRepository extends Repository<Stage, Long>, StageRepositor

List<Stage> findAllByFestivalId(Long festivalId);

List<Stage> findAll();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.festago.festival.domain.FestivalQueryInfo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -39,4 +41,9 @@ public Optional<FestivalQueryInfo> findByFestivalId(Long festivalId) {
public void deleteByFestivalId(Long festivalId) {
memory.entrySet().removeIf(it -> Objects.equals(it.getValue().getFestivalId(), festivalId));
}

@Override
public List<FestivalQueryInfo> findAll() {
return new ArrayList<>(memory.values());
}
}
Loading
Loading