Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions src/main/java/com/back/domain/board/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class Post extends BaseEntity {
@Column(nullable = false)
private Long likeCount = 0L;

@Column(nullable = false)
private Long bookmarkCount = 0L;

@Column(nullable = false)
private Long commentCount = 0L;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostCategoryMapping> postCategoryMappings = new ArrayList<>();

Expand Down Expand Up @@ -72,6 +78,30 @@ public void decreaseLikeCount() {
}
}

// 북마크 수 증가
public void increaseBookmarkCount() {
this.bookmarkCount++;
}

// 북마크 수 감소
public void decreaseBookmarkCount() {
if (this.bookmarkCount > 0) {
this.bookmarkCount--;
}
}

// 댓글 수 증가
public void increaseCommentCount() {
this.commentCount++;
}

// 댓글 수 감소
public void decreaseCommentCount() {
if (this.commentCount > 0) {
this.commentCount--;
}
}

// -------------------- 헬퍼 메서드 --------------------
// 게시글에 연결된 카테고리 목록 조회
public List<PostCategory> getCategories() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostCategoryRepository extends JpaRepository<PostCategory, Long> {
boolean existsByName(String name);
List<PostCategory> findAllByNameIn(List<String> categoryNames);
}
109 changes: 88 additions & 21 deletions src/main/java/com/back/global/initData/DevInitData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.back.global.initData;

import com.back.domain.board.comment.entity.Comment;
import com.back.domain.board.comment.repository.CommentRepository;
import com.back.domain.board.post.entity.Post;
import com.back.domain.board.post.entity.PostCategory;
import com.back.domain.board.post.repository.PostCategoryRepository;
import com.back.domain.board.post.repository.PostRepository;
import com.back.domain.user.entity.User;
import com.back.domain.user.entity.UserProfile;
import com.back.domain.user.entity.UserStatus;
Expand All @@ -13,51 +19,112 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Configuration
@Profile("default")
@RequiredArgsConstructor
public class DevInitData {
private final UserRepository userRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final PostCategoryRepository postCategoryRepository;
private final PasswordEncoder passwordEncoder;
private final Environment environment;

@Bean
ApplicationRunner DevInitDataApplicationRunner() {
return args -> {
String activeProfile = environment.getProperty("spring.profiles.active", "none");
if (!"default".equals(activeProfile)) {
return; // default 환경이 아니면 실행하지 않음
}
initUsers();
initUsersAndPostsAndComments();
};
}

@Transactional
public void initUsers() {
public void initUsersAndPostsAndComments() {
if (userRepository.count() == 0) {
User admin = User.createAdmin(
"admin",
"[email protected]",
passwordEncoder.encode("12345678!")
);
// -------------------- 유저 --------------------
User admin = User.createAdmin("admin", "[email protected]", passwordEncoder.encode("12345678!"));
admin.setUserProfile(new UserProfile(admin, "관리자", null, null, null, 0));
userRepository.save(admin);

User user1 = User.createUser(
"user1",
"[email protected]",
passwordEncoder.encode("12345678!")
);
User user1 = User.createUser("user1", "[email protected]", passwordEncoder.encode("12345678!"));
user1.setUserProfile(new UserProfile(user1, "사용자1", null, null, null, 0));
user1.setUserStatus(UserStatus.ACTIVE);
userRepository.save(user1);

User user2 = User.createUser(
"user2",
"[email protected]",
passwordEncoder.encode("12345678!")
);
User user2 = User.createUser("user2", "[email protected]", passwordEncoder.encode("12345678!"));
user2.setUserProfile(new UserProfile(user2, "사용자2", null, null, null, 0));
user2.setUserStatus(UserStatus.ACTIVE);
userRepository.save(user2);

User user3 = User.createUser("user3", "[email protected]", passwordEncoder.encode("12345678!"));
user3.setUserProfile(new UserProfile(user3, "사용자3", null, null, null, 0));
user3.setUserStatus(UserStatus.ACTIVE);
userRepository.save(user3);

// -------------------- 게시글 --------------------
createSamplePosts(user1, user2, user3);
}
}

private void createSamplePosts(User user1, User user2, User user3) {
Post post1 = new Post(user1,
"[백엔드] 같이 스프링 공부하실 분 구해요!",
"매주 토요일 오후 2시에 온라인으로 스터디 진행합니다.\n교재는 '스프링 완전정복'을 사용할 예정입니다.");
attachCategories(post1, List.of("백엔드", "직장인", "5~10명"));

Post post2 = new Post(user2,
"[프론트엔드] 리액트 입문 스터디원 모집",
"리액트 교재를 같이 읽고 실습해보는 스터디입니다. GitHub로 코드 리뷰도 진행합니다.");
attachCategories(post2, List.of("프론트엔드", "대학생", "2~4명"));

Post post3 = new Post(user2,
"[CS] 컴퓨터 구조 스터디",
"운영체제, 네트워크, 컴퓨터 구조 기본 개념을 함께 정리해요.\n스터디원 5명 정도 모집합니다.");
attachCategories(post3, List.of("CS", "취준생", "5~10명"));

Post post4 = new Post(user3,
"[알고리즘] 백준 골드 도전 스터디",
"매주 3문제씩 풀이, 코드 리뷰 및 전략 공유합니다.\n실력 향상을 목표로 합니다!");
attachCategories(post4, List.of("알고리즘", "대학생", "5~10명"));

Post post5 = new Post(user1,
"[영어 회화] 직장인 아침 스터디",
"출근 전 30분, 영어회화 연습 스터디입니다.\n줌으로 진행하고 서로 피드백 나눠요 :)");
attachCategories(post5, List.of("영어 회화", "직장인", "2~4명"));

postRepository.saveAll(List.of(post1, post2, post3, post4, post5));

// -------------------- 댓글 --------------------
createSampleComments(user1, user2, user3, post1, post2, post3);
}

private void createSampleComments(User user1, User user2, User user3, Post post1, Post post2, Post post3) {
// Post1에 댓글
Comment comment1 = new Comment(post1, user2, "저도 참여하고 싶어요!");
Comment reply1 = new Comment(post1, user1, "좋아요 :) 디스코드 링크 드릴게요.", comment1);

// Post2에 댓글
Comment comment2 = new Comment(post2, user3, "스터디 모집 기간은 언제까지인가요?");
Comment reply2 = new Comment(post2, user2, "이번 주 일요일까지 받을 예정이에요.", comment2);

// Post3에 댓글
Comment comment3 = new Comment(post3, user1, "CS는 항상 중요하죠 💪");

commentRepository.saveAll(List.of(comment1, reply1, comment2, reply2, comment3));

// 게시글 commentCount 반영
post1.increaseCommentCount();
post2.increaseCommentCount();
post3.increaseCommentCount();

postRepository.saveAll(List.of(post1, post2, post3));
}

private void attachCategories(Post post, List<String> categoryNames) {
List<PostCategory> categories = postCategoryRepository.findAllByNameIn(categoryNames);
if (!categories.isEmpty()) {
post.updateCategories(categories);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -39,6 +40,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class NotificationControllerTest {

@Autowired
Expand Down