Skip to content

Commit fd94ff9

Browse files
committed
Chore: 샘플 데이터 추가
1 parent a9786db commit fd94ff9

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

src/main/java/com/back/domain/board/post/repository/PostCategoryRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.List;
8+
79
@Repository
810
public interface PostCategoryRepository extends JpaRepository<PostCategory, Long> {
911
boolean existsByName(String name);
12+
List<PostCategory> findAllByNameIn(List<String> categoryNames);
1013
}
Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.back.global.initData;
22

3+
import com.back.domain.board.comment.entity.Comment;
4+
import com.back.domain.board.comment.repository.CommentRepository;
5+
import com.back.domain.board.post.entity.Post;
6+
import com.back.domain.board.post.entity.PostCategory;
7+
import com.back.domain.board.post.repository.PostCategoryRepository;
8+
import com.back.domain.board.post.repository.PostRepository;
39
import com.back.domain.user.entity.User;
410
import com.back.domain.user.entity.UserProfile;
511
import com.back.domain.user.entity.UserStatus;
@@ -13,51 +19,118 @@
1319
import org.springframework.security.crypto.password.PasswordEncoder;
1420
import org.springframework.transaction.annotation.Transactional;
1521

22+
import java.util.List;
23+
1624
@Configuration
1725
@RequiredArgsConstructor
1826
public class DevInitData {
1927
private final UserRepository userRepository;
28+
private final PostRepository postRepository;
29+
private final CommentRepository commentRepository;
30+
private final PostCategoryRepository postCategoryRepository;
2031
private final PasswordEncoder passwordEncoder;
2132
private final Environment environment;
33+
2234
@Bean
2335
ApplicationRunner DevInitDataApplicationRunner() {
2436
return args -> {
2537
String activeProfile = environment.getProperty("spring.profiles.active", "none");
2638
if (!"default".equals(activeProfile)) {
2739
return; // default 환경이 아니면 실행하지 않음
2840
}
29-
initUsers();
41+
// if (!"none".equals(activeProfile) && !"default".equals(activeProfile)) {
42+
// return;
43+
// }
44+
initUsersAndPostsAndComments();
3045
};
3146
}
3247

3348
@Transactional
34-
public void initUsers() {
49+
public void initUsersAndPostsAndComments() {
3550
if (userRepository.count() == 0) {
36-
User admin = User.createAdmin(
37-
"admin",
38-
39-
passwordEncoder.encode("12345678!")
40-
);
51+
// -------------------- 유저 --------------------
52+
User admin = User.createAdmin("admin", "[email protected]", passwordEncoder.encode("12345678!"));
4153
admin.setUserProfile(new UserProfile(admin, "관리자", null, null, null, 0));
4254
userRepository.save(admin);
4355

44-
User user1 = User.createUser(
45-
"user1",
46-
47-
passwordEncoder.encode("12345678!")
48-
);
56+
User user1 = User.createUser("user1", "[email protected]", passwordEncoder.encode("12345678!"));
4957
user1.setUserProfile(new UserProfile(user1, "사용자1", null, null, null, 0));
5058
user1.setUserStatus(UserStatus.ACTIVE);
5159
userRepository.save(user1);
5260

53-
User user2 = User.createUser(
54-
"user2",
55-
56-
passwordEncoder.encode("12345678!")
57-
);
61+
User user2 = User.createUser("user2", "[email protected]", passwordEncoder.encode("12345678!"));
5862
user2.setUserProfile(new UserProfile(user2, "사용자2", null, null, null, 0));
5963
user2.setUserStatus(UserStatus.ACTIVE);
6064
userRepository.save(user2);
65+
66+
User user3 = User.createUser("user3", "[email protected]", passwordEncoder.encode("12345678!"));
67+
user3.setUserProfile(new UserProfile(user3, "사용자3", null, null, null, 0));
68+
user3.setUserStatus(UserStatus.ACTIVE);
69+
userRepository.save(user3);
70+
71+
// -------------------- 게시글 --------------------
72+
createSamplePosts(user1, user2, user3);
73+
}
74+
}
75+
76+
private void createSamplePosts(User user1, User user2, User user3) {
77+
Post post1 = new Post(user1,
78+
"[백엔드] 같이 스프링 공부하실 분 구해요!",
79+
"매주 토요일 오후 2시에 온라인으로 스터디 진행합니다.\n교재는 '스프링 완전정복'을 사용할 예정입니다.");
80+
attachCategories(post1, List.of("백엔드", "직장인", "5~10명"));
81+
82+
Post post2 = new Post(user2,
83+
"[프론트엔드] 리액트 입문 스터디원 모집",
84+
"리액트 교재를 같이 읽고 실습해보는 스터디입니다. GitHub로 코드 리뷰도 진행합니다.");
85+
attachCategories(post2, List.of("프론트엔드", "대학생", "2~4명"));
86+
87+
Post post3 = new Post(user2,
88+
"[CS] 컴퓨터 구조 스터디",
89+
"운영체제, 네트워크, 컴퓨터 구조 기본 개념을 함께 정리해요.\n스터디원 5명 정도 모집합니다.");
90+
attachCategories(post3, List.of("CS", "취준생", "5~10명"));
91+
92+
Post post4 = new Post(user3,
93+
"[알고리즘] 백준 골드 도전 스터디",
94+
"매주 3문제씩 풀이, 코드 리뷰 및 전략 공유합니다.\n실력 향상을 목표로 합니다!");
95+
attachCategories(post4, List.of("알고리즘", "대학생", "5~10명"));
96+
97+
Post post5 = new Post(user1,
98+
"[영어 회화] 직장인 아침 스터디",
99+
"출근 전 30분, 영어회화 연습 스터디입니다.\n줌으로 진행하고 서로 피드백 나눠요 :)");
100+
attachCategories(post5, List.of("영어 회화", "직장인", "2~4명"));
101+
102+
postRepository.saveAll(List.of(post1, post2, post3, post4, post5));
103+
104+
// -------------------- 댓글 --------------------
105+
createSampleComments(user1, user2, user3, post1, post2, post3);
106+
}
107+
108+
private void createSampleComments(User user1, User user2, User user3, Post post1, Post post2, Post post3) {
109+
// Post1에 댓글
110+
Comment comment1 = new Comment(post1, user2, "저도 참여하고 싶어요!");
111+
Comment reply1 = new Comment(post1, user1, "좋아요 :) 디스코드 링크 드릴게요.", comment1);
112+
113+
// Post2에 댓글
114+
Comment comment2 = new Comment(post2, user3, "스터디 모집 기간은 언제까지인가요?");
115+
Comment reply2 = new Comment(post2, user2, "이번 주 일요일까지 받을 예정이에요.", comment2);
116+
117+
// Post3에 댓글
118+
Comment comment3 = new Comment(post3, user1, "CS는 항상 중요하죠 💪");
119+
120+
commentRepository.saveAll(List.of(comment1, reply1, comment2, reply2, comment3));
121+
122+
// 게시글 commentCount 반영
123+
post1.increaseCommentCount();
124+
post2.increaseCommentCount();
125+
post3.increaseCommentCount();
126+
127+
postRepository.saveAll(List.of(post1, post2, post3));
128+
}
129+
130+
private void attachCategories(Post post, List<String> categoryNames) {
131+
List<PostCategory> categories = postCategoryRepository.findAllByNameIn(categoryNames);
132+
if (!categories.isEmpty()) {
133+
post.updateCategories(categories);
61134
}
62135
}
63136
}

0 commit comments

Comments
 (0)