Skip to content

Commit

Permalink
🛠️Fix #25: [가계부 공유] 게시글 검색
Browse files Browse the repository at this point in the history
  • Loading branch information
DDonghyeo committed Jul 23, 2023
1 parent ccc02cc commit e6f3a92
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 58 deletions.
1 change: 1 addition & 0 deletions out/production/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ spring:
properties:
hibernate:
format_sql: true
show_sql: true
---
# Settings for local
spring:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package com.umc.DongnaeFriend.domain.account.book.controller;
package com.umc.DongnaeFriend.domain.account.sharing.controller;

import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto;
import com.umc.DongnaeFriend.domain.account.sharing.service.AccountBookSharingService;
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.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/account-books/sharing")
public class accountBookSharingController {

@Autowired
AccountBookSharingService accountBookSharingService;

/**
* [가계부 공유] 게시글 검색
* @param keyword
* @param pageable
*
* <pageable>
* @param pageable
* size : 페이지 사이즈
* page : 페이지
* sortBy : 정렬순
* -
* - like
* - createdAt
*/

@GetMapping("/search")
public ResponseEntity<?> searchAll(@RequestParam("keyword")String keyword, @RequestParam("category") int category, Pageable pageable) {


public ResponseEntity<?> searchAll(@RequestParam("keyword") String keyword, @RequestParam("category") int category, Pageable pageable) {
log.info("searching : " + keyword + category);
List<SharingDto.ListResponse> res = accountBookSharingService.searchByKeyword(keyword, category, pageable);
log.info("res ");
return ResponseEntity.ok(res);
}





}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.umc.DongnaeFriend.domain.account.book.dto;
package com.umc.DongnaeFriend.domain.account.sharing.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -12,7 +12,7 @@ public class SharingDto {
@AllArgsConstructor
@NoArgsConstructor
public static class ListResponse {
private String id;
private Long id;

private int category;

Expand All @@ -29,6 +29,9 @@ public static class ListResponse {
private int commentCount;

private int likes;

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.umc.DongnaeFriend.domain.user.entity.User;
import javax.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;

@Getter
@Builder
Expand All @@ -38,4 +39,8 @@ public class SharingBoard extends BaseTimeEntity {

@Column(columnDefinition = "MEDIUMTEXT", nullable = false)
private String content;

@Column(name = "view")
@ColumnDefault("0")
private Integer view;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
package com.umc.DongnaeFriend.domain.account.book.repository;
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;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import java.util.List;

public interface SharingBoardRepository extends JpaRepository <SharingBoard,Long> {
@Repository
public interface SharingBoardRepository extends JpaRepository<SharingBoard,Long> {

@Query(value = "SELECT sharing_board.*, COUNT(sharing_sympathy.sharing_board_id) AS like FROM sharing_board\n" +

// private final EntityManager em;
//
// public SharingBoardRepository(EntityManager em) {
// this.em = em;
// }

// public List<SharingDto.ListResponse> findByKeywordAAndCategory(@Param("keyword") String keyword, @Param("category") String category, Pageable pageable) {
// return em.createQuery("SELECT new com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto.ListResponse(s.id, s.category, s.title, s.content, s.createdAt, s.view, likes), count(SharingSympathy.sharingBoard.id) as likes " +
// "FROM SharingBoard s left join SharingSympathy on s.id=SharingSympathy.sharingBoard.id " +
// "WHERE (s.title LIKE %:keyword% or s.content LIKE % :keyword%) and s.category = :category " +
// "group by s.id", SharingDto.ListResponse.class)
// .setParameter("keyword", keyword)
// .setParameter("category", category)
// .getResultList();
// }


@Query(value = "SELECT sharing_board.*, COUNT(sharing_sympathy.sharing_board_id) AS cnt FROM sharing_board\n" +
"LEFT JOIN sharing_sympathy ON sharing_board.sharing_board_id = sharing_sympathy.sharing_board_id\n" +
"WHERE (sharing_board.title LIKE %?1% OR sharing_board.content LIKE %?2%)\n" +
"AND sharing_board.category = ?2 GROUP BY sharing_board.sharing_board_id ;", nativeQuery = true)
List<SharingBoard> findByKeywordAAndCategory(String keyword, String category, Pageable pageable);
"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);




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SharingCommentRepository extends JpaRepository<SharingComment, Long> {

public int countAllBySharingBoardId(Long sharing_board_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingImg;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface SharingImgRepository extends JpaRepository<SharingImg, Long> {

@Query(value = "SELECT * FROM sharing_img WHERE sharing_board_id = ?1 ORDER BY created_at ASC LIMIT 1", nativeQuery = true)
Optional<SharingImg> findFirst(long sharing_board_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeSympathy;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface SharingSympathyRepository extends JpaRepository<SharingSympathy, Long> {

int countAllBySharingBoardId(Long sharing_board_id);

List<SharingSympathy> findByUser_Id(long user_id);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.umc.DongnaeFriend.domain.account.book.service;
package com.umc.DongnaeFriend.domain.account.sharing.service;

public interface accountBookSharingService {
import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface AccountBookSharingService {

List<SharingDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,92 @@
package com.umc.DongnaeFriend.domain.account.book.service;
package com.umc.DongnaeFriend.domain.account.sharing.service;

import com.umc.DongnaeFriend.domain.account.book.dto.SharingDto;
import com.umc.DongnaeFriend.domain.account.book.repository.SharingBoardRepository;
import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingImg;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingBoardRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingImgRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingSympathyRepository;
import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae;
import com.umc.DongnaeFriend.domain.type.Age;
import com.umc.DongnaeFriend.domain.type.Gender;
import com.umc.DongnaeFriend.domain.type.SharingCategory;
import com.umc.DongnaeFriend.domain.type.YesNo;
import com.umc.DongnaeFriend.domain.user.entity.User;
import com.umc.DongnaeFriend.global.exception.CustomException;
import com.umc.DongnaeFriend.global.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime;

@Slf4j
@Service
public class accountBookSharingServiceImpl implements accountBookSharingService {
public class AccountBookSharingServiceImpl implements AccountBookSharingService {


//임시 유저
Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build();
User user = User.builder().id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build();


@Autowired
private SharingBoardRepository sharingBoardRepository;

@Autowired
private SharingImgRepository sharingImgRepository;

@Autowired
private SharingCommentRepository sharingCommentRepository;

@Autowired
private SharingSympathyRepository sharingSympathyRepository;


/*
* [가계부 공유] 게시글
* [가계부 공유] 게시글 검색
*/
@Override
public List<SharingDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable) {
sharingBoardRepository.findByKeywordAAndCategory(keyword, SharingCategory.valueOf(category).name(), pageable);
//TODO : 전체 카테고리 처리
List<SharingBoard> sharingBoards = sharingBoardRepository.findByKeywordOrderByLikes(keyword, SharingCategory.valueOf(category).name(), pageable);
if (sharingBoards.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}
log.info("board found" + sharingBoards.get(0).getId());
return getListResponses(sharingBoards);
}


//ListResponse 변환
private List<SharingDto.ListResponse> getListResponses(List<SharingBoard> sharingBoardList) {
return sharingBoardList.stream()
.map(origin -> SharingDto.ListResponse.builder()
.id(origin.getId())
.category(origin.getCategory().getValue())
.title(origin.getTitle())
.content(origin.getContent())
.imageUrl(
sharingImgRepository.findFirst(origin.getId()).map(SharingImg::getImageUrl).orElse("")
)
.createdAt(
getTime(origin.getCreatedAt())
)
.view(origin.getView())
.commentCount(
sharingCommentRepository.countAllBySharingBoardId(origin.getId())
)
.likes(
sharingSympathyRepository.countAllBySharingBoardId(origin.getId())
)
.build())
.collect(Collectors.toList());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import java.util.List;

import static com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory.*;


public class DongnaeBoardDto {

Expand Down Expand Up @@ -55,6 +53,7 @@ public DongnaeBoard toEntity(User user, Dongnae dongnae) {
@AllArgsConstructor
@NoArgsConstructor
public static class ListResponse {
private Long id;
private String town;

private int category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.umc.DongnaeFriend.domain.BaseTimeEntity;
import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeBoardDto;
import com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory;
Expand All @@ -15,8 +14,6 @@
import lombok.*;
import org.hibernate.annotations.ColumnDefault;

import java.util.List;

@Getter
@Builder
@NoArgsConstructor(access = PROTECTED)
Expand Down
Loading

0 comments on commit e6f3a92

Please sign in to comment.