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)
|
||||
private EpubViewerPreferencesEntity epubViewerPrefs;
|
||||
|
||||
@Column(name = "last_read_time")
|
||||
private Instant lastReadTime;
|
||||
|
||||
@Column(name = "added_on")
|
||||
private Instant addedOn;
|
||||
|
||||
@Column(name = "epub_progress")
|
||||
private String epubProgress;
|
||||
|
||||
@Column(name = "pdf_progress")
|
||||
private Integer pdfProgress;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
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 BookMapper bookMapper;
|
||||
private final UserRepository userRepository;
|
||||
private final UserBookProgressRepository userBookProgressRepository;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final PdfViewerPreferencesMapper pdfViewerPreferencesMapper;
|
||||
private final EpubViewerPreferencesMapper epubViewerPreferencesMapper;
|
||||
@@ -82,8 +83,13 @@ public class BooksService {
|
||||
}
|
||||
|
||||
public Book getBook(long bookId, boolean withDescription) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
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.setLastReadTime(userProgress.getLastReadTime());
|
||||
book.setPdfProgress(userProgress.getPdfProgress());
|
||||
book.setEpubProgress(userProgress.getEpubProgress());
|
||||
if (!withDescription) {
|
||||
book.getMetadata().setDescription(null);
|
||||
}
|
||||
@@ -132,15 +138,20 @@ public class BooksService {
|
||||
return fileService.getBookCover(bookEntity.getMetadata().getThumbnail());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
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());
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
UserBookProgressEntity userBookProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), book.getId()).orElse(new UserBookProgressEntity());
|
||||
userBookProgress.setUser(userRepository.findById(user.getId()).orElseThrow(() -> new UsernameNotFoundException("User not found")));
|
||||
userBookProgress.setBook(book);
|
||||
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) {
|
||||
|
||||
+22
-10
@@ -18,15 +18,12 @@ CREATE TABLE IF NOT EXISTS library_path
|
||||
CREATE TABLE IF NOT EXISTS book
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
file_name VARCHAR(255) NOT NULL,
|
||||
file_sub_path VARCHAR(512) NOT NULL,
|
||||
book_type VARCHAR(6) NOT NULL,
|
||||
library_id BIGINT NOT NULL,
|
||||
library_path_id BIGINT NOT NULL,
|
||||
file_name VARCHAR(255) NOT NULL,
|
||||
file_sub_path VARCHAR(512) NOT NULL,
|
||||
book_type VARCHAR(6) NOT NULL,
|
||||
library_id BIGINT NOT NULL,
|
||||
library_path_id BIGINT NOT NULL,
|
||||
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_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_last_read_time ON book (last_read_time);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS book_metadata
|
||||
(
|
||||
@@ -194,4 +190,20 @@ CREATE TABLE app_settings
|
||||
name VARCHAR(255) NOT NULL,
|
||||
val TEXT NOT NULL,
|
||||
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