Skip to content

Commit

Permalink
[Merge] 2024-06-05 버전 적용 (#207)
Browse files Browse the repository at this point in the history
* [Refactor] 책 조회시 처방전 개수 및 인기순으로 조회기능 추가 (#205)

* [Refactor] 처방전 개수, 인기순으로 책 조회할 수 있도록 수정

* [Refactor] 한줄처방 및 고민 작성시 Count 개수 관리되도록 수정

* [Refactor] BoardController UserDetail이 아닌 AuthenticationAdapter를 받도록 수정

---------

Signed-off-by: 이성훈 <[email protected]>
Signed-off-by: simjaeyoun <[email protected]>
Co-authored-by: simjaeyoun <[email protected]>
  • Loading branch information
seonghooni and Simy2 authored Jun 5, 2024
1 parent 7d71dc7 commit 1a41a7f
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public ResponseEntity<String> createBoard(@AuthenticationPrincipal Authenticatio

@DeleteMapping("/{boardId}")
@Operation(summary = "게시판 삭제")
public ResponseEntity<String> deleteBoard(@PathVariable("boardId") Long boardId){
boardService.deleteBoard(boardId);
public ResponseEntity<String> deleteBoard(@PathVariable("boardId") Long boardId, @AuthenticationPrincipal AuthenticationAdapter userDetails){
boardService.deleteBoard(boardId, userDetails);
return ResponseEntity.ok("success");
}
@GetMapping("/my")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Long createBoard(BoardCreateDto boardCreateDto, AuthenticationAdapter aut
Board board = boardCreateDto.toEntity(client);
Long id = boardRepository.save(board).getId();
answerService.createAnswer(id,boardCreateDto.getAnswers());
client.plusBoardCount();

applicationEventPublisher.publishEvent(new BoardUpdatedEvent(this,id));

Expand All @@ -100,8 +101,9 @@ private Client getClient(AuthenticationAdapter authenticationAdapter){
}

@Transactional
public void deleteBoard(Long boardId){

public void deleteBoard(Long boardId, AuthenticationAdapter authenticationAdapter){
Client client = getClient(authenticationAdapter);
client.minusBoardCount();
boardRepository.deleteById(boardId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,39 @@ public ResponseEntity<List<BookSearchResponseDto>> getBookListByAuthorOnModal(@R
}

@Operation(summary = "[페이지]책제목에 검색어를 포함하는 book 리스트 ?개 요청 <파라미터에서 target을 page로 지정해야함>" +
" 예시) /api/search/book?title=그림&target=page&page=0&size=20")
" 예시) /api/search/book?title=그림&target=page&sort=oneLineCount&page=0&size=20")
@GetMapping(params = {"title", "target=page"})
public ResponseEntity<Page<BookDto>> getBookListByTitleOnPage(@RequestParam(name = "title") String searchWord,
public ResponseEntity<Page<BookDto>> getBookListByTitleOnPageOrderByCount(@RequestParam(name = "title") String searchWord,
@RequestParam(name = "sort") String sortType,
@RequestParam(name = "page") int page, @RequestParam(name = "size") int size){

PageRequest pageRequest = PageRequest.of(page, size, Sort.by("count").descending());
PageRequest pageRequest = PageRequest.of(page, size);

if(sortType.equals("oneline-count")){
pageRequest = PageRequest.of(page, size, Sort.by("oneLineCount").descending());
}else if(sortType.equals("view-count")){
pageRequest = PageRequest.of(page, size, Sort.by("viewCount").descending());
}

Page<BookDto> bookDtoList = bookSearchService.searchBookOnPageByTitleContainingSearchWord(searchWord, pageRequest);

return ResponseEntity.ok(bookDtoList);
}

@Operation(summary = "[페이지]작가명에 검색어를 포함하는 book 리스트 ?개 요청 <파라미터에서 target을 page로 지정해야함>" +
" 예시) /api/search/book?author=남해운&target=page&page=0&size=20")
" 예시) /api/search/book?author=남해운&target=page&sort=oneLineCount&page=0&size=20")
@GetMapping(params = {"author", "target=page"})
public ResponseEntity<Page<BookDto>> getBookListByAuthorOnPage(@RequestParam(name = "author") String searchWord,
@RequestParam(name = "sort") String sortType,
@RequestParam(name = "page") int page, @RequestParam(name = "size") int size){

PageRequest pageRequest = PageRequest.of(page, size, Sort.by("count").descending());
PageRequest pageRequest = PageRequest.of(page, size);

if(sortType.equals("oneline-count")){
pageRequest = PageRequest.of(page, size, Sort.by("oneLineCount").descending());
}else if(sortType.equals("view-count")){
pageRequest = PageRequest.of(page, size, Sort.by("viewCount").descending());
}

Page<BookDto> bookDtoList = bookSearchService.searchBookOnPageByAuthorContainingSearchWord(searchWord, pageRequest);

Expand All @@ -87,10 +101,18 @@ public ResponseEntity<Page<BookDto>> getBookListByKeyword(@RequestParam(name = "
@Operation(summary = "검색어와 함께 키워드를 조회할때 사용함")
@PostMapping()
public ResponseEntity<Page<BookDto>> getBookListBySearchAndKeyword(@RequestBody BookSearchRequestDto dto,
@RequestParam(name = "page") int page, @RequestParam(name = "size") int size) {
@RequestParam(name = "sort") String sortType,
@RequestParam(name = "page") int page,
@RequestParam(name = "size") int size) {

PageRequest pageRequest = PageRequest.of(page, size);

if(sortType.equals("oneline-count")){
pageRequest = PageRequest.of(page, size, Sort.by("oneLineCount").descending());
}else if(sortType.equals("view-count")){
pageRequest = PageRequest.of(page, size, Sort.by("viewCount").descending());
}

List<String> keywordList = dto.getKeywordList();
String searchWord = dto.getSearchWord();
Page<BookDto> bookDtoList = bookSearchService.searchBookBySearchAndKeyword(searchWord, keywordList, pageRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"isbn", "title", "author", "bigCategory", "middleCategory", "imageUrl", "count"})
@Table(indexes = {
Expand All @@ -33,7 +32,8 @@ public class Book {
private int reviewNum; // 리뷰 개수
private String mediaFlagNumber; // 미디어구분명
private String imageUrl; // 이미지 url
private int count; // 조회된 횟수
private int viewCount; // 조회된 횟수
private int oneLineCount; // 한줄처방 개수

@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
private List<BookKeyword> bookKeywords = new ArrayList<>();
Expand All @@ -49,7 +49,7 @@ public class Book {

@Builder
public Book(String isbn, String title, String author, String publishingHouse, String publishYear, String content,
int reviewNum, Categories bigCategory, Categories middleCategory, String imageUrl, int count) {
int reviewNum, Categories bigCategory, Categories middleCategory, String imageUrl, int viewCount, int oneLineCount) {
this.isbn = isbn;
this.title = title;
this.author = author;
Expand All @@ -60,6 +60,23 @@ public Book(String isbn, String title, String author, String publishingHouse, St
this.bigCategory = bigCategory;
this.middleCategory = middleCategory;
this.imageUrl = imageUrl;
this.count = count;
this.viewCount = viewCount;
this.oneLineCount = oneLineCount;
}

public void plusViewCount() {
this.viewCount += 1;
}

public void plusOneLineCount() {
this.oneLineCount += 1;
}

public void minusOneLineCount() {
if(this.oneLineCount > 0) {
this.oneLineCount -= 1;
} else{
this.oneLineCount = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class BookDto {
private String publicYear; // 발행년도
private String content; // 책내용
private String mediaFlagNumber; // 미디어구분명
private String bigCategoryName; // 대분류명
private String middleCategoryName; // 중분류명
private String imageUrl; // 이미지 URL
private List<KeywordItemDto> keywordItemList; // 키워드 리스트
Expand All @@ -35,22 +34,20 @@ public BookDto(Book book) {
this.publicYear = book.getPublishYear();
this.content = book.getContent();
this.mediaFlagNumber = book.getMediaFlagNumber();
this.bigCategoryName = book.getBigCategory().getName();
this.middleCategoryName = book.getMiddleCategory().getName();
this.imageUrl = book.getImageUrl();
}

@Builder
public BookDto(String isbn, String title, String author, String publishingHouse, String publicYear, String content, String mediaFlagNumber,
String bigCategoryName, String middleCategoryName, String imageUrl) {
String middleCategoryName, String imageUrl) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.publishingHouse = publishingHouse;
this.publicYear = publicYear;
this.content = content;
this.mediaFlagNumber = mediaFlagNumber;
this.bigCategoryName = bigCategoryName;
this.middleCategoryName = middleCategoryName;
this.imageUrl = imageUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,36 @@ public interface BookRepository extends JpaRepository<Book, Long> {
Page<BookSearchResponseDto> findDtoBook10ListByMiddleCategory(@Param("categoryName") String categoryName, Pageable pageable);

/**
* 검색어로 제목에 대하여 책 조회 (페이징)
* 검색어로 제목에 대하여 한줄처방 개수 or 조회수로 정렬하여 책 조회 (페이징)
* @param searchWord
* @return Page<Book>
*/
@EntityGraph(attributePaths = {"bigCategory", "middleCategory"})
Page<Book> findPagingByTitleContaining(String searchWord, Pageable pageable);
@EntityGraph(attributePaths = {"middleCategory"})
@Query("select b from Book b where b.title like %:searchWord%")
Page<Book> findPagingByTitleOrderByCount(@Param("searchWord") String searchWord, Pageable pageable);

/**
* 검색어로 작가명에 대하여 책 조회 (페이징)
* 검색어로 작가명에 대하여 한줄처방 개수 or 조회수로 정렬하여 책 조회 (페이징)
* @param searchWord
* @return Page<Book>
*/
@EntityGraph(attributePaths = {"bigCategory", "middleCategory"})
Page<Book> findPagingByAuthorContaining(String searchWord, Pageable pageable);
@EntityGraph(attributePaths = {"middleCategory"})
@Query("select b from Book b where b.author like %:searchWord%")
Page<Book> findPagingByAuthorOrderByCount(@Param("searchWord") String searchWord, Pageable pageable);

@EntityGraph(attributePaths = {"bigCategory", "middleCategory"})

@EntityGraph(attributePaths = {"middleCategory"})
@Query("select b from Book b left join b.bookKeywords bk left join bk.keywordItem where b.isbn = :isbn")
Book findBookDetailWithKeywordByIsbn(@Param("isbn") String isbn);
Book findBookWithKeywordByIsbn(@Param("isbn") String isbn);

/**
* 해당 키워드를 갖고 있는 책을 검색 (리스트)
* 해당 키워드를 갖고 있는 책을 한줄처방 개수로 정렬하여 검색 (리스트)
* @param keywordName
* @return
*/
@EntityGraph(attributePaths = {"bigCategory", "middleCategory"})
@Query("select b from Book b join fetch b.bookKeywords bk join fetch bk.keywordItem ki " +
@EntityGraph(attributePaths = {"middleCategory"})
@Query("select b from Book b left join b.bookKeywords bk left join bk.keywordItem ki " +
"left join OneLinePrescription op on op.book.id = b.id " +
"where ki.name = :keywordName")
Page<Book> findPagingByKeyword(@Param("keywordName") String keywordName, Pageable pageable);

Expand All @@ -67,7 +71,7 @@ public interface BookRepository extends JpaRepository<Book, Long> {
* @param keywordNameList
* @return
*/
@EntityGraph(attributePaths = {"bigCategory", "middleCategory"})
@EntityGraph(attributePaths = {"middleCategory"})
@Query("select b from Book b left join b.bookKeywords bk left join bk.keywordItem ki " +
"where (LOWER(b.title) like %:searchWord% or LOWER(b.author) like %:searchWord%) and ki.name in :names")
Page<Book> findPagingBySearchWordAndKeyword(@Param("searchWord") String searchWord,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BookSearchService {
*/
public List<BookSearchResponseDto> searchBookOnModalByTitleContainingSearchWord(String searchWord, Pageable pageable) {

Page<Book> bookPageList = bookRepository.findPagingByTitleContaining(searchWord, pageable);
Page<Book> bookPageList = bookRepository.findPagingByTitleOrderByCount(searchWord, pageable);

List<Book> bookList = bookPageList.getContent();
List<BookSearchResponseDto> bookSearchResponseDtoList = BookSearchResponseDto.toDtoList(bookList);
Expand All @@ -36,7 +36,7 @@ public List<BookSearchResponseDto> searchBookOnModalByTitleContainingSearchWord(
*/
public Page<BookDto> searchBookOnPageByTitleContainingSearchWord(String searchWord, Pageable pageable) {

Page<Book> bookPageList = bookRepository.findPagingByTitleContaining(searchWord, pageable);
Page<Book> bookPageList = bookRepository.findPagingByTitleOrderByCount(searchWord, pageable);

Page<BookDto> bookDtoPage = BookDto.toDtoPageWithKeywordDto(bookPageList);

Expand All @@ -48,7 +48,7 @@ public Page<BookDto> searchBookOnPageByTitleContainingSearchWord(String searchWo
*/
public List<BookSearchResponseDto> searchBookOnModalByAuthorContainingSearchWord(String searchWord, Pageable pageable) {

Page<Book> bookPageList = bookRepository.findPagingByAuthorContaining(searchWord, pageable);
Page<Book> bookPageList = bookRepository.findPagingByAuthorOrderByCount(searchWord, pageable);

List<Book> bookList = bookPageList.getContent();
List<BookSearchResponseDto> bookSearchResponseDtoList = BookSearchResponseDto.toDtoList(bookList);
Expand All @@ -61,7 +61,7 @@ public List<BookSearchResponseDto> searchBookOnModalByAuthorContainingSearchWord
*/
public Page<BookDto> searchBookOnPageByAuthorContainingSearchWord(String searchWord, Pageable pageable) {

Page<Book> bookPageList = bookRepository.findPagingByAuthorContaining(searchWord, pageable);
Page<Book> bookPageList = bookRepository.findPagingByAuthorOrderByCount(searchWord, pageable);

Page<BookDto> bookDtoPage = BookDto.toDtoPageWithKeywordDto(bookPageList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public Page<BookDto> getBookPageByMiddleCategory(String categoryName, Pageable p
}

public BookDto getBookDetails(String isbn){
Book book = bookRepository.findBookDetailWithKeywordByIsbn(isbn);
Book book = bookRepository.findBookWithKeywordByIsbn(isbn);
book.plusViewCount();

BookDto bookDto = BookDto.toDtoWithKeywordDto(book);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,28 @@ public void setOccupation(Occupation occupation) {
}
public void setDescription(String description) {this.description = description;}

public void setBoardCount(int boardCount) {
this.boardCount = boardCount;
public void plusBoardCount() {
this.boardCount += 1;
}

public void setPrescriptionCount(int prescriptionCount) {
this.prescriptionCount = prescriptionCount;
public void minusBoardCount() {
if(this.boardCount > 0) {
this.boardCount -= 1;
} else{
this.boardCount = 0;
}
}

public void plusOneLineCount() {
this.prescriptionCount += 1;
}

public void minusOneLineCount() {
if(this.prescriptionCount > 0) {
this.prescriptionCount -= 1;
} else{
this.prescriptionCount = 0;
}
}

public void update(Occupation occupation, String description){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public OneLinePrescription createOneLinePrescription(OneLineCreateDto oneLineCre
oneLinePrescription.setClientAndBook(client, book);
OneLinePrescription savedResult = oneLinePrescriptionRepository.save(oneLinePrescription);

client.setPrescriptionCount(client.getPrescriptionCount() + 1);
client.plusOneLineCount();
book.plusOneLineCount();

return savedResult;
}
Expand All @@ -53,15 +54,18 @@ public void updateOneLinePrescription(Long oneLinePrescriptionId, OneLineUpdateD
String loginId = authentication.getUsername();

String bookIsbn = oneLineUpdateDto.getBookIsbn();
Book book = bookRepository.findOptionalByIsbn(bookIsbn).get();
Book afterBook = bookRepository.findOptionalByIsbn(bookIsbn).get();

OneLinePrescription oneLinePrescription = oneLinePrescriptionRepository.findById(oneLinePrescriptionId).get();
Book beforeBook = oneLinePrescription.getBook();

beforeBook.minusOneLineCount();
afterBook.plusOneLineCount();

oneLinePrescription.setTitle(oneLineUpdateDto.getTitle());
oneLinePrescription.setDescription(oneLineUpdateDto.getDescription());
oneLinePrescription.setKeyword(oneLineUpdateDto.getKeyword());
oneLinePrescription.setBook(book);

oneLinePrescription.setBook(afterBook);
}

@Transactional
Expand All @@ -70,9 +74,12 @@ public void deleteOneLinePrescription(Long oneLinePrescriptionId, Authentication
Client client = clientRepository.findByLoginId(loginId).get();

OneLinePrescription prescription = oneLinePrescriptionRepository.findById(oneLinePrescriptionId).get();
Book book = prescription.getBook();

oneLinePrescriptionRepository.delete(prescription);
client.setPrescriptionCount(client.getPrescriptionCount() - 1);
client.minusOneLineCount();
book.minusOneLineCount();

}

public OneLineResponseDto getOneLinePrescription(Long oneLinePrescriptionId, AuthenticationAdapter authentication) {
Expand Down

0 comments on commit 1a41a7f

Please sign in to comment.