Generalize DashboardScrollerComponent and display Latest Added Books

This commit is contained in:
aditya.chandel
2024-12-13 16:58:20 -07:00
parent 033fc95373
commit 96a04621e1
11 changed files with 63 additions and 20 deletions
@@ -37,10 +37,14 @@ public class BookController {
@RequestParam(defaultValue = "25") @Min(1) @Max(100) int size,
@RequestParam(defaultValue = "lastReadTime") String sortBy,
@RequestParam(defaultValue = "desc") String sortDir) {
if (!sortBy.equals("lastReadTime") && !sortBy.equals("addedOn")) {
return ResponseEntity.badRequest().body(null);
}
Page<BookDTO> books = booksService.getBooks(page, size, sortBy, sortDir);
return ResponseEntity.ok(books);
}
@GetMapping("/search")
public ResponseEntity<List<BookDTO>> searchBooks(@RequestParam String title) {
List<BookDTO> books = booksService.search(title);
@@ -15,5 +15,6 @@ public class BookDTO {
private String fileName;
private String title;
private Instant lastReadTime;
private Instant addedOn;
private List<AuthorDTO> authors = new ArrayList<>();
}
@@ -44,4 +44,7 @@ public class Book {
@Column(name = "last_read_time")
private Instant lastReadTime;
@Column(name = "added_on")
private Instant addedOn;
}
@@ -21,5 +21,7 @@ public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificat
List<Book> findByTitleContainingIgnoreCase(String title);
Page<Book> findByLastReadTimeIsNotNull(Pageable pageable);
Page<Book> findByAddedOnIsNotNull(Pageable pageable);
}
@@ -55,7 +55,12 @@ public class BooksService {
public Page<BookDTO> getBooks(int page, int size, String sortBy, String sortDir) {
Sort sort = Sort.by(Sort.Direction.fromString(sortDir), sortBy);
PageRequest pageRequest = PageRequest.of(page, size, sort);
Page<Book> bookPage = bookRepository.findByLastReadTimeIsNotNull(pageRequest);
Page<Book> bookPage = Page.empty();
if (sortBy.equals("addedOn")) {
bookPage = bookRepository.findByAddedOnIsNotNull(pageRequest);
} else if (sortBy.equals("lastReadTime")) {
bookPage = bookRepository.findByLastReadTimeIsNotNull(pageRequest);
}
List<BookDTO> bookDTOs = bookPage.getContent().stream()
.map(BookTransformer::convertToBookDTO)
.collect(Collectors.toList());
@@ -14,6 +14,7 @@ public class BookTransformer {
bookDTO.setFileName(book.getFileName());
bookDTO.setTitle(book.getTitle());
bookDTO.setLastReadTime(book.getLastReadTime());
bookDTO.setAddedOn(book.getAddedOn());
bookDTO.setAuthors(book.getAuthors().stream().map(AuthorTransformer::toAuthorDTO).collect(Collectors.toList()));
return bookDTO;
}
@@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS book
library_id BIGINT NOT NULL,
path VARCHAR(1000) NOT NULL,
last_read_time TIMESTAMP NULL,
added_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_library FOREIGN KEY (library_id) REFERENCES library (id) ON DELETE CASCADE,
CONSTRAINT unique_file_library UNIQUE (file_name, library_id),
INDEX idx_library_id (library_id),