diff --git a/src/main/java/com/techeer/checkIt/domain/book/dto/Response/BookSearchLikeRes.java b/src/main/java/com/techeer/checkIt/domain/book/dto/Response/BookSearchLikeRes.java new file mode 100644 index 0000000..81d82f9 --- /dev/null +++ b/src/main/java/com/techeer/checkIt/domain/book/dto/Response/BookSearchLikeRes.java @@ -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; +} diff --git a/src/main/java/com/techeer/checkIt/domain/book/entity/Book.java b/src/main/java/com/techeer/checkIt/domain/book/entity/Book.java index 6abc1bc..becef39 100644 --- a/src/main/java/com/techeer/checkIt/domain/book/entity/Book.java +++ b/src/main/java/com/techeer/checkIt/domain/book/entity/Book.java @@ -37,6 +37,8 @@ public class Book extends BaseEntity { private String category; @OneToMany(mappedBy = "book") private List 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; @@ -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; } } diff --git a/src/main/java/com/techeer/checkIt/domain/book/mapper/BookMapper.java b/src/main/java/com/techeer/checkIt/domain/book/mapper/BookMapper.java index 331c9e3..d362706 100644 --- a/src/main/java/com/techeer/checkIt/domain/book/mapper/BookMapper.java +++ b/src/main/java/com/techeer/checkIt/domain/book/mapper/BookMapper.java @@ -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; @@ -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 toSearchDtoList(List books){ return books.stream() .map(this::toBookSearchDto) @@ -47,4 +59,10 @@ public Page toPageDtoList(Page books) { .map(this::toBookSearchDto) .collect(Collectors.toList())); } + + public Page toPageDtoList2(Page books) { + return new PageImpl<>(books.stream() + .map(this::toBookSearchLikeDto) + .collect(Collectors.toList())); + } }