Fix PDF setting not applying bug

This commit is contained in:
aditya.chandel
2025-03-07 23:18:34 -07:00
parent 39616dabd7
commit b0ae188310
19 changed files with 157 additions and 113 deletions
@@ -1,7 +1,10 @@
package com.adityachandel.booklore.config.security;
import com.adityachandel.booklore.model.dto.BookLoreUser;
import com.adityachandel.booklore.model.entity.EpubViewerPreferencesEntity;
import com.adityachandel.booklore.repository.PdfViewerPreferencesRepository;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
@@ -92,6 +92,8 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
bookLoreUser.setEmail(userEntity.getEmail());
bookLoreUser.setPermissions(permissions);
bookLoreUser.setBookPreferences(userEntity.getBookPreferences());
return bookLoreUser;
}
}
@@ -10,6 +10,7 @@ import lombok.AllArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@@ -19,6 +19,12 @@ public class UserController {
private final UserService userService;
@GetMapping("/me")
public ResponseEntity<BookLoreUser> getMyself() {
return ResponseEntity.ok(userService.getMyself());
}
@GetMapping("/{id}")
@PreAuthorize("@securityUtil.canViewUserProfile(#id)")
public ResponseEntity<BookLoreUser> getUser(@PathVariable Long id) {
@@ -40,12 +40,6 @@ public class BookEntity {
@JoinColumn(name = "library_path_id", nullable = false)
private LibraryPathEntity libraryPath;
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private PdfViewerPreferencesEntity pdfViewerPrefs;
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private EpubViewerPreferencesEntity epubViewerPrefs;
@Column(name = "added_on")
private Instant addedOn;
@@ -1,6 +1,5 @@
package com.adityachandel.booklore.model.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
@@ -10,16 +9,20 @@ import lombok.*;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "epub_viewer_preference")
@Table(name = "epub_viewer_preference", uniqueConstraints = {
@UniqueConstraint(columnNames = {"user_id", "book_id"})
})
public class EpubViewerPreferencesEntity {
@Id
private Long bookId;
@OneToOne
@MapsId
@JoinColumn(name = "book_id")
@JsonIgnore
private BookEntity book;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "book_id", nullable = false)
private Long bookId;
@Column(name = "theme")
private String theme;
@@ -1,6 +1,5 @@
package com.adityachandel.booklore.model.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
@@ -10,16 +9,20 @@ import lombok.*;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "pdf_viewer_preference")
@Table(name = "pdf_viewer_preference", uniqueConstraints = {
@UniqueConstraint(columnNames = {"user_id", "book_id"})
})
public class PdfViewerPreferencesEntity {
@Id
private Long bookId;
@OneToOne
@MapsId
@JoinColumn(name = "book_id")
@JsonIgnore
private BookEntity book;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "book_id", nullable = false)
private Long bookId;
@Column(name = "zoom")
private String zoom;
@@ -4,7 +4,11 @@ import com.adityachandel.booklore.model.entity.EpubViewerPreferencesEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface EpubViewerPreferencesRepository extends JpaRepository<EpubViewerPreferencesEntity, Long> {
Optional<EpubViewerPreferencesEntity> findByBookIdAndUserId(Long bookId, Long userId);
}
@@ -4,7 +4,10 @@ import com.adityachandel.booklore.model.entity.PdfViewerPreferencesEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface PdfViewerPreferencesRepository extends JpaRepository<PdfViewerPreferencesEntity, Long> {
Optional<PdfViewerPreferencesEntity> findByBookIdAndUserId(long bookId, Long id);
}
@@ -34,19 +34,6 @@ public class BookCreatorService {
.addedOn(Instant.now())
.build();
BookMetadataEntity bookMetadataEntity = BookMetadataEntity.builder().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 = bookRepository.saveAndFlush(bookEntity);
@@ -97,10 +84,5 @@ public class BookCreatorService {
}
bookRepository.save(bookEntity);
bookMetadataRepository.save(bookEntity.getMetadata());
if (bookEntity.getBookType() == BookFileType.EPUB) {
epubViewerPreferencesRepository.save(bookEntity.getEpubViewerPrefs());
} else if (bookEntity.getBookType() == BookFileType.PDF) {
pdfViewerPreferencesRepository.save(bookEntity.getPdfViewerPrefs());
}
}
}
@@ -1,10 +1,12 @@
package com.adityachandel.booklore.service;
import com.adityachandel.booklore.config.security.AuthenticationService;
import com.adityachandel.booklore.config.security.SecurityUtil;
import com.adityachandel.booklore.exception.ApiError;
import com.adityachandel.booklore.mapper.BookMapper;
import com.adityachandel.booklore.mapper.EpubViewerPreferencesMapper;
import com.adityachandel.booklore.mapper.PdfViewerPreferencesMapper;
import com.adityachandel.booklore.model.BookPreferences;
import com.adityachandel.booklore.model.dto.*;
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
import com.adityachandel.booklore.model.entity.*;
@@ -30,6 +32,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@@ -47,36 +50,71 @@ public class BooksService {
private final UserRepository userRepository;
private final UserBookProgressRepository userBookProgressRepository;
private final AuthenticationService authenticationService;
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();
BookLoreUser user = authenticationService.getAuthenticatedUser();
BookViewerSettings.BookViewerSettingsBuilder settingsBuilder = BookViewerSettings.builder();
if (bookEntity.getBookType() == BookFileType.EPUB) {
epubViewerPreferencesRepository.findByBookIdAndUserId(bookId, user.getId())
.ifPresent(epubPref -> settingsBuilder.epubSettings(EpubViewerPreferences.builder()
.bookId(bookId)
.font(epubPref.getFont())
.fontSize(epubPref.getFontSize())
.theme(epubPref.getTheme())
.build()));
} else if (bookEntity.getBookType() == BookFileType.PDF) {
pdfViewerPreferencesRepository.findByBookIdAndUserId(bookId, user.getId())
.ifPresent(pdfPref -> settingsBuilder.pdfSettings(PdfViewerPreferences.builder()
.bookId(bookId)
.zoom(pdfPref.getZoom())
.sidebarVisible(pdfPref.getSidebarVisible())
.spread(pdfPref.getSpread())
.build()));
} else {
throw ApiError.UNSUPPORTED_BOOK_TYPE.createException();
}
return settingsBuilder.build();
}
public void updateBookViewerSetting(long bookId, BookViewerSettings bookViewerSettings) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
BookLoreUser user = authenticationService.getAuthenticatedUser();
if (bookEntity.getBookType() == BookFileType.PDF) {
PdfViewerPreferencesEntity pdfPrefs = pdfViewerPreferencesRepository
.findByBookIdAndUserId(bookId, user.getId())
.orElseGet(() -> {
PdfViewerPreferencesEntity newPrefs = PdfViewerPreferencesEntity.builder()
.bookId(bookId)
.userId(user.getId())
.build();
return pdfViewerPreferencesRepository.save(newPrefs);
});
PdfViewerPreferences pdfSettings = bookViewerSettings.getPdfSettings();
PdfViewerPreferencesEntity viewerPrefs = bookEntity.getPdfViewerPrefs();
viewerPrefs.setZoom(pdfSettings.getZoom());
viewerPrefs.setSpread(pdfSettings.getSpread());
viewerPrefs.setSidebarVisible(pdfSettings.getSidebarVisible());
pdfViewerPreferencesRepository.save(viewerPrefs);
pdfPrefs.setZoom(pdfSettings.getZoom());
pdfPrefs.setSpread(pdfSettings.getSpread());
pdfPrefs.setSidebarVisible(pdfSettings.getSidebarVisible());
pdfViewerPreferencesRepository.save(pdfPrefs);
} else if (bookEntity.getBookType() == BookFileType.EPUB) {
EpubViewerPreferencesEntity epubPrefs = epubViewerPreferencesRepository
.findByBookIdAndUserId(bookId, user.getId())
.orElseGet(() -> {
EpubViewerPreferencesEntity newPrefs = EpubViewerPreferencesEntity.builder()
.bookId(bookId)
.userId(user.getId())
.build();
return epubViewerPreferencesRepository.save(newPrefs);
});
EpubViewerPreferences epubSettings = bookViewerSettings.getEpubSettings();
EpubViewerPreferencesEntity viewerPrefs = bookEntity.getEpubViewerPrefs();
viewerPrefs.setFont(epubSettings.getFont());
viewerPrefs.setFontSize(epubSettings.getFontSize());
viewerPrefs.setTheme(epubSettings.getTheme());
epubViewerPreferencesRepository.save(viewerPrefs);
epubPrefs.setFont(epubSettings.getFont());
epubPrefs.setFontSize(epubSettings.getFontSize());
epubPrefs.setTheme(epubSettings.getTheme());
epubViewerPreferencesRepository.save(epubPrefs);
} else {
throw ApiError.UNSUPPORTED_BOOK_TYPE.createException();
}
@@ -1,5 +1,6 @@
package com.adityachandel.booklore.service.user;
import com.adityachandel.booklore.config.security.AuthenticationService;
import com.adityachandel.booklore.config.security.JwtUtils;
import com.adityachandel.booklore.exception.ApiError;
import com.adityachandel.booklore.mapper.BookLoreUserMapper;
@@ -31,6 +32,7 @@ public class UserService {
private final PasswordEncoder passwordEncoder;
private final LibraryRepository libraryRepository;
private final JwtUtils jwtUtils;
private final AuthenticationService authenticationService;
public ResponseEntity<Map<String, String>> loginUser(UserLoginRequest loginRequest) {
BookLoreUserEntity user = userRepository.findByUsername(loginRequest.getUsername()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(loginRequest.getUsername()));
@@ -98,4 +100,8 @@ public class UserService {
userRepository.save(user);
}
public BookLoreUser getMyself() {
BookLoreUser user = authenticationService.getAuthenticatedUser();
return user;
}
}
@@ -101,20 +101,24 @@ CREATE TABLE IF NOT EXISTS category
CREATE TABLE IF NOT EXISTS pdf_viewer_preference
(
book_id BIGINT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL,
book_id BIGINT NOT NULL,
zoom VARCHAR(16) NULL,
sidebar_visible BOOLEAN NULL,
spread VARCHAR(16) NULL,
CONSTRAINT fk_pdf_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
UNIQUE (user_id, book_id)
);
CREATE TABLE IF NOT EXISTS epub_viewer_preference
(
book_id BIGINT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL,
book_id BIGINT NOT NULL,
theme VARCHAR(128) NULL,
font VARCHAR(128) NULL,
font_size INT NULL,
CONSTRAINT fk_epub_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
UNIQUE (user_id, book_id)
);
CREATE TABLE IF NOT EXISTS book_metadata_category_mapping