Skip to content

Commit

Permalink
feat: Post 도메인 Service 단위 테스트
Browse files Browse the repository at this point in the history
* refactor: HOT_POST_LIKES_THRESHOLD MagicNumber 등록

* fix: 익명유저일 때 ssafyInfo에 데이터가 들어있는 문제 수정

* fix: GetPostReqDto build추가

* test: report 관련 repository 삭제

* test: 정상적인 BoardId, Cursor, Size가 주어졌다면 게시글 목록 조회가 성공합니다.

* test: 존재하지 않은 boardId가 주어졌다면 게시글 목록 조회 실패하는 테스트 코드 구현

* test: 로그인 시 유효한 postId와 loginMemberId가 주어졌다면 게시글 상세 보기가 성공합니다.

* test: 비 로그인 시 유효한 postId가 주어졌다면 게시글 상세 보기가 성공합니다.

* test: 유효하지 않은 postId가 주어졌다면 게시글 상세보기에 예외를 발생합니다.

* test: 로그인 시 유효하지 않은 loginMemberId가 주어졌다면 게시글 상세보기에 예외를 발생합니다.

* fix: 유효하지 않은 게시글을 좋아요 하면 예외 처리 추가

* fix: Hot게시글이 중복되어 등록되는 버그 수정

* test: 게시글을 좋아요 하지 않았다면 좋아요가 저장됩니다.

* test: 게시글을 이미 좋아요 했다면 좋아요가 취소됩니다.

* test: 좋아요가 특정 개수를 달성했다면 Hot 게시글로 등록됩니다.

* test: 좋아요가 특정 개수를 달성했지만 이미 Hot 게시글이라면 등록되지 않습니다.

* test: 유효하지 않은 postId 또는 loginMemberId가 주어졌다면 게시글 좋아요에 예외를 발생합니다.

* fix: 유효하지 않은 게시글을 스크랩했을 때 예외 처리 추가

* test: 게시글을 스크랩 하지 않았다면 스크랩이 저장됩니다.

* test: 게시글을 이미 스크랩 했다면 스크랩이 취소됩니다.

* test: 유효하지 않은 loginMemberId가 주어졌다면 게시글 스크랩에 예외를 발생합니다.

* test: 유효하지 않은 postId가 주어졌다면 게시글 스크랩에 예외를 발생합니다.

* fix: commentGroup 수정하는 로직 NativeQuery로 수정
  • Loading branch information
jjuny0310 authored Sep 23, 2023
1 parent 22a20cb commit 65ae6ec
Show file tree
Hide file tree
Showing 15 changed files with 910 additions and 343 deletions.
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

0 comments on commit 65ae6ec

Please sign in to comment.