Skip to content

Commit

Permalink
fix: add reading status to bookshelf
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Aug 23, 2024
1 parent 41f0664 commit ca9b1b5
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,84 @@
import lombok.Getter;
import toy.bookchat.bookchat.domain.book.Book;
import toy.bookchat.bookchat.domain.bookshelf.BookShelf;
import toy.bookchat.bookchat.domain.bookshelf.ReadingStatus;
import toy.bookchat.bookchat.domain.bookshelf.Star;

@Getter
@EqualsAndHashCode
public class BookShelfWithBook {

private Long bookShelfId;
private Long bookId;
private String title;
private String isbn;
private String bookCoverImageUrl;
private LocalDate publishAt;
private List<String> authors;
private String publisher;
private Star star;
private Integer pages;
private LocalDateTime lastUpdatedAt;
private Long bookShelfId;
private Long bookId;
private String title;
private String isbn;
private String bookCoverImageUrl;
private LocalDate publishAt;
private List<String> authors;
private String publisher;
private ReadingStatus readingStatus;
private Star star;
private Integer pages;
private LocalDateTime lastUpdatedAt;

@Builder
private BookShelfWithBook(Long bookShelfId, Long bookId, String title, String isbn, String bookCoverImageUrl, LocalDate publishAt, List<String> authors, String publisher, Star star, Integer pages,
LocalDateTime lastUpdatedAt) {
this.bookShelfId = bookShelfId;
this.bookId = bookId;
this.title = title;
this.isbn = isbn;
this.bookCoverImageUrl = bookCoverImageUrl;
this.publishAt = publishAt;
this.authors = authors;
this.publisher = publisher;
this.star = star;
this.pages = pages;
this.lastUpdatedAt = lastUpdatedAt;
}
@Builder
private BookShelfWithBook(Long bookShelfId, Long bookId, String title, String isbn, String bookCoverImageUrl,
LocalDate publishAt, List<String> authors, String publisher, ReadingStatus readingStatus, Star star,
Integer pages,
LocalDateTime lastUpdatedAt) {
this.bookShelfId = bookShelfId;
this.bookId = bookId;
this.title = title;
this.isbn = isbn;
this.bookCoverImageUrl = bookCoverImageUrl;
this.publishAt = publishAt;
this.authors = authors;
this.publisher = publisher;
this.readingStatus = readingStatus;
this.star = star;
this.pages = pages;
this.lastUpdatedAt = lastUpdatedAt;
}

@QueryProjection
public BookShelfWithBook(Long bookShelfId, Long bookId, String title, String isbn, String bookCoverImageUrl, LocalDate publishAt, String publisher, Star star, Integer pages,
LocalDateTime lastUpdatedAt) {
this.bookShelfId = bookShelfId;
this.bookId = bookId;
this.title = title;
this.isbn = isbn;
this.bookCoverImageUrl = bookCoverImageUrl;
this.publishAt = publishAt;
this.publisher = publisher;
this.star = star;
this.pages = pages;
this.lastUpdatedAt = lastUpdatedAt;
}
@QueryProjection
public BookShelfWithBook(Long bookShelfId, Long bookId, String title, String isbn, String bookCoverImageUrl,
LocalDate publishAt, String publisher, ReadingStatus readingStatus, Star star, Integer pages,
LocalDateTime lastUpdatedAt) {
this.bookShelfId = bookShelfId;
this.bookId = bookId;
this.title = title;
this.isbn = isbn;
this.bookCoverImageUrl = bookCoverImageUrl;
this.publishAt = publishAt;
this.publisher = publisher;
this.readingStatus = readingStatus;
this.star = star;
this.pages = pages;
this.lastUpdatedAt = lastUpdatedAt;
}

public void setAuthors(List<String> authors) {
this.authors = authors;
}
public void setAuthors(List<String> authors) {
this.authors = authors;
}


public BookShelf toBookShelf() {
Book book = Book.builder()
.title(this.title)
.isbn(this.isbn)
.bookCoverImageUrl(this.bookCoverImageUrl)
.publisher(this.publisher)
.publishAt(this.publishAt)
.authors(this.authors)
.build();
public BookShelf toBookShelf() {
Book book = Book.builder()
.title(this.title)
.isbn(this.isbn)
.bookCoverImageUrl(this.bookCoverImageUrl)
.publisher(this.publisher)
.publishAt(this.publishAt)
.authors(this.authors)
.build();

return BookShelf.builder()
.id(this.bookShelfId)
.book(book)
.star(this.star)
.pages(this.pages)
.lastUpdatedAt(this.lastUpdatedAt)
.build();
}
return BookShelf.builder()
.id(this.bookShelfId)
.book(book)
.readingStatus(this.readingStatus)
.star(this.star)
.pages(this.pages)
.lastUpdatedAt(this.lastUpdatedAt)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

public interface BookShelfQueryRepository {

Optional<BookShelfEntity> findByIdAndUserId(Long bookShelfId, Long userId);
Optional<BookShelfEntity> findByIdAndUserId(Long bookShelfId, Long userId);

void deleteBookShelfByIdAndUserId(Long bookShelfId, Long userId);
void deleteBookShelfByIdAndUserId(Long bookShelfId, Long userId);

void deleteAllByUserId(Long userId);
void deleteAllByUserId(Long userId);

Page<BookShelfWithBook> findBookShelfWithBook(Long userId, ReadingStatus readingStatus, Pageable pageable);
Page<BookShelfWithBook> findBookShelfWithBook(Long userId, ReadingStatus readingStatus, Pageable pageable);

Optional<BookShelfWithBook> findByUserIdAndIsbnAndPublishAt(Long userId, String isbn, LocalDate publishAt);
BookShelfWithBook findByUserIdAndIsbnAndPublishAt(Long userId, String isbn, LocalDate publishAt);

Optional<BookShelfWithBook> findBookShelfWithBook(Long userId, Long bookShelfId);
BookShelfWithBook findBookShelfWithBook(Long userId, Long bookShelfId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import toy.bookchat.bookchat.db_module.bookshelf.BookShelfWithBook;
import toy.bookchat.bookchat.db_module.bookshelf.QBookShelfWithBook;
import toy.bookchat.bookchat.domain.bookshelf.ReadingStatus;
import toy.bookchat.bookchat.exception.notfound.book.BookNotFoundException;

@Repository
public class BookShelfQueryRepositoryImpl implements BookShelfQueryRepository {
Expand All @@ -39,6 +40,7 @@ public Page<BookShelfWithBook> findBookShelfWithBook(Long userId, ReadingStatus
bookEntity.bookCoverImageUrl,
bookEntity.publishAt,
bookEntity.publisher,
bookShelfEntity.readingStatus,
bookShelfEntity.star,
bookShelfEntity.pages,
bookShelfEntity.updatedAt
Expand Down Expand Up @@ -68,7 +70,7 @@ public Page<BookShelfWithBook> findBookShelfWithBook(Long userId, ReadingStatus
}

@Override
public Optional<BookShelfWithBook> findByUserIdAndIsbnAndPublishAt(Long userId, String isbn, LocalDate publishAt) {
public BookShelfWithBook findByUserIdAndIsbnAndPublishAt(Long userId, String isbn, LocalDate publishAt) {
BookShelfWithBook bookShelfWithBook = queryFactory.select(new QBookShelfWithBook(
bookShelfEntity.id,
bookEntity.id,
Expand All @@ -77,6 +79,7 @@ public Optional<BookShelfWithBook> findByUserIdAndIsbnAndPublishAt(Long userId,
bookEntity.bookCoverImageUrl,
bookEntity.publishAt,
bookEntity.publisher,
bookShelfEntity.readingStatus,
bookShelfEntity.star,
bookShelfEntity.pages,
bookShelfEntity.updatedAt
Expand All @@ -88,17 +91,21 @@ public Optional<BookShelfWithBook> findByUserIdAndIsbnAndPublishAt(Long userId,
.and(bookEntity.publishAt.eq(publishAt)))
.fetchOne();

if (bookShelfWithBook == null) {
throw new BookNotFoundException();
}

BookEntity findBookEntity = queryFactory.select(bookEntity)
.from(bookEntity)
.where(bookEntity.id.eq(bookShelfWithBook.getBookId()))
.fetchOne();
bookShelfWithBook.setAuthors(findBookEntity.getAuthors());

return Optional.ofNullable(bookShelfWithBook);
return bookShelfWithBook;
}

@Override
public Optional<BookShelfWithBook> findBookShelfWithBook(Long userId, Long bookShelfId) {
public BookShelfWithBook findBookShelfWithBook(Long userId, Long bookShelfId) {
BookShelfWithBook bookShelfWithBook = queryFactory.select(new QBookShelfWithBook(
bookShelfEntity.id,
bookEntity.id,
Expand All @@ -107,6 +114,7 @@ public Optional<BookShelfWithBook> findBookShelfWithBook(Long userId, Long bookS
bookEntity.bookCoverImageUrl,
bookEntity.publishAt,
bookEntity.publisher,
bookShelfEntity.readingStatus,
bookShelfEntity.star,
bookShelfEntity.pages,
bookShelfEntity.updatedAt
Expand All @@ -117,13 +125,17 @@ public Optional<BookShelfWithBook> findBookShelfWithBook(Long userId, Long bookS
.and(bookShelfEntity.id.eq(bookShelfId)))
.fetchOne();

if (bookShelfWithBook == null) {
throw new BookNotFoundException();
}

BookEntity findBookEntity = queryFactory.select(bookEntity)
.from(bookEntity)
.where(bookEntity.id.eq(bookShelfWithBook.getBookId()))
.fetchOne();
bookShelfWithBook.setAuthors(findBookEntity.getAuthors());

return Optional.ofNullable(bookShelfWithBook);
return bookShelfWithBook;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import toy.bookchat.bookchat.domain.bookshelf.BookShelf;
import toy.bookchat.bookchat.domain.bookshelf.ReadingStatus;
Expand All @@ -31,47 +32,54 @@
@RequestMapping("/v1/api")
public class BookShelfController {

private final BookShelfService bookShelfService;
private final BookShelfService bookShelfService;

public BookShelfController(BookShelfService bookShelfService) {
this.bookShelfService = bookShelfService;
}
public BookShelfController(BookShelfService bookShelfService) {
this.bookShelfService = bookShelfService;
}

@GetMapping("/bookshelves/{bookShelfId}")
public BookShelfResponse getBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) {
return BookShelfResponse.from(bookShelfService.getBookOnBookShelf(bookShelfId, tokenPayload.getUserId()));
}
@GetMapping("/bookshelves/{bookShelfId}")
public BookShelfResponse getBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) {
return BookShelfResponse.from(bookShelfService.getBookOnBookShelf(bookShelfId, tokenPayload.getUserId()));
}


@PostMapping("/bookshelves")
public ResponseEntity<Void> putBookOnBookShelf(@Valid @RequestBody CreateBookShelfRequest createBookShelfRequest, @UserPayload TokenPayload tokenPayload) {
Long bookShelfId = bookShelfService.putBookOnBookShelf(createBookShelfRequest.toTarget(), createBookShelfRequest.getBook(), tokenPayload.getUserId());
@PostMapping("/bookshelves")
public ResponseEntity<Void> putBookOnBookShelf(@Valid @RequestBody CreateBookShelfRequest createBookShelfRequest,
@UserPayload TokenPayload tokenPayload) {
Long bookShelfId = bookShelfService.putBookOnBookShelf(createBookShelfRequest.toTarget(),
createBookShelfRequest.getBook(), tokenPayload.getUserId());

return ResponseEntity.status(CREATED)
.headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId)))
.build();
}
return ResponseEntity.status(CREATED)
.headers(hs -> hs.setLocation(URI.create("/v1/api/bookshelves/" + bookShelfId)))
.build();
}

@GetMapping("/bookshelves")
public SearchBookShelfByReadingStatus takeBooksOutOfBookShelves(ReadingStatus readingStatus, Pageable pageable, @UserPayload TokenPayload tokenPayload) {
return new SearchBookShelfByReadingStatus(bookShelfService.takeBooksOutOfBookShelves(readingStatus, pageable, tokenPayload.getUserId()));
}
@GetMapping("/bookshelves")
public SearchBookShelfByReadingStatus takeBooksOutOfBookShelves(ReadingStatus readingStatus, Pageable pageable,
@UserPayload TokenPayload tokenPayload) {
return new SearchBookShelfByReadingStatus(
bookShelfService.takeBooksOutOfBookShelves(readingStatus, pageable, tokenPayload.getUserId()));
}

@GetMapping("/bookshelves/book")
public ExistenceBookOnBookShelfResponse findBookIfExistedOnBookShelves(String isbn, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate publishAt, @UserPayload TokenPayload tokenPayload) {
BookShelf bookShelf = bookShelfService.getBookIfExisted(isbn, publishAt, tokenPayload.getUserId());
@GetMapping("/bookshelves/book")
public ExistenceBookOnBookShelfResponse findBookIfExistedOnBookShelves(@RequestParam String isbn,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate publishAt,
@UserPayload TokenPayload tokenPayload) {
BookShelf bookShelf = bookShelfService.getBookIfExisted(isbn, publishAt, tokenPayload.getUserId());

return ExistenceBookOnBookShelfResponse.from(bookShelf);
}
return ExistenceBookOnBookShelfResponse.from(bookShelf);
}

@PutMapping("/bookshelves/{bookShelfId}")
public void reviseBookOnBookShelf(@PathVariable Long bookShelfId, @Valid @RequestBody ReviseBookShelfRequest reviseBookShelfStarRequest, @UserPayload TokenPayload tokenPayload) {
bookShelfService.reviseBookShelf(bookShelfId, reviseBookShelfStarRequest.toTarget(), tokenPayload.getUserId());
}
@PutMapping("/bookshelves/{bookShelfId}")
public void reviseBookOnBookShelf(@PathVariable Long bookShelfId,
@Valid @RequestBody ReviseBookShelfRequest reviseBookShelfStarRequest, @UserPayload TokenPayload tokenPayload) {
bookShelfService.reviseBookShelf(bookShelfId, reviseBookShelfStarRequest.toTarget(), tokenPayload.getUserId());
}

@DeleteMapping("/bookshelves/{bookShelfId}")
public void deleteBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) {
bookShelfService.deleteBookShelf(bookShelfId, tokenPayload.getUserId());
}
@DeleteMapping("/bookshelves/{bookShelfId}")
public void deleteBookOnBookShelf(@PathVariable Long bookShelfId, @UserPayload TokenPayload tokenPayload) {
bookShelfService.deleteBookShelf(bookShelfId, tokenPayload.getUserId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,31 @@
import toy.bookchat.bookchat.db_module.bookshelf.repository.BookShelfRepository;
import toy.bookchat.bookchat.domain.bookshelf.BookShelf;
import toy.bookchat.bookchat.domain.bookshelf.ReadingStatus;
import toy.bookchat.bookchat.exception.notfound.book.BookNotFoundException;

@Component
@Transactional(readOnly = true)
public class BookShelfReader {

private final BookShelfRepository bookShelfRepository;
private final BookShelfRepository bookShelfRepository;

public BookShelfReader(BookShelfRepository bookShelfRepository) {
this.bookShelfRepository = bookShelfRepository;
}
public BookShelfReader(BookShelfRepository bookShelfRepository) {
this.bookShelfRepository = bookShelfRepository;
}

public BookShelf readBookShelf(Long userId, Long bookShelfId) {
BookShelfWithBook bookShelfWithBook = bookShelfRepository.findBookShelfWithBook(userId, bookShelfId)
.orElseThrow(BookNotFoundException::new);
public BookShelf readBookShelf(Long userId, Long bookShelfId) {
BookShelfWithBook bookShelfWithBook = bookShelfRepository.findBookShelfWithBook(userId, bookShelfId);

return bookShelfWithBook.toBookShelf();
}
return bookShelfWithBook.toBookShelf();
}

public BookShelf readBookShelf(Long userId, String isbn, LocalDate publishAt) {
BookShelfWithBook bookShelfWithBook = bookShelfRepository.findByUserIdAndIsbnAndPublishAt(userId, isbn, publishAt)
.orElseThrow(BookNotFoundException::new);
return bookShelfWithBook.toBookShelf();
}
public BookShelf readBookShelf(Long userId, String isbn, LocalDate publishAt) {
BookShelfWithBook bookShelfWithBook = bookShelfRepository.findByUserIdAndIsbnAndPublishAt(userId, isbn, publishAt);
return bookShelfWithBook.toBookShelf();
}

public Page<BookShelf> readPagedBookShelves(Long userId, ReadingStatus readingStatus, Pageable pageable) {
Page<BookShelfWithBook> pagedBookShelfWithBook = bookShelfRepository.findBookShelfWithBook(userId, readingStatus, pageable);
return pagedBookShelfWithBook.map(BookShelfWithBook::toBookShelf);
}
public Page<BookShelf> readPagedBookShelves(Long userId, ReadingStatus readingStatus, Pageable pageable) {
Page<BookShelfWithBook> pagedBookShelfWithBook = bookShelfRepository.findBookShelfWithBook(userId, readingStatus,
pageable);
return pagedBookShelfWithBook.map(BookShelfWithBook::toBookShelf);
}
}
Loading

0 comments on commit ca9b1b5

Please sign in to comment.