-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
253 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ spring: | |
properties: | ||
hibernate: | ||
format_sql: true | ||
show_sql: true | ||
--- | ||
# Settings for local | ||
spring: | ||
|
29 changes: 24 additions & 5 deletions
29
...com/umc/DongnaeFriend/domain/account/sharing/controller/accountBookSharingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 32 additions & 6 deletions
38
.../java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingBoardRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ava/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingCommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
15 changes: 15 additions & 0 deletions
15
...in/java/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingImgRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
14 changes: 14 additions & 0 deletions
14
...va/com/umc/DongnaeFriend/domain/account/sharing/repository/SharingSympathyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
11 changes: 9 additions & 2 deletions
11
.../java/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
71 changes: 63 additions & 8 deletions
71
...a/com/umc/DongnaeFriend/domain/account/sharing/service/AccountBookSharingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.