Skip to content

Commit

Permalink
게시글 조회 시 마감 여부 필터링 로직 추가 (#320)
Browse files Browse the repository at this point in the history
* feat: 게시글 조회 시 마감 여부 필터링 로직 추가

* refactor: 불필요한 필드 및 메서드 제거
  • Loading branch information
Hanjaemo authored Apr 28, 2024
1 parent dc9cda1 commit 3bd5a03
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
26 changes: 17 additions & 9 deletions src/main/java/balancetalk/module/post/application/PostService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package balancetalk.module.post.application;

import static balancetalk.global.exception.ErrorCode.*;
import static balancetalk.global.utils.SecurityUtils.getCurrentMember;

import balancetalk.global.exception.BalanceTalkException;
import balancetalk.global.redis.application.RedisService;
import balancetalk.module.bookmark.domain.BookmarkRepository;
Expand All @@ -19,10 +16,7 @@
import balancetalk.module.report.domain.Report;
import balancetalk.module.report.domain.ReportRepository;
import balancetalk.module.report.dto.ReportRequest;
import balancetalk.module.post.dto.*;
import balancetalk.module.vote.domain.VoteRepository;
import java.util.stream.Collectors;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -31,6 +25,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static balancetalk.global.exception.ErrorCode.*;
import static balancetalk.global.utils.SecurityUtils.getCurrentMember;

@Slf4j
@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -77,8 +77,8 @@ private List<File> getImages(PostRequest postRequest) {
}

@Transactional(readOnly = true)
public Page<PostResponse> findAll(String token, Pageable pageable) {
Page<Post> posts = postRepository.findAll(pageable);
public Page<PostResponse> findAll(String token, Boolean containsClosed, Pageable pageable) {
Page<Post> posts = getPosts(containsClosed, pageable);

if (token == null) {
return posts.map(post -> PostResponse.fromEntity(post, null, false, false, false));
Expand All @@ -92,6 +92,13 @@ public Page<PostResponse> findAll(String token, Pageable pageable) {
member.hasVoted(post)));
}

private Page<Post> getPosts(Boolean containsClosed, Pageable pageable) {
if (containsClosed) {
return postRepository.findAll(pageable);
}
return postRepository.findAllOnlyOpened(pageable);
}

@Transactional
public PostResponse findById(Long postId, String token) {
Post post = getCurrentPost(postId);
Expand All @@ -104,7 +111,7 @@ public PostResponse findById(Long postId, String token) {
Member member = getCurrentMember(memberRepository);

if (member.getRole() == Role.USER) {
post.increaseViews();
post.increaseViews();
}
return PostResponse.fromEntity(post, member, member.hasLiked(post), member.hasBookmarked(post),
member.hasVoted(post));
Expand Down Expand Up @@ -174,6 +181,7 @@ public void cancelLikePost(Long postId) {
private boolean notExistsPostLikeBy(Member member, Post post) {
return !postLikeRepository.existsByMemberAndPost(member, post);
}

private Post getCurrentPost(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new BalanceTalkException(NOT_FOUND_POST));
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/balancetalk/module/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,4 @@ public boolean hasDeadlineExpired() {
public void increaseViews() {
views++;
}

public boolean isClosed() {
return LocalDateTime.now().isAfter(deadline);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package balancetalk.module.post.domain;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findAllByMemberId(Long id, Pageable pageable);

Expand All @@ -25,4 +26,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
"JOIN pt.tag t " +
"WHERE t.name = :tagName")
List<Post> findByPostTagsContaining(String tagName);

@Query("select p from Post p where p.deadline > function('now')")
Page<Post> findAllOnlyOpened(Pageable pageable);
}
4 changes: 0 additions & 4 deletions src/main/java/balancetalk/module/post/dto/PostResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public class PostResponse {
@Schema(description = "투료 종료 기한", example = "2024/12/25 15:30:00")
private LocalDateTime deadline;

@Schema(description = "마감 여부", example = "true")
private Boolean isClosed;

@Schema(description = "게시글 조회수", example = "126")
private long views;

Expand Down Expand Up @@ -91,7 +88,6 @@ public static PostResponse fromEntity(Post post, Member member, boolean myLike,
.id(post.getId())
.title(post.getTitle())
.deadline(post.getDeadline())
.isClosed(post.isClosed())
.views(post.getViews())
// .viewStatus(post.getViewStatus())
.likesCount(post.likesCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import balancetalk.module.report.dto.ReportRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -15,6 +14,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
Expand All @@ -26,7 +27,7 @@ public class PostController {

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
@Operation(summary = "게시글 생성" , description = "로그인 상태인 회원이 게시글을 작성한다.")
@Operation(summary = "게시글 생성", description = "로그인 상태인 회원이 게시글을 작성한다.")
public PostResponse createPost(@Valid @RequestBody final PostRequest postRequestDto) {
return postService.save(postRequestDto);
}
Expand All @@ -35,8 +36,9 @@ public PostResponse createPost(@Valid @RequestBody final PostRequest postRequest
@GetMapping
@Operation(summary = "모든 게시글 조회", description = "해당 회원이 쓴 모든 글을 조회한다.")
public Page<PostResponse> findAllPosts(@RequestHeader(value = "Authorization", required = false) String token,
@RequestParam Boolean closed,
Pageable pageable) {
return postService.findAll(token, pageable);
return postService.findAll(token, closed, pageable);
}

@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -66,7 +68,7 @@ public List<PostResponse> findPostsByTitle(@RequestHeader(value = "Authorization
@GetMapping("/tag")
@Operation(summary = "게시글 태그 검색 기능", description = "태그에 맞는 모든 게시글을 조회한다.")
public List<PostResponse> findPostsByTag(@RequestHeader(value = "Authorization", required = false) String token,
@RequestParam String tagName) {
@RequestParam String tagName) {
return postService.findPostsByTag(token, tagName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PostServiceTest {

@InjectMocks
PostService postService;

Member member = Member.builder()
.id(1L)
.email("[email protected]")
Expand Down

0 comments on commit 3bd5a03

Please sign in to comment.