diff --git a/src/main/java/com/techeer/checkIt/domain/book/repository/BookJpaRepository.java b/src/main/java/com/techeer/checkIt/domain/book/repository/BookJpaRepository.java index c03873b..5a5866f 100644 --- a/src/main/java/com/techeer/checkIt/domain/book/repository/BookJpaRepository.java +++ b/src/main/java/com/techeer/checkIt/domain/book/repository/BookJpaRepository.java @@ -1,6 +1,7 @@ package com.techeer.checkIt.domain.book.repository; import com.techeer.checkIt.domain.book.entity.Book; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -11,4 +12,6 @@ public interface BookJpaRepository extends JpaRepository { @Query("select b from Book b where b.id = :id AND b.isDeleted = false") Optional findByBookId(@Param("id") Long id); + @Query("select b from Book b where b.id IN :bookId") + List findByBookIdIn(@Param("bookId") List bookId); } diff --git a/src/main/java/com/techeer/checkIt/domain/reading/mapper/ReadingMapper.java b/src/main/java/com/techeer/checkIt/domain/reading/mapper/ReadingMapper.java index f271bd1..6a091de 100644 --- a/src/main/java/com/techeer/checkIt/domain/reading/mapper/ReadingMapper.java +++ b/src/main/java/com/techeer/checkIt/domain/reading/mapper/ReadingMapper.java @@ -19,39 +19,56 @@ public class ReadingMapper { public Reading toEntity(User user, Book book, int lastPage, ReadingStatus status) { return Reading.builder() - .user(user) - .book(book) - .lastPage(lastPage) - .status(status) - .build(); + .user(user) + .book(book) + .lastPage(lastPage) + .status(status) + .build(); } public BookRes toDto(Reading reading) { return BookRes.builder() - .id(reading.getBook().getId()) - .title(reading.getBook().getTitle()) - .author(reading.getBook().getAuthor()) - .publisher(reading.getBook().getPublisher()) - .coverImageUrl(reading.getBook().getCoverImageUrl()) - .height(reading.getBook().getHeight()) - .width(reading.getBook().getWidth()) - .pages(reading.getBook().getPages()) - .build(); + .id(reading.getBook().getId()) + .title(reading.getBook().getTitle()) + .author(reading.getBook().getAuthor()) + .publisher(reading.getBook().getPublisher()) + .coverImageUrl(reading.getBook().getCoverImageUrl()) + .height(reading.getBook().getHeight()) + .width(reading.getBook().getWidth()) + .pages(reading.getBook().getPages()) + .build(); + } + + public BookRes toDtoByBook(Book book) { + return BookRes.builder() + .id(book.getId()) + .title(book.getTitle()) + .author(book.getAuthor()) + .publisher(book.getPublisher()) + .coverImageUrl(book.getCoverImageUrl()) + .height(book.getHeight()) + .width(book.getWidth()) + .pages(book.getPages()) + .build(); } public List toDtoList(List readings) { return readings.stream().map(this::toDto).collect(Collectors.toList()); } + public List toDtoListByBook(List books) { + return books.stream().map(this::toDtoByBook).collect(Collectors.toList()); + } + public UpdateReadingAndReadingVolumeRes toUpdateReadingAndReadingVolumeResDto(Reading reading, ReadingVolume readingVolume){ return UpdateReadingAndReadingVolumeRes.builder() - .pages(readingVolume.getTodayPages()) - .lastPage(reading.getLastPage()) - .build(); + .pages(readingVolume.getTodayPages()) + .lastPage(reading.getLastPage()) + .build(); } public UpdateLastPageAndPercentageRes toUpdateLastPageAndPercentageResDto(Reading reading, double percentage) { return UpdateLastPageAndPercentageRes.builder() - .lastPage(reading.getLastPage()) - .percentage(percentage) - .build(); + .lastPage(reading.getLastPage()) + .percentage(percentage) + .build(); } - } +} diff --git a/src/main/java/com/techeer/checkIt/domain/reading/service/ReadingService.java b/src/main/java/com/techeer/checkIt/domain/reading/service/ReadingService.java index d9fe214..79f6af5 100644 --- a/src/main/java/com/techeer/checkIt/domain/reading/service/ReadingService.java +++ b/src/main/java/com/techeer/checkIt/domain/reading/service/ReadingService.java @@ -1,7 +1,9 @@ package com.techeer.checkIt.domain.reading.service; +import com.techeer.checkIt.domain.book.dao.RedisDao; import com.techeer.checkIt.domain.book.dto.Response.BookRes; import com.techeer.checkIt.domain.book.entity.Book; +import com.techeer.checkIt.domain.book.repository.BookJpaRepository; import com.techeer.checkIt.domain.reading.dto.request.CreateReadingReq; import com.techeer.checkIt.domain.reading.dto.request.UpdateReadingAndReadingVolumeReq; import com.techeer.checkIt.domain.reading.dto.request.UpdateReadingStatusReq; @@ -18,6 +20,8 @@ import com.techeer.checkIt.domain.readingVolume.mapper.ReadingVolumeMapper; import com.techeer.checkIt.domain.readingVolume.service.ReadingVolumeService; import com.techeer.checkIt.domain.user.entity.User; +import java.util.ArrayList; +import java.util.stream.Collectors; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -34,6 +38,8 @@ public class ReadingService { private final ReadingMapper readingMapper; private final ReadingVolumeService readingVolumeService; private final ReadingVolumeMapper readingVolumeMapper; + private final RedisDao redisDao; + private final BookJpaRepository bookJpaRepository; public int registerReading(User user, Book book, CreateReadingReq createRequest){ ReadingStatus status = ReadingStatus.convert(createRequest.getStatus().toUpperCase()); @@ -51,9 +57,20 @@ public int registerReading(User user, Book book, CreateReadingReq createRequest) } public List findReadingByStatus(Long userId, ReadingStatus status) { - List readings =readingRepository.findByUserIdAndStatus(userId ,status); + if (status.toString().equals("UNREAD")) { + List readings = new ArrayList<>(); + String redisUserKey = "U" + userId.toString(); + List likeBook = redisDao.getValuesList(redisUserKey).stream() + .map(s -> Long.parseLong(s)) + .collect(Collectors.toList()); + readings = bookJpaRepository.findByBookIdIn(likeBook); + return readingMapper.toDtoListByBook(readings); + } else { + List readings = new ArrayList<>(); + readings = readingRepository.findByUserIdAndStatus(userId ,status); + return readingMapper.toDtoList(readings); + } - return readingMapper.toDtoList(readings); } public void updateReadingStatus(Long userId, Long bookId, ReadingStatus status, UpdateReadingStatusReq updateStatus) { @@ -82,14 +99,14 @@ public UpdateReadingAndReadingVolumeRes updateReadingAndReadingVolume(User user, reading.updateStatus(ReadingStatus.READING); reading.updateLastPage(updateRequest.getLastPage()); // reading의 lastpages 갱신 return readingMapper - .toUpdateReadingAndReadingVolumeResDto(reading, readingVolume); + .toUpdateReadingAndReadingVolumeResDto(reading, readingVolume); } public UpdateLastPageAndPercentageRes findReadingByUserAndBook(User user, Book book) { Reading reading = readingRepository.findByUserAndBook(user,book).orElseThrow(ReadingNotFoundException::new); double percentage = calcPercentage(reading.getLastPage(), book.getPages()); UpdateLastPageAndPercentageRes updateLastPageAndPercentageRes = readingMapper - .toUpdateLastPageAndPercentageResDto(reading, percentage); + .toUpdateLastPageAndPercentageResDto(reading, percentage); return updateLastPageAndPercentageRes; }