Per user and book progress
This commit is contained in:
@@ -46,18 +46,9 @@ public class BookEntity {
|
|||||||
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToOne(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
private EpubViewerPreferencesEntity epubViewerPrefs;
|
private EpubViewerPreferencesEntity epubViewerPrefs;
|
||||||
|
|
||||||
@Column(name = "last_read_time")
|
|
||||||
private Instant lastReadTime;
|
|
||||||
|
|
||||||
@Column(name = "added_on")
|
@Column(name = "added_on")
|
||||||
private Instant addedOn;
|
private Instant addedOn;
|
||||||
|
|
||||||
@Column(name = "epub_progress")
|
|
||||||
private String epubProgress;
|
|
||||||
|
|
||||||
@Column(name = "pdf_progress")
|
|
||||||
private Integer pdfProgress;
|
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "book_shelf_mapping",
|
name = "book_shelf_mapping",
|
||||||
|
|||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package com.adityachandel.booklore.model.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Table(name = "user_book_progress")
|
||||||
|
public class UserBookProgressEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "user_id", nullable = false)
|
||||||
|
private BookLoreUserEntity user;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "book_id", nullable = false)
|
||||||
|
private BookEntity book;
|
||||||
|
|
||||||
|
@Column(name = "last_read_time")
|
||||||
|
private Instant lastReadTime;
|
||||||
|
|
||||||
|
@Column(name = "pdf_progress")
|
||||||
|
private Integer pdfProgress;
|
||||||
|
|
||||||
|
@Column(name = "epub_progress", length = 1000)
|
||||||
|
private String epubProgress;
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package com.adityachandel.booklore.repository;
|
||||||
|
|
||||||
|
import com.adityachandel.booklore.model.entity.UserBookProgressEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserBookProgressRepository extends JpaRepository<UserBookProgressEntity, Long> {
|
||||||
|
|
||||||
|
Optional<UserBookProgressEntity> findByUserIdAndBookId(Long userId, Long bookId);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -45,6 +45,7 @@ public class BooksService {
|
|||||||
private final FileService fileService;
|
private final FileService fileService;
|
||||||
private final BookMapper bookMapper;
|
private final BookMapper bookMapper;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final UserBookProgressRepository userBookProgressRepository;
|
||||||
private final AuthenticationService authenticationService;
|
private final AuthenticationService authenticationService;
|
||||||
private final PdfViewerPreferencesMapper pdfViewerPreferencesMapper;
|
private final PdfViewerPreferencesMapper pdfViewerPreferencesMapper;
|
||||||
private final EpubViewerPreferencesMapper epubViewerPreferencesMapper;
|
private final EpubViewerPreferencesMapper epubViewerPreferencesMapper;
|
||||||
@@ -82,8 +83,13 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Book getBook(long bookId, boolean withDescription) {
|
public Book getBook(long bookId, boolean withDescription) {
|
||||||
|
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||||
|
UserBookProgressEntity userProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), bookId).orElse(new UserBookProgressEntity());
|
||||||
Book book = bookMapper.toBook(bookEntity);
|
Book book = bookMapper.toBook(bookEntity);
|
||||||
|
book.setLastReadTime(userProgress.getLastReadTime());
|
||||||
|
book.setPdfProgress(userProgress.getPdfProgress());
|
||||||
|
book.setEpubProgress(userProgress.getEpubProgress());
|
||||||
if (!withDescription) {
|
if (!withDescription) {
|
||||||
book.getMetadata().setDescription(null);
|
book.getMetadata().setDescription(null);
|
||||||
}
|
}
|
||||||
@@ -132,15 +138,20 @@ public class BooksService {
|
|||||||
return fileService.getBookCover(bookEntity.getMetadata().getThumbnail());
|
return fileService.getBookCover(bookEntity.getMetadata().getThumbnail());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public void updateReadProgress(ReadProgressRequest request) {
|
public void updateReadProgress(ReadProgressRequest request) {
|
||||||
BookEntity book = bookRepository.findById(request.getBookId()).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
|
BookEntity book = bookRepository.findById(request.getBookId()).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
|
||||||
book.setLastReadTime(Instant.now());
|
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||||
if (book.getBookType() == BookFileType.EPUB) {
|
UserBookProgressEntity userBookProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), book.getId()).orElse(new UserBookProgressEntity());
|
||||||
book.setEpubProgress(request.getEpubProgress());
|
userBookProgress.setUser(userRepository.findById(user.getId()).orElseThrow(() -> new UsernameNotFoundException("User not found")));
|
||||||
} else if (book.getBookType() == BookFileType.PDF) {
|
userBookProgress.setBook(book);
|
||||||
book.setPdfProgress(request.getPdfProgress());
|
userBookProgress.setLastReadTime(Instant.now());
|
||||||
|
if (book.getBookType() == BookFileType.EPUB && request.getEpubProgress() != null) {
|
||||||
|
userBookProgress.setEpubProgress(request.getEpubProgress());
|
||||||
|
} else if (book.getBookType() == BookFileType.PDF && request.getPdfProgress() != null) {
|
||||||
|
userBookProgress.setPdfProgress(request.getPdfProgress());
|
||||||
}
|
}
|
||||||
bookRepository.save(book);
|
userBookProgressRepository.save(userBookProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<Resource> downloadBook(Long bookId) {
|
public ResponseEntity<Resource> downloadBook(Long bookId) {
|
||||||
|
|||||||
+16
-4
@@ -24,9 +24,6 @@ CREATE TABLE IF NOT EXISTS book
|
|||||||
library_id BIGINT NOT NULL,
|
library_id BIGINT NOT NULL,
|
||||||
library_path_id BIGINT NOT NULL,
|
library_path_id BIGINT NOT NULL,
|
||||||
added_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
added_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
last_read_time TIMESTAMP NULL,
|
|
||||||
pdf_progress INT NULL,
|
|
||||||
epub_progress VARCHAR(1000) NULL,
|
|
||||||
|
|
||||||
CONSTRAINT fk_library FOREIGN KEY (library_id) REFERENCES library (id) ON DELETE CASCADE,
|
CONSTRAINT fk_library FOREIGN KEY (library_id) REFERENCES library (id) ON DELETE CASCADE,
|
||||||
CONSTRAINT fk_library_path_id FOREIGN KEY (library_path_id) REFERENCES library_path (id) ON DELETE CASCADE,
|
CONSTRAINT fk_library_path_id FOREIGN KEY (library_path_id) REFERENCES library_path (id) ON DELETE CASCADE,
|
||||||
@@ -34,7 +31,6 @@ CREATE TABLE IF NOT EXISTS book
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_library_id ON book (library_id);
|
CREATE INDEX IF NOT EXISTS idx_library_id ON book (library_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_last_read_time ON book (last_read_time);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS book_metadata
|
CREATE TABLE IF NOT EXISTS book_metadata
|
||||||
(
|
(
|
||||||
@@ -195,3 +191,19 @@ CREATE TABLE app_settings
|
|||||||
val TEXT NOT NULL,
|
val TEXT NOT NULL,
|
||||||
UNIQUE (category, name)
|
UNIQUE (category, name)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS user_book_progress
|
||||||
|
(
|
||||||
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
user_id BIGINT NOT NULL,
|
||||||
|
book_id BIGINT NOT NULL,
|
||||||
|
last_read_time TIMESTAMP NULL,
|
||||||
|
pdf_progress INT NULL,
|
||||||
|
epub_progress VARCHAR(1000) NULL,
|
||||||
|
CONSTRAINT fk_user_book_progress_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT fk_user_book_progress_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT unique_user_book_progress UNIQUE (user_id, book_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_user_book_progress_user ON user_book_progress (user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_user_book_progress_book ON user_book_progress (book_id);
|
||||||
Reference in New Issue
Block a user