Skip to content

Commit

Permalink
refactor : 찜기능 redis 사용하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Oct 10, 2023
1 parent 2e6907e commit 79bf42f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,4 +12,6 @@ public interface BookJpaRepository extends JpaRepository<Book,Long> {
@Query("select b from Book b where b.id = :id AND b.isDeleted = false")
Optional<Book> findByBookId(@Param("id") Long id);

@Query("select b from Book b where b.id IN :bookId")
List<Book> findByBookIdIn(@Param("bookId") List<Long> bookId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookRes> toDtoList(List<Reading> readings) {
return readings.stream().map(this::toDto).collect(Collectors.toList());
}

public List<BookRes> toDtoListByBook(List<Book> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -51,9 +57,20 @@ public int registerReading(User user, Book book, CreateReadingReq createRequest)
}

public List<BookRes> findReadingByStatus(Long userId, ReadingStatus status) {
List<Reading> readings =readingRepository.findByUserIdAndStatus(userId ,status);
if (status.toString().equals("UNREAD")) {
List<Book> readings = new ArrayList<>();
String redisUserKey = "U" + userId.toString();
List<Long> likeBook = redisDao.getValuesList(redisUserKey).stream()
.map(s -> Long.parseLong(s))
.collect(Collectors.toList());
readings = bookJpaRepository.findByBookIdIn(likeBook);
return readingMapper.toDtoListByBook(readings);
} else {
List<Reading> 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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 79bf42f

Please sign in to comment.