Save epub progress

This commit is contained in:
aditya.chandel
2025-01-14 17:25:09 -07:00
parent e9a42ac473
commit 6e42548fbb
5 changed files with 61 additions and 27 deletions
@@ -2,9 +2,9 @@ package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.BookViewerSetting;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
import com.adityachandel.booklore.service.BooksService;
import com.adityachandel.booklore.service.metadata.BookMetadataService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.core.io.Resource;
@@ -20,7 +20,6 @@ import java.util.List;
public class BookController {
private BooksService booksService;
private BookMetadataService bookMetadataService;
@GetMapping("/{bookId}")
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
@@ -68,4 +67,10 @@ public class BookController {
public ResponseEntity<List<Book>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
}
@PostMapping("/progress")
public ResponseEntity<Void> addBookToProgress(@RequestBody @Valid ReadProgressRequest request) {
booksService.updateReadProgress(request);
return ResponseEntity.noContent().build();
}
}
@@ -0,0 +1,18 @@
package com.adityachandel.booklore.model.dto.request;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ReadProgressRequest {
@NotNull
private Long bookId;
private String epubProgress;
private Integer pdfProgress;
@AssertTrue(message = "Either epubProgress or pdfProgress must be provided")
public boolean isProgressValid() {
return epubProgress != null || pdfProgress != null;
}
}
@@ -6,14 +6,17 @@ import com.adityachandel.booklore.mapper.BookViewerSettingMapper;
import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.BookViewerSetting;
import com.adityachandel.booklore.model.dto.BookWithNeighbors;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookViewerSettingEntity;
import com.adityachandel.booklore.model.entity.ShelfEntity;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.BookViewerSettingRepository;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.repository.ShelfRepository;
import com.adityachandel.booklore.util.FileService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
@@ -131,4 +134,15 @@ public class BooksService {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
return fileService.getBookCover(bookEntity.getMetadata().getThumbnail());
}
public void updateReadProgress(ReadProgressRequest request) {
BookEntity book = bookRepository.findById(request.getBookId()).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
book.setLastReadTime(Instant.now());
if (book.getBookType() == BookFileType.EPUB) {
book.setEpubProgress(request.getEpubProgress());
} else if (book.getBookType() == BookFileType.PDF) {
book.setPdfProgress(request.getPdfProgress());
}
bookRepository.save(book);
}
}