From c2ff8282343d82b925db217ee7d4635d50a3a982 Mon Sep 17 00:00:00 2001 From: DDonghyeo Date: Fri, 28 Jul 2023 21:43:54 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8FReafactor=20#28:=20?= =?UTF-8?q?=EB=8F=99=EB=84=A4=EC=A0=95=EB=B3=B4=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20Paging=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/SharingBoardRepository.java | 4 +-- .../AccountBookSharingServiceImpl.java | 2 +- .../controller/DongnaeBoardController.java | 14 +++-------- .../respository/DongnaeBoardRepository.java | 13 +++++----- .../dongnae/service/DongnaeBoardService.java | 4 +-- .../service/DongnaeBoardServiceImpl.java | 25 +++---------------- 6 files changed, 17 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java index 54bc584..f99d1f3 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java @@ -1,8 +1,6 @@ package com.umc.DongnaeFriend.domain.account.sharing.repository; -import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto; import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; -import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -36,7 +34,7 @@ public interface SharingBoardRepository extends JpaRepository "LEFT JOIN sharing_sympathy ON sharing_board.sharing_board_id = sharing_sympathy.sharing_board_id\n" + "WHERE (sharing_board.title LIKE %:keyword% OR sharing_board.content LIKE %:keyword%)\n" + "AND sharing_board.category = :category GROUP BY sharing_board.sharing_board_id ", nativeQuery = true) - List findByKeywordOrderByLikes(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); + List findByKeyword(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); diff --git a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java index 6687692..e741567 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java @@ -55,7 +55,7 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService @Override public List searchByKeyword(String keyword, int category, Pageable pageable) { //TODO : 전체 카테고리 처리 - List sharingBoards = sharingBoardRepository.findByKeywordOrderByLikes(keyword, SharingCategory.valueOf(category).name(), pageable); + List sharingBoards = sharingBoardRepository.findByKeyword(keyword, SharingCategory.valueOf(category).name(), pageable); if (sharingBoards.isEmpty()) { throw new CustomException(ErrorCode.NO_CONTENT_FOUND); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java index 51c4fe2..ec1ea11 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeBoardController.java @@ -5,6 +5,7 @@ import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeBoardService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; @@ -26,15 +27,6 @@ public class DongnaeBoardController { @Autowired DongnaeBoardService dongnaeBoardService; - /* - * [동네정보] 홈 화면 - */ - //TODO : 파라미터 다시 확인해보기 - @GetMapping("/home") - public ResponseEntity home(@RequestParam("category") int category, - @RequestParam("sortBy") int sort) { - return ResponseEntity.ok(dongnaeBoardService.home(category)); - } /* * [동네정보] 사용자 위치 정보 @@ -54,11 +46,11 @@ public ResponseEntity getLocation() { @GetMapping("/search") public ResponseEntity getBoards(@RequestParam("keyword") String keyword, @RequestParam("category") int category, - @RequestParam("sortBy") int sort) { + Pageable pageable) { log.info("User Id: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal()); - return ResponseEntity.ok(dongnaeBoardService.searchByKeyword(keyword, category, sort)); + return ResponseEntity.ok(dongnaeBoardService.searchByKeyword(keyword, category, pageable)); } /* diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java index eaded3e..54cd3d4 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeBoardRepository.java @@ -1,6 +1,8 @@ package com.umc.DongnaeFriend.domain.dongnae.respository; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -13,14 +15,11 @@ @Repository public interface DongnaeBoardRepository extends JpaRepository { - @Query(value = "select * from dongnae_board where title like %?1% or content like %?1% and category = ?2 ORDER BY created_at DESC;", nativeQuery = true) - List findByKeywordOrderByCreatedAt(String keyword, String category); - - @Query(value = "SELECT dongnae_board.*, COUNT(dongnae_sympathy.dongnae_board_id) AS cnt FROM dongnae_board\n" + + @Query(value = "SELECT dongnae_board.*, COUNT(dongnae_sympathy.dongnae_board_id) AS likes FROM dongnae_board\n" + "LEFT JOIN dongnae_sympathy ON dongnae_board.dongnae_board_id = dongnae_sympathy.dongnae_board_id\n" + - "WHERE (dongnae_board.title LIKE %?1% OR dongnae_board.content LIKE %?2%)\n" + - "AND dongnae_board.category = ?2 GROUP BY dongnae_board.dongnae_board_id ORDER BY cnt DESC ;", nativeQuery = true) - List findByKeywordOrderByLikes(String keyword, String category); + "WHERE (dongnae_board.title LIKE %:keyword% OR dongnae_board.content LIKE %:keyword%)\n" + + "AND dongnae_board.category = :category GROUP BY dongnae_board.dongnae_board_id ", nativeQuery = true) + List findByKeyword(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable); @Query(value = "select * from dongnae_board ORDER BY created_at DESC;", nativeQuery = true) List findAllOrderByCreatedAt(); diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java index 8f1c978..02c6e4d 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardService.java @@ -2,19 +2,19 @@ import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeBoardDto; import com.umc.DongnaeFriend.domain.dongnae.dto.UserLocationDto; +import org.springframework.data.domain.Pageable; import javax.naming.AuthenticationException; import java.util.List; public interface DongnaeBoardService { - List searchByKeyword(String keyword, int category, int sort); + List searchByKeyword(String keyword, int category, Pageable pageable); List searchAll(int sort); void createBoard(DongnaeBoardDto.Request req); - List home(int category); UserLocationDto getUserLocation(); diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java index 5d127bc..a0554b0 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeBoardServiceImpl.java @@ -14,6 +14,7 @@ import com.umc.DongnaeFriend.domain.user.repository.UserRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -55,19 +56,6 @@ public class DongnaeBoardServiceImpl implements DongnaeBoardService { @Autowired private DongnaeRepository dongnaeRepository; - /* - * [동네정보] 홈 화면 - * 카테고리 별 게시글 2개씩 반환 - * @param sort - */ - @Override - public List home(int category) { - String category_String = DongnaeBoardCategory.valueOf(category).name(); - //TODO : 동네 인증 여부 확인하기 - (User 필요) - String category_ = "RESTAURANT"; - List dongnaeBoardList = dongnaeBoardRepository.findTwoByCategoryOrderByCreatedAt(category_); - return getListResponses(dongnaeBoardList); - } /* * [동네정보] 사용자 위치 정보 @@ -87,16 +75,10 @@ public UserLocationDto getUserLocation() { @Override // @Transactional(propagation = Propagation.REQUIRED) - public List searchByKeyword(String keyword, int category, int sort) { + public List searchByKeyword(String keyword, int category, Pageable pageable) { String categoryName = DongnaeBoardCategory.valueOf(category).name(); - - List dongnaeBoardList; - if (sort == 0) { - dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByCreatedAt(keyword, categoryName); - } else { - dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByLikes(keyword, categoryName); - } + List dongnaeBoardList = dongnaeBoardRepository.findByKeyword(keyword, categoryName, pageable); return getListResponses(dongnaeBoardList); } @@ -134,6 +116,7 @@ public void createBoard(DongnaeBoardDto.Request req) { } + /* * [동네정보] 게시글 상세 조회 */