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

feat: Post 도메인 Service 단위 테스트 #252

Merged
merged 23 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b243928
refactor: HOT_POST_LIKES_THRESHOLD MagicNumber 등록
jjuny0310 Aug 28, 2023
de17d93
fix: 익명유저일 때 ssafyInfo에 데이터가 들어있는 문제 수정
jjuny0310 Sep 4, 2023
4d81e43
fix: GetPostReqDto build추가
jjuny0310 Sep 5, 2023
b2b90c6
test: report 관련 repository 삭제
jjuny0310 Sep 5, 2023
24b7f73
test: 정상적인 BoardId, Cursor, Size가 주어졌다면 게시글 목록 조회가 성공합니다.
jjuny0310 Sep 7, 2023
06a1314
test: 존재하지 않은 boardId가 주어졌다면 게시글 목록 조회 실패하는 테스트 코드 구현
jjuny0310 Sep 7, 2023
133596f
test: 로그인 시 유효한 postId와 loginMemberId가 주어졌다면 게시글 상세 보기가 성공합니다.
jjuny0310 Sep 13, 2023
446ac13
test: 비 로그인 시 유효한 postId가 주어졌다면 게시글 상세 보기가 성공합니다.
jjuny0310 Sep 16, 2023
bae0223
test: 유효하지 않은 postId가 주어졌다면 게시글 상세보기에 예외를 발생합니다.
jjuny0310 Sep 16, 2023
36526e7
test: 로그인 시 유효하지 않은 loginMemberId가 주어졌다면 게시글 상세보기에 예외를 발생합니다.
jjuny0310 Sep 16, 2023
8b05282
fix: 유효하지 않은 게시글을 좋아요 하면 예외 처리 추가
jjuny0310 Sep 16, 2023
c201d49
fix: Hot게시글이 중복되어 등록되는 버그 수정
jjuny0310 Sep 16, 2023
99e544f
test: 게시글을 좋아요 하지 않았다면 좋아요가 저장됩니다.
jjuny0310 Sep 16, 2023
36005d7
test: 게시글을 이미 좋아요 했다면 좋아요가 취소됩니다.
jjuny0310 Sep 16, 2023
35d3560
test: 좋아요가 특정 개수를 달성했다면 Hot 게시글로 등록됩니다.
jjuny0310 Sep 16, 2023
ec317ff
test: 좋아요가 특정 개수를 달성했지만 이미 Hot 게시글이라면 등록되지 않습니다.
jjuny0310 Sep 16, 2023
53bc96a
test: 유효하지 않은 postId 또는 loginMemberId가 주어졌다면 게시글 좋아요에 예외를 발생합니다.
jjuny0310 Sep 16, 2023
f09e699
fix: 유효하지 않은 게시글을 스크랩했을 때 예외 처리 추가
jjuny0310 Sep 16, 2023
8f34218
test: 게시글을 스크랩 하지 않았다면 스크랩이 저장됩니다.
jjuny0310 Sep 16, 2023
239fece
test: 게시글을 이미 스크랩 했다면 스크랩이 취소됩니다.
jjuny0310 Sep 16, 2023
96291ba
test: 유효하지 않은 loginMemberId가 주어졌다면 게시글 스크랩에 예외를 발생합니다.
jjuny0310 Sep 16, 2023
379aab5
test: 유효하지 않은 postId가 주어졌다면 게시글 스크랩에 예외를 발생합니다.
jjuny0310 Sep 16, 2023
edacb73
fix: commentGroup 수정하는 로직 NativeQuery로 수정
jjuny0310 Sep 23, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ssafy.ssafsound.batch.config;

import com.ssafy.ssafsound.batch.tasklet.PostTasklet;
import com.ssafy.ssafsound.domain.post.service.PostConstantProvider;
import com.ssafy.ssafsound.domain.post.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,6 +22,7 @@ public class PostBatchConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
private final PostService postService;
private final PostConstantProvider postConstantProvider;


@Bean
Expand All @@ -40,6 +42,6 @@ public Step deleteHotPostStep() {

@Bean
public Tasklet deleteHotPostTasklet() {
return new PostTasklet(postService);
return new PostTasklet(postService, postConstantProvider);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ssafy.ssafsound.batch.tasklet;

import com.ssafy.ssafsound.domain.post.service.PostConstantProvider;
import com.ssafy.ssafsound.domain.post.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -10,22 +11,20 @@
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
@RequiredArgsConstructor
public class PostTasklet implements Tasklet, StepExecutionListener {
@Value("${spring.constant.post.HOT_POST_LIKES_THRESHOLD}")
private Long HOT_POST_LIKES_THRESHOLD;
private final PostService postService;
private final PostConstantProvider postConstantProvider;

@Override
public void beforeStep(StepExecution stepExecution) {
}

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
postService.deleteHotPostsUnderThreshold(HOT_POST_LIKES_THRESHOLD);
postService.deleteHotPostsUnderThreshold(postConstantProvider.getHOT_POST_LIKES_THRESHOLD());
return RepeatStatus.FINISHED;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ssafy.ssafsound.domain.comment.domain;

import com.ssafy.ssafsound.domain.BaseTimeEntity;
import com.ssafy.ssafsound.domain.comment.dto.PostCommentWriteReqDto;
import com.ssafy.ssafsound.domain.member.domain.Member;
import com.ssafy.ssafsound.domain.post.domain.Post;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.ssafy.ssafsound.domain.comment.repository;

import com.ssafy.ssafsound.domain.comment.domain.Comment;
import org.springframework.data.domain.Pageable;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import com.ssafy.ssafsound.domain.comment.domain.Comment;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query("SELECT c FROM comment c " +
"JOIN FETCH c.commentNumber " +
"JOIN FETCH c.member " +
"JOIN FETCH c.commentGroup g " +
"WHERE c.post.id = :postId " +
"ORDER BY g.id ")
List<Comment> findAllPostIdWithDetailsFetchOrderByCommentGroupId(@Param("postId") Long postId);
@Query("SELECT c FROM comment c " +
"JOIN FETCH c.commentNumber " +
"JOIN FETCH c.member " +
"JOIN FETCH c.commentGroup g " +
"WHERE c.post.id = :postId " +
"ORDER BY g.id ")
List<Comment> findAllPostIdWithDetailsFetchOrderByCommentGroupId(@Param("postId") Long postId);

@Modifying
@Query(value = "update comment "
+ "set comment_group = :id "
+ "where comment_id = :id", nativeQuery = true)
void updateByCommentGroup(@Param("id") Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.ssafy.ssafsound.domain.member.exception.MemberErrorInfo;
import com.ssafy.ssafsound.domain.member.exception.MemberException;
import com.ssafy.ssafsound.domain.member.repository.MemberRepository;
import com.ssafy.ssafsound.domain.post.domain.Post;
import com.ssafy.ssafsound.domain.post.dto.PostCommonLikeResDto;
import com.ssafy.ssafsound.domain.post.exception.PostErrorInfo;
import com.ssafy.ssafsound.domain.post.exception.PostException;
Expand Down Expand Up @@ -69,7 +70,7 @@ public CommentIdElement writeComment(Long postId, Long loginMemberId, PostCommen
.build();

comment = commentRepository.save(comment);
comment.setCommentGroup(comment);
commentRepository.updateByCommentGroup(comment.getId());

return new CommentIdElement(comment.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public AuthorElement(Member member, Boolean anonymity, Long number) {
public AuthorElement(Member member, Boolean anonymity) {
this.memberId = anonymity? -1 : member.getId();
this.nickname = anonymity? "익명" : member.getNickname();
this.memberRole = member.getRole().getRoleType();
this.ssafyMember = member.getSsafyMember();
this.isMajor = member.getMajor();
this.ssafyInfo = SSAFYInfo.from(member);
this.memberRole = anonymity? null : member.getRole().getRoleType();
this.ssafyMember = anonymity? null : member.getSsafyMember();
this.isMajor = anonymity? null : member.getMajor();
this.ssafyInfo = anonymity? null : SSAFYInfo.from(member);
}
}
33 changes: 23 additions & 10 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/HotPost.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package com.ssafy.ssafsound.domain.post.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.ssafy.ssafsound.domain.BaseTimeEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity(name="hot_post")
@Entity(name = "hot_post")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class HotPost extends BaseTimeEntity {

@Id
@Column(name = "hot_post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "hot_post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne
@JoinColumn(name = "post_id")
private Post post;

@OneToOne
@JoinColumn(name = "post_id")
private Post post;
public static HotPost from(Post post) {
return HotPost.builder()
.post(post)
.build();
}
}
41 changes: 28 additions & 13 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/PostLike.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
package com.ssafy.ssafsound.domain.post.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.ssafy.ssafsound.domain.BaseTimeEntity;
import com.ssafy.ssafsound.domain.member.domain.Member;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity(name="post_like")
@Entity(name = "post_like")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostLike extends BaseTimeEntity {

@Id
@Column(name = "post_like_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "post_like_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
public static PostLike of(Post post, Member member) {
return PostLike.builder()
.post(post)
.member(member)
.build();
}
}
41 changes: 28 additions & 13 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/PostScrap.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
package com.ssafy.ssafsound.domain.post.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.ssafy.ssafsound.domain.member.domain.Member;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity(name="post_scrap")
@Entity(name = "post_scrap")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostScrap {

@Id
@Column(name = "post_scrap_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "post_scrap_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
public static PostScrap of(Post post, Member member) {
return PostScrap.builder()
.post(post)
.member(member)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.ssafy.ssafsound.domain.post.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.Min;

@Getter
@Setter
@Builder
public class GetPostReqDto {
private Long boardId;
private Long cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface HotPostRepository extends JpaRepository<HotPost, Long>, HotPost
void deleteHotPostsUnderThreshold(@Param("threshold") Long threshold);

Optional<HotPost> findByPostId(Long postId);
Boolean existsByPostId(Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ssafy.ssafsound.domain.post.service;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@Getter
@RequiredArgsConstructor
@ConfigurationProperties(prefix = "spring.constant.post")
@ConstructorBinding
public class PostConstantProvider {
private final Long HOT_POST_LIKES_THRESHOLD;
}
Loading