Skip to content

Commit

Permalink
🛠️Reafactor #28: 동네정보 게시글 Paging 처리
Browse files Browse the repository at this point in the history
🛠️Reafactor #28: 동네정보 게시글 Paging 처리
  • Loading branch information
DDonghyeo authored Jul 28, 2023
2 parents 19cd7d2 + c2ff828 commit eb8f73e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,7 +34,7 @@ public interface SharingBoardRepository extends JpaRepository<SharingBoard,Long>
"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<SharingBoard> findByKeywordOrderByLikes(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable);
List<SharingBoard> findByKeyword(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService
@Override
public List<SharingDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable) {
//TODO : 전체 카테고리 처리
List<SharingBoard> sharingBoards = sharingBoardRepository.findByKeywordOrderByLikes(keyword, SharingCategory.valueOf(category).name(), pageable);
List<SharingBoard> sharingBoards = sharingBoardRepository.findByKeyword(keyword, SharingCategory.valueOf(category).name(), pageable);
if (sharingBoards.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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));
}

/*
* [동네정보] 사용자 위치 정보
Expand All @@ -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));
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,14 +15,11 @@
@Repository
public interface DongnaeBoardRepository extends JpaRepository<DongnaeBoard, Long> {

@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<DongnaeBoard> 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<DongnaeBoard> 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<DongnaeBoard> 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<DongnaeBoard> findAllOrderByCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DongnaeBoardDto.ListResponse> searchByKeyword(String keyword, int category, int sort);
List<DongnaeBoardDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable);

List<DongnaeBoardDto.ListResponse> searchAll(int sort);

void createBoard(DongnaeBoardDto.Request req);

List<DongnaeBoardDto.ListResponse> home(int category);

UserLocationDto getUserLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,19 +56,6 @@ public class DongnaeBoardServiceImpl implements DongnaeBoardService {
@Autowired
private DongnaeRepository dongnaeRepository;

/*
* [동네정보] 홈 화면
* 카테고리 별 게시글 2개씩 반환
* @param sort
*/
@Override
public List<DongnaeBoardDto.ListResponse> home(int category) {
String category_String = DongnaeBoardCategory.valueOf(category).name();
//TODO : 동네 인증 여부 확인하기 - (User 필요)
String category_ = "RESTAURANT";
List<DongnaeBoard> dongnaeBoardList = dongnaeBoardRepository.findTwoByCategoryOrderByCreatedAt(category_);
return getListResponses(dongnaeBoardList);
}

/*
* [동네정보] 사용자 위치 정보
Expand All @@ -87,16 +75,10 @@ public UserLocationDto getUserLocation() {

@Override
// @Transactional(propagation = Propagation.REQUIRED)
public List<DongnaeBoardDto.ListResponse> searchByKeyword(String keyword, int category, int sort) {
public List<DongnaeBoardDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable) {
String categoryName = DongnaeBoardCategory.valueOf(category).name();


List<DongnaeBoard> dongnaeBoardList;
if (sort == 0) {
dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByCreatedAt(keyword, categoryName);
} else {
dongnaeBoardList = dongnaeBoardRepository.findByKeywordOrderByLikes(keyword, categoryName);
}
List<DongnaeBoard> dongnaeBoardList = dongnaeBoardRepository.findByKeyword(keyword, categoryName, pageable);

return getListResponses(dongnaeBoardList);
}
Expand Down Expand Up @@ -134,6 +116,7 @@ public void createBoard(DongnaeBoardDto.Request req) {
}



/*
* [동네정보] 게시글 상세 조회
*/
Expand Down

0 comments on commit eb8f73e

Please sign in to comment.