-
Notifications
You must be signed in to change notification settings - Fork 20
feat: postSeeder 추가 #1029
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
Merged
Merged
feat: postSeeder 추가 #1029
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
478abba
feat(seed): 게시판 시딩 코드 추가
hyoinkang aa22796
feat(seed): Post 시딩을 위한 템플릿 메서드 패턴 적용 및 기반 유틸 구현
hyoinkang 5a9777d
feat(seed): PostSeeder 구체화
hyoinkang c7cb9e1
feat(seed): CommentSeeder 구현
hyoinkang 24cccd9
feat(seed): ChildCommentSeeder 및 InteractionSeeder 구현
hyoinkang 8e3b7b0
Merge branch 'dev' of https://github.com/CAUCSE/CAUSW_backend into ch…
hyoinkang 87b4865
style: spotless 포매팅 적용
hyoinkang 95e840f
fix: 제미나이 코드 리뷰 반영
hyoinkang 92db8f1
Merge branch 'dev' of https://github.com/CAUCSE/CAUSW_backend into ch…
hyoinkang a899901
Merge branch 'dev' into chore/#1024/post-seed
hyoinkang baaf724
Merge branch 'chore/#1024/post-seed' of https://github.com/CAUCSE/CAU…
hyoinkang 7e4dcb8
fix: 시더 실행 순서 보장 및 이미지 시간 중복 문제 해결
hyoinkang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app-main/src/main/java/net/causw/app/main/shared/seed/BoardSeeder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package net.causw.app.main.shared.seed; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.causw.app.main.domain.community.board.entity.Board; | ||
| import net.causw.app.main.domain.community.board.entity.BoardApply; | ||
| import net.causw.app.main.domain.community.board.entity.BoardApplyStatus; | ||
| import net.causw.app.main.domain.community.board.repository.BoardApplyRepository; | ||
| import net.causw.app.main.domain.community.board.repository.BoardRepository; | ||
| import net.causw.app.main.domain.user.account.entity.user.User; | ||
| import net.causw.app.main.domain.user.account.repository.user.UserRepository; | ||
| import net.causw.global.constant.StaticValue; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Component | ||
| @Profile("seed") | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class BoardSeeder { | ||
| private final EntityManager em; | ||
| private final BoardApplyRepository boardApplyRepository; | ||
| private final BoardRepository boardRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Transactional | ||
| public void seed() { | ||
| if (boardRepository.count() > 0) { | ||
| log.warn("🚫 Seed skipped: boards already exist"); | ||
| return; | ||
| } | ||
|
|
||
| User user = userRepository.findTopBy() | ||
| .orElseThrow(() -> new IllegalStateException("🚫 Seed skipped: User not found for seeding boards")); | ||
|
|
||
| process(user); | ||
| } | ||
|
|
||
| private void process(User user) { | ||
| // 게시판 카테고리 이름 예시 (20개) | ||
| List<String> boardNames = List.of( | ||
| "자유게시판", "익명게시판", "질문게시판", "정보공유", "취업게시판", | ||
| "동아리홍보", "스터디모집", "중고장터", "분실물센터", "학교생활", | ||
| "유머게시판", "건의사항", "컴공게시판", "학사공지", "장학공지", | ||
| "학생회 공지 게시판", "크자회 공지 게시판", "서비스 공지", "졸업생게시판", "새내기게시판" | ||
| ); | ||
|
|
||
| int count = 0; | ||
| for (String name : boardNames) { | ||
| createActiveBoard(user, name, count++); | ||
| } | ||
|
|
||
| em.flush(); | ||
| em.clear(); | ||
|
|
||
| log.info("✅ Seeded {} boards completed.", boardNames.size()); | ||
| } | ||
|
|
||
| private void createActiveBoard(User user, String name, int index) { | ||
hyoinkang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BoardApply boardApply = BoardApply.of( // 일반 게시판 신청 | ||
| user, | ||
| name, | ||
| null, | ||
| StaticValue.BOARD_NAME_APP_FREE, | ||
| false, | ||
| null | ||
| ); | ||
| boardApply.updateAcceptStatus(BoardApplyStatus.ACCEPTED); | ||
| this.boardApplyRepository.save(boardApply); | ||
| em.persist(boardApply); | ||
|
|
||
| Board newBoard = Board.of( // 일반 게시판 생성 | ||
| boardApply.getBoardName(), | ||
| boardApply.getDescription(), | ||
| boardApply.getCategory(), | ||
| boardApply.getIsAnonymousAllowed(), | ||
| null | ||
| ); | ||
|
|
||
| this.boardRepository.save(newBoard); | ||
| em.persist(newBoard); | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
app-main/src/main/java/net/causw/app/main/shared/seed/PostSeedRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package net.causw.app.main.shared.seed; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @Profile("seed") | ||
| @Slf4j | ||
| public class PostSeedRunner implements CommandLineRunner { | ||
|
|
||
| private final BoardSeeder boardSeeder; | ||
|
|
||
| public PostSeedRunner(BoardSeeder boardSeeder) { | ||
| this.boardSeeder = boardSeeder; | ||
| } | ||
hyoinkang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
| log.info("🌱 Seeding data initialized..."); | ||
| // NOTE: 유저 시딩 필수 | ||
| boardSeeder.seed(); | ||
| log.info("🌳 Seeding data finished."); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.