findByBookIdAndBookType(Long bookId, BookFileType bookType);
/**
- * Finds a {@link BookAdditionalFileEntity} by its alternative format current hash.
+ * Finds a {@link BookFileEntity} by its alternative format current hash.
*
* This method queries against the {@code alt_format_current_hash} virtual column, which is indexed
* and only contains values for files where the {@code additional_file_type} is 'ALTERNATIVE_FORMAT'.
@@ -27,18 +30,32 @@ public interface BookAdditionalFileRepository extends JpaRepository findByAltFormatCurrentHash(String altFormatCurrentHash);
+ Optional findByAltFormatCurrentHash(String altFormatCurrentHash);
- @Query("SELECT af FROM BookAdditionalFileEntity af WHERE af.book.libraryPath.id = :libraryPathId AND af.fileSubPath = :fileSubPath AND af.fileName = :fileName")
- Optional findByLibraryPath_IdAndFileSubPathAndFileName(@Param("libraryPathId") Long libraryPathId,
+ @Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.libraryPath.id = :libraryPathId AND bf.fileSubPath = :fileSubPath AND bf.fileName = :fileName")
+ Optional findByLibraryPath_IdAndFileSubPathAndFileName(@Param("libraryPathId") Long libraryPathId,
@Param("fileSubPath") String fileSubPath,
@Param("fileName") String fileName);
- List findByAdditionalFileType(AdditionalFileType additionalFileType);
+ List findByIsBookFormat(boolean isBookFormat);
- @Query("SELECT COUNT(af) FROM BookAdditionalFileEntity af WHERE af.book.id = :bookId AND af.additionalFileType = :additionalFileType")
- long countByBookIdAndAdditionalFileType(@Param("bookId") Long bookId, @Param("additionalFileType") AdditionalFileType additionalFileType);
+ List findByBookType(BookFileType bookType);
- @Query("SELECT af FROM BookAdditionalFileEntity af WHERE af.book.library.id = :libraryId")
- List findByLibraryId(@Param("libraryId") Long libraryId);
+ @Query("SELECT COUNT(bf) FROM BookFileEntity bf WHERE bf.book.id = :bookId AND bf.isBookFormat = :isBookFormat")
+ long countByBookIdAndIsBookFormat(@Param("bookId") Long bookId, @Param("isBookFormat") boolean isBookFormat);
+
+ @Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.library.id = :libraryId")
+ List findByLibraryId(@Param("libraryId") Long libraryId);
+
+ @Modifying
+ @Query("""
+ UPDATE BookFileEntity bf SET
+ bf.fileName = :fileName,
+ bf.fileSubPath = :fileSubPath
+ WHERE bf.id = :bookFileId
+ """)
+ void updateFileNameAndSubPath(
+ @Param("bookFileId") Long bookFileId,
+ @Param("fileName") String fileName,
+ @Param("fileSubPath") String fileSubPath);
}
diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookNoteV2Repository.java b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookNoteV2Repository.java
new file mode 100644
index 000000000..92f96a945
--- /dev/null
+++ b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookNoteV2Repository.java
@@ -0,0 +1,34 @@
+package com.adityachandel.booklore.repository;
+
+import com.adityachandel.booklore.model.entity.BookNoteV2Entity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+import java.util.Optional;
+
+public interface BookNoteV2Repository extends JpaRepository {
+
+ Optional findByIdAndUserId(Long id, Long userId);
+
+ @Query("SELECT n FROM BookNoteV2Entity n WHERE n.bookId = :bookId AND n.userId = :userId ORDER BY n.createdAt DESC")
+ List findByBookIdAndUserIdOrderByCreatedAtDesc(
+ @Param("bookId") Long bookId,
+ @Param("userId") Long userId
+ );
+
+ boolean existsByCfiAndBookIdAndUserId(String cfi, Long bookId, Long userId);
+
+ @Query("SELECT COUNT(n) > 0 FROM BookNoteV2Entity n WHERE n.cfi = :cfi AND n.bookId = :bookId AND n.userId = :userId AND n.id != :excludeId")
+ boolean existsByCfiAndBookIdAndUserIdExcludeId(
+ @Param("cfi") String cfi,
+ @Param("bookId") Long bookId,
+ @Param("userId") Long userId,
+ @Param("excludeId") Long excludeId
+ );
+
+ long countByBookIdAndUserId(Long bookId, Long userId);
+
+ void deleteByBookIdAndUserId(Long bookId, Long userId);
+}
diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookOpdsRepository.java b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookOpdsRepository.java
index 4b93afa34..da51b00f7 100644
--- a/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookOpdsRepository.java
+++ b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookOpdsRepository.java
@@ -23,7 +23,7 @@ public interface BookOpdsRepository extends JpaRepository, Jpa
@Query("SELECT b.id FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false) ORDER BY b.addedOn DESC")
Page findBookIds(Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "additionalFiles", "shelves"})
+ @EntityGraph(attributePaths = {"metadata", "bookFiles", "shelves"})
@Query("SELECT b FROM BookEntity b WHERE b.id IN :ids AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByIds(@Param("ids") Collection ids);
@@ -43,7 +43,7 @@ public interface BookOpdsRepository extends JpaRepository, Jpa
@Query("SELECT b.id FROM BookEntity b WHERE b.library.id IN :libraryIds AND (b.deleted IS NULL OR b.deleted = false) ORDER BY b.addedOn DESC")
Page findBookIdsByLibraryIds(@Param("libraryIds") Collection libraryIds, Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "additionalFiles", "shelves"})
+ @EntityGraph(attributePaths = {"metadata", "bookFiles", "shelves"})
@Query("SELECT b FROM BookEntity b WHERE b.id IN :ids AND b.library.id IN :libraryIds AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByIdsAndLibraryIds(@Param("ids") Collection ids, @Param("libraryIds") Collection libraryIds);
@@ -63,7 +63,7 @@ public interface BookOpdsRepository extends JpaRepository, Jpa
@Query("SELECT DISTINCT b.id FROM BookEntity b JOIN b.shelves s WHERE s.id = :shelfId AND (b.deleted IS NULL OR b.deleted = false) ORDER BY b.addedOn DESC")
Page findBookIdsByShelfId(@Param("shelfId") Long shelfId, Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "additionalFiles", "shelves"})
+ @EntityGraph(attributePaths = {"metadata", "bookFiles", "shelves"})
@Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE b.id IN :ids AND s.id = :shelfId AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByIdsAndShelfId(@Param("ids") Collection ids, @Param("shelfId") Long shelfId);
@@ -81,7 +81,7 @@ public interface BookOpdsRepository extends JpaRepository, Jpa
""")
Page findBookIdsByMetadataSearch(@Param("text") String text, Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "additionalFiles", "shelves"})
+ @EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "bookFiles", "shelves"})
@Query("SELECT DISTINCT b FROM BookEntity b WHERE b.id IN :ids AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithFullMetadataByIds(@Param("ids") Collection ids);
@@ -101,10 +101,42 @@ public interface BookOpdsRepository extends JpaRepository, Jpa
""")
Page findBookIdsByMetadataSearchAndLibraryIds(@Param("text") String text, @Param("libraryIds") Collection libraryIds, Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "additionalFiles", "shelves"})
+ @EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "bookFiles", "shelves"})
@Query("SELECT DISTINCT b FROM BookEntity b WHERE b.id IN :ids AND b.library.id IN :libraryIds AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithFullMetadataByIdsAndLibraryIds(@Param("ids") Collection ids, @Param("libraryIds") Collection libraryIds);
+ // ============================================
+ // SEARCH BY METADATA IN SHELVES - Two Query Pattern
+ // ============================================
+
+ @Query("""
+ SELECT DISTINCT b.id FROM BookEntity b
+ LEFT JOIN b.metadata m
+ JOIN b.shelves s
+ WHERE (b.deleted IS NULL OR b.deleted = false)
+ AND s.id IN :shelfIds
+ AND (
+ m.searchText LIKE CONCAT('%', :text, '%')
+ )
+ ORDER BY b.addedOn DESC
+ """)
+ Page findBookIdsByMetadataSearchAndShelfIds(@Param("text") String text, @Param("shelfIds") Collection shelfIds, Pageable pageable);
+
+ @EntityGraph(attributePaths = {"metadata", "metadata.authors", "metadata.categories", "additionalFiles", "shelves"})
+ @Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE b.id IN :ids AND s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false)")
+ List findAllWithFullMetadataByIdsAndShelfIds(@Param("ids") Collection ids, @Param("shelfIds") Collection shelfIds);
+
+ // ============================================
+ // BOOKS BY SHELF IDs - Two Query Pattern
+ // ============================================
+
+ @Query("SELECT DISTINCT b.id FROM BookEntity b JOIN b.shelves s WHERE s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false) ORDER BY b.addedOn DESC")
+ Page findBookIdsByShelfIds(@Param("shelfIds") Collection shelfIds, Pageable pageable);
+
+ @EntityGraph(attributePaths = {"metadata", "additionalFiles", "shelves"})
+ @Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE b.id IN :ids AND s.id IN :shelfIds AND (b.deleted IS NULL OR b.deleted = false)")
+ List findAllWithMetadataByIdsAndShelfIds(@Param("ids") Collection ids, @Param("shelfIds") Collection shelfIds);
+
// ============================================
// RANDOM BOOKS - "Surprise Me" Feed
// ============================================
diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookRepository.java b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookRepository.java
index a360e48e4..5a3b003eb 100644
--- a/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookRepository.java
+++ b/booklore-api/src/main/java/com/adityachandel/booklore/repository/BookRepository.java
@@ -20,16 +20,22 @@ import java.util.Set;
public interface BookRepository extends JpaRepository, JpaSpecificationExecutor {
Optional findBookByIdAndLibraryId(long id, long libraryId);
- Optional findByCurrentHash(String currentHash);
+ @EntityGraph(attributePaths = { "metadata", "shelves", "libraryPath", "bookFiles" })
+ @Query("SELECT b FROM BookEntity b LEFT JOIN FETCH b.bookFiles bf WHERE b.id = :id AND (b.deleted IS NULL OR b.deleted = false)")
+ Optional findByIdWithBookFiles(@Param("id") Long id);
+
+ @Query("SELECT b FROM BookEntity b JOIN b.bookFiles bf WHERE bf.currentHash = :currentHash AND bf.isBookFormat = true AND (b.deleted IS NULL OR b.deleted = false)")
+ Optional findByCurrentHash(@Param("currentHash") String currentHash);
Optional findByBookCoverHash(String bookCoverHash);
@Query("SELECT b.id FROM BookEntity b WHERE b.library.id = :libraryId AND (b.deleted IS NULL OR b.deleted = false)")
Set findBookIdsByLibraryId(@Param("libraryId") long libraryId);
- List findAllByLibraryPathIdAndFileSubPathStartingWith(Long libraryPathId, String fileSubPathPrefix);
+ @Query("SELECT DISTINCT b FROM BookEntity b JOIN b.bookFiles bf WHERE b.libraryPath.id = :libraryPathId AND bf.fileSubPath LIKE CONCAT(:fileSubPathPrefix, '%') AND bf.isBookFormat = true AND (b.deleted IS NULL OR b.deleted = false)")
+ List findAllByLibraryPathIdAndFileSubPathStartingWith(@Param("libraryPathId") Long libraryPathId, @Param("fileSubPathPrefix") String fileSubPathPrefix);
- @Query("SELECT b FROM BookEntity b WHERE b.libraryPath.id = :libraryPathId AND b.fileSubPath = :fileSubPath AND b.fileName = :fileName AND (b.deleted IS NULL OR b.deleted = false)")
+ @Query("SELECT b FROM BookEntity b JOIN b.bookFiles bf WHERE b.libraryPath.id = :libraryPathId AND bf.fileSubPath = :fileSubPath AND bf.fileName = :fileName AND bf.isBookFormat = true AND (b.deleted IS NULL OR b.deleted = false)")
Optional findByLibraryPath_IdAndFileSubPathAndFileName(@Param("libraryPathId") Long libraryPathId,
@Param("fileSubPath") String fileSubPath,
@Param("fileName") String fileName);
@@ -37,32 +43,32 @@ public interface BookRepository extends JpaRepository, JpaSpec
@Query("SELECT b.id FROM BookEntity b WHERE b.libraryPath.id IN :libraryPathIds AND (b.deleted IS NULL OR b.deleted = false)")
List findAllBookIdsByLibraryPathIdIn(@Param("libraryPathIds") Collection libraryPathIds);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadata();
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE b.id IN :bookIds AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByIds(@Param("bookIds") Set bookIds);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE b.id IN :bookIds AND (b.deleted IS NULL OR b.deleted = false)")
List findWithMetadataByIdsWithPagination(@Param("bookIds") Set bookIds, Pageable pageable);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE b.library.id = :libraryId AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByLibraryId(@Param("libraryId") Long libraryId);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE b.library.id IN :libraryIds AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByLibraryIds(@Param("libraryIds") Collection libraryIds);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath", "bookFiles"})
@Query("SELECT DISTINCT b FROM BookEntity b JOIN b.shelves s WHERE s.id = :shelfId AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByShelfId(@Param("shelfId") Long shelfId);
- @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
- @Query("SELECT b FROM BookEntity b WHERE b.fileSizeKb IS NULL AND (b.deleted IS NULL OR b.deleted = false)")
+ @EntityGraph(attributePaths = { "metadata", "shelves", "libraryPath", "bookFiles" })
+ @Query("SELECT DISTINCT b FROM BookEntity b JOIN b.bookFiles bf WHERE bf.isBookFormat = true AND bf.fileSizeKb IS NULL AND (b.deleted IS NULL OR b.deleted = false)")
List findAllWithMetadataByFileSizeKbIsNull();
@Query("""
@@ -105,31 +111,17 @@ public interface BookRepository extends JpaRepository, JpaSpec
@Query("SELECT COUNT(b) FROM BookEntity b WHERE b.deleted = TRUE")
long countAllSoftDeleted();
- @Modifying
- @Query("""
- UPDATE BookEntity b
- SET b.fileSubPath = :fileSubPath,
- b.fileName = :fileName,
- b.library.id = :libraryId,
- b.libraryPath = :libraryPath
- WHERE b.id = :bookId
- """)
- void updateFileAndLibrary(
- @Param("bookId") Long bookId,
- @Param("fileSubPath") String fileSubPath,
- @Param("fileName") String fileName,
- @Param("libraryId") Long libraryId,
- @Param("libraryPath") LibraryPathEntity libraryPath);
-
@Query(value = """
- SELECT *
- FROM book
- WHERE library_id = :libraryId
- AND library_path_id = :libraryPathId
- AND file_sub_path = :fileSubPath
- AND file_name = :fileName
- LIMIT 1
- """, nativeQuery = true)
+ SELECT b.*
+ FROM book b
+ JOIN book_file bf ON bf.book_id = b.id
+ WHERE b.library_id = :libraryId
+ AND b.library_path_id = :libraryPathId
+ AND bf.file_sub_path = :fileSubPath
+ AND bf.file_name = :fileName
+ AND bf.is_book = true
+ LIMIT 1
+ """, nativeQuery = true)
Optional findByLibraryIdAndLibraryPathIdAndFileSubPathAndFileName(
@Param("libraryId") Long libraryId,
@Param("libraryPathId") Long libraryPathId,
@@ -139,7 +131,13 @@ public interface BookRepository extends JpaRepository, JpaSpec
@Query("SELECT COUNT(b.id) FROM BookEntity b WHERE b.id IN :bookIds AND (b.deleted IS NULL OR b.deleted = false)")
long countByIdIn(@Param("bookIds") List bookIds);
- @Query("SELECT COUNT(b) FROM BookEntity b WHERE b.bookType = :type AND (b.deleted IS NULL OR b.deleted = false)")
+ @Query("""
+ SELECT COUNT(DISTINCT b) FROM BookEntity b
+ JOIN b.bookFiles bf
+ WHERE bf.isBookFormat = true
+ AND bf.bookType = :type
+ AND (b.deleted IS NULL OR b.deleted = false)
+ """)
long countByBookType(@Param("type") BookFileType type);
@Query("SELECT COUNT(b) FROM BookEntity b WHERE b.library.id = :libraryId AND (b.deleted IS NULL OR b.deleted = false)")
@@ -147,4 +145,166 @@ public interface BookRepository extends JpaRepository, JpaSpec
@Query("SELECT b.id as id, m.coverUpdatedOn as coverUpdatedOn FROM BookEntity b LEFT JOIN b.metadata m WHERE b.id IN :bookIds")
List findCoverUpdateInfoByIds(@Param("bookIds") Collection bookIds);
+
+ @Modifying
+ @Query("""
+ UPDATE BookEntity b SET
+ b.library.id = :libraryId,
+ b.libraryPath = :libraryPath
+ WHERE b.id = :bookId
+ """)
+ void updateLibrary(
+ @Param("bookId") Long bookId,
+ @Param("libraryId") Long libraryId,
+ @Param("libraryPath") LibraryPathEntity libraryPath);
+
+ /**
+ * Get distinct series names for a library when groupUnknown=true.
+ * Books without series name are grouped as "Unknown Series".
+ */
+ @Query("""
+ SELECT DISTINCT
+ CASE
+ WHEN m.seriesName IS NOT NULL THEN m.seriesName
+ ELSE :unknownSeriesName
+ END as seriesName
+ FROM BookEntity b
+ LEFT JOIN b.metadata m
+ WHERE b.library.id = :libraryId
+ AND (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY seriesName
+ """)
+ List findDistinctSeriesNamesGroupedByLibraryId(
+ @Param("libraryId") Long libraryId,
+ @Param("unknownSeriesName") String unknownSeriesName);
+
+ /**
+ * Get distinct series names across all libraries when groupUnknown=true.
+ * Books without series name are grouped as "Unknown Series".
+ */
+ @Query("""
+ SELECT DISTINCT
+ CASE
+ WHEN m.seriesName IS NOT NULL THEN m.seriesName
+ ELSE :unknownSeriesName
+ END as seriesName
+ FROM BookEntity b
+ LEFT JOIN b.metadata m
+ WHERE (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY seriesName
+ """)
+ List findDistinctSeriesNamesGrouped(@Param("unknownSeriesName") String unknownSeriesName);
+
+ /**
+ * Get distinct series names for a library when groupUnknown=false.
+ * Each book without series gets its own entry (title or filename).
+ */
+ @Query("""
+ SELECT DISTINCT
+ CASE
+ WHEN m.seriesName IS NOT NULL THEN m.seriesName
+ WHEN m.title IS NOT NULL THEN m.title
+ ELSE (
+ SELECT bf2.fileName FROM BookFileEntity bf2
+ WHERE bf2.book = b
+ AND bf2.isBookFormat = true
+ AND bf2.id = (
+ SELECT MIN(bf3.id) FROM BookFileEntity bf3
+ WHERE bf3.book = b AND bf3.isBookFormat = true
+ )
+ )
+ END as seriesName
+ FROM BookEntity b
+ LEFT JOIN b.metadata m
+ WHERE b.library.id = :libraryId
+ AND (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY seriesName
+ """)
+ List findDistinctSeriesNamesUngroupedByLibraryId(@Param("libraryId") Long libraryId);
+
+ /**
+ * Get distinct series names across all libraries when groupUnknown=false.
+ * Each book without series gets its own entry (title or filename).
+ */
+ @Query("""
+ SELECT DISTINCT
+ CASE
+ WHEN m.seriesName IS NOT NULL THEN m.seriesName
+ WHEN m.title IS NOT NULL THEN m.title
+ ELSE (
+ SELECT bf2.fileName FROM BookFileEntity bf2
+ WHERE bf2.book = b
+ AND bf2.isBookFormat = true
+ AND bf2.id = (
+ SELECT MIN(bf3.id) FROM BookFileEntity bf3
+ WHERE bf3.book = b AND bf3.isBookFormat = true
+ )
+ )
+ END as seriesName
+ FROM BookEntity b
+ LEFT JOIN b.metadata m
+ WHERE (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY seriesName
+ """)
+ List findDistinctSeriesNamesUngrouped();
+
+ /**
+ * Find books by series name for a library when groupUnknown=true.
+ * Uses the first bookFile.fileName as fallback when metadata.seriesName is null.
+ */
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @Query("""
+ SELECT DISTINCT b FROM BookEntity b
+ LEFT JOIN b.metadata m
+ LEFT JOIN b.bookFiles bf
+ WHERE b.library.id = :libraryId
+ AND (
+ (m.seriesName = :seriesName)
+ OR (
+ m.seriesName IS NULL
+ AND bf.isBookFormat = true
+ AND bf.id = (
+ SELECT MIN(bf2.id) FROM BookFileEntity bf2
+ WHERE bf2.book = b AND bf2.isBookFormat = true
+ )
+ AND bf.fileName = :seriesName
+ )
+ )
+ AND (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY COALESCE(m.seriesNumber, 0)
+ """)
+ List findBooksBySeriesNameGroupedByLibraryId(
+ @Param("seriesName") String seriesName,
+ @Param("libraryId") Long libraryId,
+ @Param("unknownSeriesName") String unknownSeriesName);
+
+ /**
+ * Find books by series name for a library when groupUnknown=false.
+ * Matches by series name, or by title/filename for books without series.
+ */
+ @EntityGraph(attributePaths = {"metadata", "shelves", "libraryPath"})
+ @Query("""
+ SELECT b FROM BookEntity b
+ LEFT JOIN b.metadata m
+ LEFT JOIN b.bookFiles bf
+ WHERE b.library.id = :libraryId
+ AND (
+ (m.seriesName = :seriesName)
+ OR (m.seriesName IS NULL AND m.title = :seriesName)
+ OR (
+ m.seriesName IS NULL AND m.title IS NULL
+ AND bf.isBookFormat = true
+ AND bf.id = (
+ SELECT MIN(bf2.id) FROM BookFileEntity bf2
+ WHERE bf2.book = b AND bf2.isBookFormat = true
+ )
+ AND bf.fileName = :seriesName
+ )
+ )
+ AND (b.deleted IS NULL OR b.deleted = false)
+ ORDER BY COALESCE(m.seriesNumber, 0)
+ """)
+ List findBooksBySeriesNameUngroupedByLibraryId(
+ @Param("seriesName") String seriesName,
+ @Param("libraryId") Long libraryId);
}
diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/repository/EbookViewerPreferenceRepository.java b/booklore-api/src/main/java/com/adityachandel/booklore/repository/EbookViewerPreferenceRepository.java
new file mode 100644
index 000000000..c34281d52
--- /dev/null
+++ b/booklore-api/src/main/java/com/adityachandel/booklore/repository/EbookViewerPreferenceRepository.java
@@ -0,0 +1,15 @@
+package com.adityachandel.booklore.repository;
+
+import com.adityachandel.booklore.model.entity.EbookViewerPreferenceEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.Optional;
+
+@Repository
+public interface EbookViewerPreferenceRepository extends JpaRepository {
+
+ Optional findByBookIdAndUserId(Long bookId, Long userId);
+
+}
+
diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/repository/ReadingSessionRepository.java b/booklore-api/src/main/java/com/adityachandel/booklore/repository/ReadingSessionRepository.java
index 3b44029d1..596a6ca02 100644
--- a/booklore-api/src/main/java/com/adityachandel/booklore/repository/ReadingSessionRepository.java
+++ b/booklore-api/src/main/java/com/adityachandel/booklore/repository/ReadingSessionRepository.java
@@ -16,11 +16,11 @@ import java.util.List;
public interface ReadingSessionRepository extends JpaRepository {
@Query("""
- SELECT CAST(rs.createdAt AS LocalDate) as date, COUNT(rs) as count
+ SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count
FROM ReadingSessionEntity rs
WHERE rs.user.id = :userId
- AND YEAR(rs.createdAt) = :year
- GROUP BY CAST(rs.createdAt AS LocalDate)
+ AND YEAR(rs.startTime) = :year
+ GROUP BY CAST(rs.startTime AS LocalDate)
ORDER BY date
""")
List