WIP: Checkpoint for global and individual book settings

This commit is contained in:
aditya.chandel
2025-01-17 16:03:40 -07:00
parent f75f005347
commit 5937574eb8
25 changed files with 384 additions and 299 deletions
@@ -2,6 +2,7 @@ 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.BookViewerSettings;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
import com.adityachandel.booklore.service.BooksService;
@@ -47,14 +48,14 @@ public class BookController {
return booksService.getBookData(bookId);
}
@GetMapping("/{bookId}/viewer-setting")
public ResponseEntity<BookViewerSetting> getBookViewerSettings(@PathVariable long bookId) {
@GetMapping("/{bookId}/book-viewer-setting")
public ResponseEntity<BookViewerSettings> getBookViewerSettings(@PathVariable long bookId) {
return ResponseEntity.ok(booksService.getBookViewerSetting(bookId));
}
@PutMapping("/{bookId}/viewer-setting")
public ResponseEntity<Void> updateBookViewerSettings(@RequestBody BookViewerSetting bookViewerSetting, @PathVariable long bookId) {
booksService.saveBookViewerSetting(bookId, bookViewerSetting);
@PutMapping("/{bookId}/book-viewer-setting")
public ResponseEntity<Void> updateBookViewerSettings(@RequestBody BookViewerSettings bookViewerSettings, @PathVariable long bookId) {
booksService.updateBookViewerSetting(bookId, bookViewerSettings);
return ResponseEntity.noContent().build();
}
@@ -8,6 +8,8 @@ import org.springframework.http.HttpStatus;
public enum ApiError {
AUTHOR_NOT_FOUND(HttpStatus.NOT_FOUND, "Author not found with ID: %d"),
BOOK_NOT_FOUND(HttpStatus.NOT_FOUND, "Book not found with ID: %d"),
UNSUPPORTED_BOOK_TYPE(HttpStatus.BAD_REQUEST, "Unsupported book type for viewer settings"),
INVALID_VIEWER_SETTING(HttpStatus.BAD_REQUEST, "Invalid viewer setting for the book"),
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path"),
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "Image not found or not readable"),
INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format, only pdf and epub are supported"),
@@ -1,12 +1,12 @@
package com.adityachandel.booklore.mapper;
import com.adityachandel.booklore.model.dto.BookViewerSetting;
import com.adityachandel.booklore.model.entity.BookViewerSettingEntity;
import com.adityachandel.booklore.model.entity.PdfViewerPreferencesEntity;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface BookViewerSettingMapper {
BookViewerSetting toBookViewerSetting(BookViewerSettingEntity entity);
BookViewerSetting toBookViewerSetting(PdfViewerPreferencesEntity entity);
}
@@ -0,0 +1,13 @@
package com.adityachandel.booklore.mapper;
import com.adityachandel.booklore.model.dto.EpubViewerPreferences;
import com.adityachandel.booklore.model.entity.EpubViewerPreferencesEntity;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface EpubViewerPreferencesMapper {
EpubViewerPreferences toModel(EpubViewerPreferencesEntity entity);
EpubViewerPreferencesEntity toEntity(EpubViewerPreferences entity);
}
@@ -0,0 +1,13 @@
package com.adityachandel.booklore.mapper;
import com.adityachandel.booklore.model.dto.PdfViewerPreferences;
import com.adityachandel.booklore.model.entity.PdfViewerPreferencesEntity;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface PdfViewerPreferencesMapper {
PdfViewerPreferences toModel(PdfViewerPreferencesEntity entity);
PdfViewerPreferencesEntity toEntity(PdfViewerPreferences model);
}
@@ -8,6 +8,6 @@ import lombok.Data;
public class BookViewerSetting {
private int pageNumber;
private String zoom;
private Boolean sidebar_visible;
private Boolean sidebarVisible;
private String spread;
}
@@ -0,0 +1,11 @@
package com.adityachandel.booklore.model.dto;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class BookViewerSettings {
private PdfViewerPreferences pdfSettings;
private EpubViewerPreferences epubSettings;
}
@@ -0,0 +1,13 @@
package com.adityachandel.booklore.model.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class EpubViewerPreferences {
private Long bookId;
private String theme;
private String font;
private Integer fontSize;
}
@@ -0,0 +1,13 @@
package com.adityachandel.booklore.model.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class PdfViewerPreferences {
private Long bookId;
private String zoom;
private Boolean sidebarVisible;
private String spread;
}
@@ -37,7 +37,10 @@ public class BookEntity {
private LibraryEntity library;
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private BookViewerSettingEntity viewerSetting;
private PdfViewerPreferencesEntity pdfViewerPrefs;
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private EpubViewerPreferencesEntity epubViewerPrefs;
@Column(name = "last_read_time")
private Instant lastReadTime;
@@ -0,0 +1,32 @@
package com.adityachandel.booklore.model.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "epub_viewer_preference")
public class EpubViewerPreferencesEntity {
@Id
private Long bookId;
@OneToOne
@MapsId
@JoinColumn(name = "book_id")
@JsonIgnore
private BookEntity book;
@Column(name = "theme")
private String theme;
@Column(name = "font")
private String font;
@Column(name = "font_size")
private Integer fontSize;
}
@@ -10,8 +10,8 @@ import lombok.*;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "book_viewer_setting")
public class BookViewerSettingEntity {
@Table(name = "pdf_viewer_preference")
public class PdfViewerPreferencesEntity {
@Id
private Long bookId;
@@ -21,14 +21,11 @@ public class BookViewerSettingEntity {
@JsonIgnore
private BookEntity book;
@Column(name = "page_number")
private int pageNumber;
@Column(name = "zoom")
private String zoom;
@Column(name = "sidebar_visible")
private Boolean sidebar_visible;
private Boolean sidebarVisible;
@Column(name = "spread")
private String spread;
@@ -1,10 +0,0 @@
package com.adityachandel.booklore.repository;
import com.adityachandel.booklore.model.entity.BookViewerSettingEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookViewerSettingRepository extends JpaRepository<BookViewerSettingEntity, Long> {
}
@@ -0,0 +1,10 @@
package com.adityachandel.booklore.repository;
import com.adityachandel.booklore.model.entity.EpubViewerPreferencesEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EpubViewerPreferencesRepository extends JpaRepository<EpubViewerPreferencesEntity, Long> {
}
@@ -0,0 +1,10 @@
package com.adityachandel.booklore.repository;
import com.adityachandel.booklore.model.entity.PdfViewerPreferencesEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PdfViewerPreferencesRepository extends JpaRepository<PdfViewerPreferencesEntity, Long> {
}
@@ -1,15 +1,9 @@
package com.adityachandel.booklore.service;
import com.adityachandel.booklore.model.LibraryFile;
import com.adityachandel.booklore.model.entity.AuthorEntity;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.model.entity.BookViewerSettingEntity;
import com.adityachandel.booklore.model.entity.*;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.repository.AuthorRepository;
import com.adityachandel.booklore.repository.BookMetadataRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.BookViewerSettingRepository;
import com.adityachandel.booklore.repository.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@@ -26,7 +20,8 @@ public class BookCreatorService {
private AuthorRepository authorRepository;
private BookRepository bookRepository;
private BookMetadataRepository bookMetadataRepository;
private BookViewerSettingRepository bookViewerSettingRepository;
private PdfViewerPreferencesRepository pdfViewerPreferencesRepository;
private EpubViewerPreferencesRepository epubViewerPreferencesRepository;
public BookEntity createShellBook(LibraryFile libraryFile, BookFileType bookFileType) {
File bookFile = new File(libraryFile.getFilePath());
@@ -35,16 +30,25 @@ public class BookCreatorService {
.path(bookFile.getPath())
.fileName(bookFile.getName())
.bookType(bookFileType)
.addedOn(Instant.now())
.build();
BookMetadataEntity bookMetadataEntity = BookMetadataEntity.builder().build();
BookViewerSettingEntity bookViewerSetting = BookViewerSettingEntity.builder()
.book(bookEntity)
.bookId(bookEntity.getId())
.build();
if (bookFileType == BookFileType.PDF) {
PdfViewerPreferencesEntity pdfViewerPreferencesEntity = PdfViewerPreferencesEntity.builder()
.book(bookEntity)
.bookId(bookEntity.getId())
.build();
bookEntity.setPdfViewerPrefs(pdfViewerPreferencesEntity);
} else if (bookFileType == BookFileType.EPUB) {
EpubViewerPreferencesEntity epubViewerPreferencesEntity = EpubViewerPreferencesEntity.builder()
.book(bookEntity)
.bookId(bookEntity.getId())
.build();
bookEntity.setEpubViewerPrefs(epubViewerPreferencesEntity);
}
bookEntity.setMetadata(bookMetadataEntity);
bookEntity.setViewerSetting(bookViewerSetting);
bookEntity.setAddedOn(Instant.now());
bookEntity = bookRepository.saveAndFlush(bookEntity);
return bookEntity;
}
@@ -73,6 +77,10 @@ public class BookCreatorService {
}
bookRepository.save(bookEntity);
bookMetadataRepository.save(bookEntity.getMetadata());
bookViewerSettingRepository.save(bookEntity.getViewerSetting());
if (bookEntity.getBookType() == BookFileType.EPUB) {
epubViewerPreferencesRepository.save(bookEntity.getEpubViewerPrefs());
} else if (bookEntity.getBookType() == BookFileType.PDF) {
pdfViewerPreferencesRepository.save(bookEntity.getPdfViewerPrefs());
}
}
}
@@ -2,21 +2,20 @@ package com.adityachandel.booklore.service;
import com.adityachandel.booklore.exception.ApiError;
import com.adityachandel.booklore.mapper.BookMapper;
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.mapper.EpubViewerPreferencesMapper;
import com.adityachandel.booklore.mapper.PdfViewerPreferencesMapper;
import com.adityachandel.booklore.model.dto.*;
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.EpubViewerPreferencesEntity;
import com.adityachandel.booklore.model.entity.PdfViewerPreferencesEntity;
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.EpubViewerPreferencesRepository;
import com.adityachandel.booklore.repository.PdfViewerPreferencesRepository;
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;
@@ -29,7 +28,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -44,13 +42,45 @@ import java.util.stream.Collectors;
public class BooksService {
private final BookRepository bookRepository;
private final BookViewerSettingRepository bookViewerSettingRepository;
private final LibraryRepository libraryRepository;
private final PdfViewerPreferencesRepository pdfViewerPreferencesRepository;
private final EpubViewerPreferencesRepository epubViewerPreferencesRepository;
private final ShelfRepository shelfRepository;
private final FileService fileService;
private final BookMapper bookMapper;
private final BookViewerSettingMapper bookViewerSettingMapper;
private final PdfViewerPreferencesMapper pdfViewerPreferencesMapper;
private final EpubViewerPreferencesMapper epubViewerPreferencesMapper;
public BookViewerSettings getBookViewerSetting(long bookId) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
if (bookEntity.getBookType() == BookFileType.PDF) {
return BookViewerSettings.builder().pdfSettings(pdfViewerPreferencesMapper.toModel(bookEntity.getPdfViewerPrefs())).build();
} else if (bookEntity.getBookType() == BookFileType.EPUB) {
return BookViewerSettings.builder().epubSettings(epubViewerPreferencesMapper.toModel(bookEntity.getEpubViewerPrefs())).build();
} else {
throw ApiError.UNSUPPORTED_BOOK_TYPE.createException();
}
}
public void updateBookViewerSetting(long bookId, BookViewerSettings bookViewerSettings) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
if (bookEntity.getBookType() == BookFileType.PDF) {
PdfViewerPreferences pdfSettings = bookViewerSettings.getPdfSettings();
PdfViewerPreferencesEntity viewerPrefs = bookEntity.getPdfViewerPrefs();
viewerPrefs.setZoom(pdfSettings.getZoom());
viewerPrefs.setSpread(pdfSettings.getSpread());
viewerPrefs.setSidebarVisible(pdfSettings.getSidebarVisible());
pdfViewerPreferencesRepository.save(viewerPrefs);
} else if (bookEntity.getBookType() == BookFileType.EPUB) {
EpubViewerPreferences epubSettings = bookViewerSettings.getEpubSettings();
EpubViewerPreferencesEntity viewerPrefs = bookEntity.getEpubViewerPrefs();
viewerPrefs.setFont(epubSettings.getFont());
viewerPrefs.setFontSize(epubSettings.getFontSize());
viewerPrefs.setTheme(epubSettings.getTheme());
epubViewerPreferencesRepository.save(viewerPrefs);
} else {
throw ApiError.UNSUPPORTED_BOOK_TYPE.createException();
}
}
public Book getBook(long bookId, boolean withDescription) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
@@ -67,15 +97,6 @@ public class BooksService {
.collect(Collectors.toList());
}
public void saveBookViewerSetting(long bookId, BookViewerSetting bookViewerSettingDTO) {
BookViewerSettingEntity bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
bookViewerSetting.setPageNumber(bookViewerSettingDTO.getPageNumber());
bookViewerSetting.setZoom(bookViewerSettingDTO.getZoom());
bookViewerSetting.setSpread(bookViewerSettingDTO.getSpread());
bookViewerSetting.setSidebar_visible(bookViewerSettingDTO.getSidebar_visible());
bookViewerSettingRepository.save(bookViewerSetting);
}
public ResponseEntity<byte[]> getBookData(long bookId) throws IOException {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
byte[] pdfBytes = Files.readAllBytes(new File(bookEntity.getPath()).toPath());
@@ -89,29 +110,12 @@ public class BooksService {
return bookEntities.stream().map(bookMapper::toBook).toList();
}
public BookViewerSetting getBookViewerSetting(long bookId) {
BookViewerSettingEntity bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
return bookViewerSettingMapper.toBookViewerSetting(bookViewerSetting);
}
public Book updateLastReadTime(long bookId) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
bookEntity.setLastReadTime(Instant.now());
return bookMapper.toBook(bookRepository.save(bookEntity));
}
public BookWithNeighbors getBookWithNeighbours(long libraryId, long bookId) {
libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
BookEntity previousBookEntity = bookRepository.findFirstByLibraryIdAndIdLessThanOrderByIdDesc(libraryId, bookId).orElse(null);
BookEntity nextBookEntity = bookRepository.findFirstByLibraryIdAndIdGreaterThanOrderByIdAsc(libraryId, bookId).orElse(null);
return BookWithNeighbors.builder()
.currentBook(bookMapper.toBook(bookEntity))
.previousBookId(previousBookEntity != null ? previousBookEntity.getId() : null)
.nextBookId(nextBookEntity != null ? nextBookEntity.getId() : null)
.build();
}
@Transactional
public List<Book> assignShelvesToBooks(Set<Long> bookIds, Set<Long> shelfIdsToAssign, Set<Long> shelfIdsToUnassign) {
List<BookEntity> bookEntities = bookRepository.findAllById(bookIds);
@@ -72,14 +72,22 @@ CREATE TABLE IF NOT EXISTS category
);
-- Create the book_viewer_setting table
CREATE TABLE IF NOT EXISTS book_viewer_setting
CREATE TABLE IF NOT EXISTS pdf_viewer_preference
(
book_id BIGINT PRIMARY KEY,
page_number INT NULL,
zoom VARCHAR(16) NULL,
sidebar_visible BOOLEAN NULL,
spread VARCHAR(16) NULL,
CONSTRAINT fk_book_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
CONSTRAINT fk_pdf_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS epub_viewer_preference
(
book_id BIGINT PRIMARY KEY,
theme VARCHAR(128) NULL,
font VARCHAR(128) NULL,
font_size TINYINT NULL,
CONSTRAINT fk_epub_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);
-- Create the book_metadata_category_mapping table
@@ -131,6 +139,6 @@ CREATE TABLE app_settings
id BIGINT AUTO_INCREMENT PRIMARY KEY,
category VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
val TEXT NOT NULL,
val TEXT NOT NULL,
UNIQUE (category, name)
);