Skip to content

Commit

Permalink
Merge pull request #30 from Domitory-CheckMate/feature/29-post
Browse files Browse the repository at this point in the history
[refactor] 게시물 service 코드 정리
  • Loading branch information
RyuKwanKon authored Jan 7, 2024
2 parents 6576523 + 9a06f3d commit 604edb9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.PostDetailResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
import org.gachon.checkmate.domain.post.dto.support.PostSearchCondition;
import org.gachon.checkmate.domain.post.repository.PostQuerydslRepository;
import org.gachon.checkmate.domain.post.service.PostService;
import org.gachon.checkmate.global.common.SuccessResponse;
import org.gachon.checkmate.global.config.auth.UserId;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
Expand All @@ -23,10 +20,11 @@ public class PostController {

@GetMapping
public ResponseEntity<SuccessResponse<?>> getAllPosts(@UserId final Long userId,
@RequestParam(required = false) final String key,
@RequestParam final String type,
@RequestParam(required = false) final String gender,
final Pageable pageable) {
final PostSearchResponseDto responseDto = postService.getAllPosts(userId, type, gender, pageable);
final PostSearchResponseDto responseDto = postService.getAllPosts(userId, key, type, gender, pageable);
return SuccessResponse.ok(responseDto);
}

Expand All @@ -44,16 +42,6 @@ public ResponseEntity<SuccessResponse<?>> searchTextPost(@UserId final Long user
return SuccessResponse.ok(responseDto);
}

@GetMapping("/search/{key}")
public ResponseEntity<SuccessResponse<?>> searchKeyWordPost(@UserId final Long userId,
@PathVariable final String key,
@RequestParam final String type,
@RequestParam(required = false) final String gender,
final Pageable pageable) {
final PostSearchResponseDto responseDto = postService.searchKeyWordPost(userId, key, type, gender, pageable);
return SuccessResponse.ok(responseDto);
}

@PostMapping
public ResponseEntity<SuccessResponse<?>> createPost(@UserId final Long userId,
@RequestBody @Valid final PostCreateRequestDto requestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gachon.checkmate.domain.post.repository;

import org.gachon.checkmate.domain.post.dto.support.PostDetailDto;
import org.gachon.checkmate.domain.post.dto.support.PostSearchCondition;
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface PostCustomRepository {
Optional<PostDetailDto> findPostDetail(Long postId);
Page<PostSearchDto> searchPosts(PostSearchCondition condition);
Page<PostSearchDto> searchTextPost(String text, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -23,10 +22,10 @@
import static org.springframework.util.StringUtils.hasText;

@RequiredArgsConstructor
@Repository
public class PostQuerydslRepository {
public class PostCustomRepositoryImpl implements PostCustomRepository {
private final JPAQueryFactory queryFactory;

@Override
public Optional<PostDetailDto> findPostDetail(Long postId) {
return Optional.ofNullable(queryFactory
.select(new QPostDetailDto(
Expand All @@ -46,6 +45,7 @@ public Optional<PostDetailDto> findPostDetail(Long postId) {
.fetchOne());
}

@Override
public Page<PostSearchDto> searchPosts(PostSearchCondition condition) {
List<PostSearchDto> content = queryFactory
.select(new QPostSearchDto(
Expand Down Expand Up @@ -74,6 +74,7 @@ public Page<PostSearchDto> searchPosts(PostSearchCondition condition) {
return PageableExecutionUtils.getPage(content, condition.pageable(), countQuery::fetchCount);
}

@Override
public Page<PostSearchDto> searchTextPost(String text, Pageable pageable) {
List<PostSearchDto> content = queryFactory
.select(new QPostSearchDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {
public interface PostRepository extends JpaRepository<Post, Long>, PostCustomRepository {
boolean existsByTitle(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.gachon.checkmate.domain.post.dto.support.PostSearchDto;
import org.gachon.checkmate.domain.post.entity.Post;
import org.gachon.checkmate.domain.post.entity.SortType;
import org.gachon.checkmate.domain.post.repository.PostQuerydslRepository;
import org.gachon.checkmate.domain.post.repository.PostRepository;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.InvalidValueException;
Expand All @@ -44,19 +43,18 @@ public class PostService {
private final UserRepository userRepository;
private final CheckListRepository checkListRepository;
private final PostRepository postRepository;
private final PostQuerydslRepository postQuerydslRepository;
private final PostCheckListRepository postCheckListRepository;

public void createPost(Long userId, PostCreateRequestDto requestDto) {
validateDuplicateTitle(requestDto.title());
User user = getUserOrThrow(userId);
Post post = createPostAndSave(requestDto, user);
PostCheckList postCheckList = createPostCheckListAndSave(requestDto.checkList(), post);
createPostCheckListAndSave(requestDto.checkList(), post);
}

public PostSearchResponseDto getAllPosts(Long userId, String type, String gender, Pageable pageable) {
public PostSearchResponseDto getAllPosts(Long userId, String key, String type, String gender, Pageable pageable) {
CheckList checkList = getCheckList(userId);
PostSearchCondition condition = PostSearchCondition.of(type, null, gender, pageable);
PostSearchCondition condition = PostSearchCondition.of(type, key, gender, pageable);
Page<PostSearchDto> postSearchList = getSearchResults(condition);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
sortByTypeForSearchResults(searchResults, condition.sortType());
Expand All @@ -71,17 +69,6 @@ public PostDetailResponseDto getPostDetails(Long postId) {
return PostDetailResponseDto.of(postDetailDto, checkListResponseDto);
}

public PostSearchResponseDto searchKeyWordPost(Long userId, String key, String type, String gender, Pageable pageable) {
CheckList checkList = getCheckList(userId);
PostSearchCondition condition = PostSearchCondition.of(type, key, gender, pageable);
Page<PostSearchDto> postSearchList = getSearchResults(condition);
List<PostSearchElementResponseDto> searchResults = createPostSearchResponseDto(postSearchList, checkList);
sortByTypeForSearchResults(searchResults, condition.sortType());
List<PostSearchElementResponseDto> pagingSearchResults
= convertPaging(searchResults, pageable.getOffset(), pageable.getPageSize());
return PostSearchResponseDto.of(pagingSearchResults, postSearchList.getTotalPages(), postSearchList.getTotalElements());
}

public PostSearchResponseDto searchTextPost(Long userId, String text, Pageable pageable) {
CheckList checkList = getCheckList(userId);
Page<PostSearchDto> postSearchList = getTextSearchResults(text, pageable);
Expand Down Expand Up @@ -162,11 +149,11 @@ private PostCheckList createPostCheckListAndSave(CheckListRequestDto checkListRe
}

private Page<PostSearchDto> getSearchResults(PostSearchCondition condition) {
return postQuerydslRepository.searchPosts(condition);
return postRepository.searchPosts(condition);
}

private Page<PostSearchDto> getTextSearchResults(String text, Pageable pageable) {
return postQuerydslRepository.searchTextPost(text, pageable);
return postRepository.searchTextPost(text, pageable);
}

private CheckList getCheckList(Long userId) {
Expand All @@ -175,7 +162,7 @@ private CheckList getCheckList(Long userId) {
}

private PostDetailDto getPostDetailDto(Long postId) {
return postQuerydslRepository.findPostDetail(postId)
return postRepository.findPostDetail(postId)
.orElseThrow(() -> new EntityNotFoundException(POST_NOT_FOUND));
}

Expand Down

0 comments on commit 604edb9

Please sign in to comment.