Skip to content

Commit

Permalink
feat: 인기 있는 책 조회 API entity, mapper 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Oct 19, 2023
1 parent cb43d5c commit ca2d13f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.techeer.checkIt.domain.book.dto.Response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class BookSearchLikeRes {
private String title;
private String author;
private String publisher;
private String coverImageUrl;
private int pages;
private String category;
private int like;
}
14 changes: 14 additions & 0 deletions src/main/java/com/techeer/checkIt/domain/book/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class Book extends BaseEntity {
private String category;
@OneToMany(mappedBy = "book")
private List<Reading> readingList = new ArrayList<>();
@Column(name = "like_count")
private Integer likeCount = 0;
@Builder
public Book(String title, String author, String publisher, String coverImageUrl, int pages, int height, int width, int thickness, String category) {
this.title = title;
Expand All @@ -49,4 +51,16 @@ public Book(String title, String author, String publisher, String coverImageUrl,
this.thickness = thickness;
this.category = category;
}

@PostLoad
@PrePersist
private void initializeDefaults() {
if (likeCount == null) {
likeCount = 0; // 기본값을 0으로 설정
}
}
public void updateLike(int like) {
this.likeCount = like;
}
public void setLikeCount(Integer likeCount) { this.likeCount = likeCount; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.techeer.checkIt.domain.book.mapper;

import com.techeer.checkIt.domain.book.dto.Response.BookRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchLikeRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchRes;
import com.techeer.checkIt.domain.book.entity.Book;
import com.techeer.checkIt.domain.book.entity.BookDocument;
Expand Down Expand Up @@ -37,6 +38,17 @@ public BookSearchRes toBookSearchDto(BookDocument book) {
.category(book.getCategory())
.build();
}
public BookSearchLikeRes toBookSearchLikeDto(Book book) {
return BookSearchLikeRes.builder()
.title(book.getTitle())
.author(book.getAuthor())
.publisher(book.getPublisher())
.coverImageUrl(book.getCoverImageUrl())
.pages(book.getPages())
.category(book.getCategory())
.like(book.getLikeCount())
.build();
}
public List<BookSearchRes> toSearchDtoList(List<BookDocument> books){
return books.stream()
.map(this::toBookSearchDto)
Expand All @@ -47,4 +59,10 @@ public Page<BookSearchRes> toPageDtoList(Page<BookDocument> books) {
.map(this::toBookSearchDto)
.collect(Collectors.toList()));
}

public Page<BookSearchLikeRes> toPageDtoList2(Page<Book> books) {
return new PageImpl<>(books.stream()
.map(this::toBookSearchLikeDto)
.collect(Collectors.toList()));
}
}

0 comments on commit ca2d13f

Please sign in to comment.