From dbf0face76d24dd0ae88572475b30588d3247586 Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Wed, 8 Apr 2026 15:26:40 +0200 Subject: [PATCH] Split query.go in two files --- internal/query/{query.go => querybooks.go} | 93 ------------------- internal/query/querycollections.go | 100 +++++++++++++++++++++ 2 files changed, 100 insertions(+), 93 deletions(-) rename internal/query/{query.go => querybooks.go} (67%) create mode 100644 internal/query/querycollections.go diff --git a/internal/query/query.go b/internal/query/querybooks.go similarity index 67% rename from internal/query/query.go rename to internal/query/querybooks.go index e441607..fca5cf9 100644 --- a/internal/query/query.go +++ b/internal/query/querybooks.go @@ -184,99 +184,6 @@ func selectBookItem() string { return "books.id, books.title, authors.name as author, books.short_description as description, books.inventaire_id, user_books.rating, user_books.read, DATE(user_books.start_read_date) as start_read_date, user_books.want_read, " + selectStaticFilesPath() } -// collection header without the books -func FetchCollectionHeader(db *gorm.DB, collectionId uint) (dto.CollectionGet, error) { - var collection dto.CollectionGet - query := db.Model(&model.Collection{}) - query = query.Select("collections.name, collections.user_id") - query = query.Where("collections.id = ?", collectionId) - res := query.Find(&collection) - return collection, res.Error -} - -type CollectionsQueryResult struct { - ID uint - UserID uint - Name string - BookId uint - BookTitle string - CoverPath string -} - -type collectionId struct { - ID uint -} - -func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]CollectionsQueryResult, error) { - var collections []CollectionsQueryResult - var collectionIds []collectionId - res := fetchCollections(db, userId).Limit(limit).Offset(offset).Order("collections.id DESC").Find(&collectionIds) - if res.Error != nil { - return collections, res.Error - } - for _, collectionId := range collectionIds { - //only takes first 8 books - queryResults, err := fetchCollectionItemBook(db, collectionId.ID, 8, 0) - if err != nil { - return collections, res.Error - } - collections = append(collections, queryResults...) - } - return collections, res.Error -} - -func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) { - var collections []CollectionsQueryResult - query := fetchCollectionItemBooksQuery(db, collectionId) - query = query.Limit(limit) - query = query.Offset(offset) - res := query.Find(&collections) - return collections, res.Error -} - -func fetchCollectionItemBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB { - query := db.Model(&model.Collection{}) - query = query.Select("collections.id, collections.user_id, collections.name, books.id as book_id, books.title as book_title, " + selectStaticFilesPath()) - query = query.Joins("left join collection_books on (collection_books.collection_id = collections.id)") - query = query.Joins("left join books on (books.id = collection_books.book_id)") - query = joinStaticFiles(query) - query = query.Where("collections.id = ?", collectionId) - return query -} - -func FetchAllCollectionsCount(db *gorm.DB, userId uint) (int64, error) { - var count int64 - res := fetchCollections(db, userId).Count(&count) - return count, res.Error -} - -func fetchCollections(db *gorm.DB, userId uint) *gorm.DB { - return db.Model(&model.Collection{}).Where("collections.user_id = ?", userId) -} - -func FetchCollectionBooks(db *gorm.DB, userId uint, collectionId uint, limit int, offset int) ([]dto.BookItemGet, error) { - var books []dto.BookItemGet - query := fetchCollectionBooksQuery(db, userId, collectionId) - query = query.Limit(limit) - query = query.Offset(offset) - query = query.Order("books.id DESC") - res := query.Find(&books) - return books, res.Error -} - -func FetchCollectionBooksCount(db *gorm.DB, userId uint, collectionId uint) (int64, error) { - var count int64 - res := fetchCollectionBooksQuery(db, userId, collectionId).Count(&count) - return count, res.Error -} - -func fetchCollectionBooksQuery(db *gorm.DB, userId uint, collectionId uint) *gorm.DB { - query := fetchBookQueryBuilder(db, userId) - query = query.Joins("left join collection_books on (collection_books.book_id = books.id)") - query = query.Where("collection_books.collection_id = ?", collectionId) - return query -} - func selectStaticFilesPath() string { return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath" } diff --git a/internal/query/querycollections.go b/internal/query/querycollections.go new file mode 100644 index 0000000..418ce19 --- /dev/null +++ b/internal/query/querycollections.go @@ -0,0 +1,100 @@ +package query + +import ( + "git.artlef.fr/bibliomane/internal/dto" + "git.artlef.fr/bibliomane/internal/model" + "gorm.io/gorm" +) + +// collection header without the books +func FetchCollectionHeader(db *gorm.DB, collectionId uint) (dto.CollectionGet, error) { + var collection dto.CollectionGet + query := db.Model(&model.Collection{}) + query = query.Select("collections.name, collections.user_id") + query = query.Where("collections.id = ?", collectionId) + res := query.Find(&collection) + return collection, res.Error +} + +type CollectionsQueryResult struct { + ID uint + UserID uint + Name string + BookId uint + BookTitle string + CoverPath string +} + +type collectionId struct { + ID uint +} + +func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]CollectionsQueryResult, error) { + var collections []CollectionsQueryResult + var collectionIds []collectionId + res := fetchCollections(db, userId).Limit(limit).Offset(offset).Order("collections.id DESC").Find(&collectionIds) + if res.Error != nil { + return collections, res.Error + } + for _, collectionId := range collectionIds { + //only takes first 8 books + queryResults, err := fetchCollectionItemBook(db, collectionId.ID, 8, 0) + if err != nil { + return collections, res.Error + } + collections = append(collections, queryResults...) + } + return collections, res.Error +} + +func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) { + var collections []CollectionsQueryResult + query := fetchCollectionItemBooksQuery(db, collectionId) + query = query.Limit(limit) + query = query.Offset(offset) + res := query.Find(&collections) + return collections, res.Error +} + +func fetchCollectionItemBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB { + query := db.Model(&model.Collection{}) + query = query.Select("collections.id, collections.user_id, collections.name, books.id as book_id, books.title as book_title, " + selectStaticFilesPath()) + query = query.Joins("left join collection_books on (collection_books.collection_id = collections.id)") + query = query.Joins("left join books on (books.id = collection_books.book_id)") + query = joinStaticFiles(query) + query = query.Where("collections.id = ?", collectionId) + return query +} + +func FetchAllCollectionsCount(db *gorm.DB, userId uint) (int64, error) { + var count int64 + res := fetchCollections(db, userId).Count(&count) + return count, res.Error +} + +func fetchCollections(db *gorm.DB, userId uint) *gorm.DB { + return db.Model(&model.Collection{}).Where("collections.user_id = ?", userId) +} + +func FetchCollectionBooks(db *gorm.DB, userId uint, collectionId uint, limit int, offset int) ([]dto.BookItemGet, error) { + var books []dto.BookItemGet + query := fetchCollectionBooksQuery(db, userId, collectionId) + query = query.Limit(limit) + query = query.Offset(offset) + query = query.Order("books.id DESC") + res := query.Find(&books) + return books, res.Error +} + +func FetchCollectionBooksCount(db *gorm.DB, userId uint, collectionId uint) (int64, error) { + var count int64 + res := fetchCollectionBooksQuery(db, userId, collectionId).Count(&count) + return count, res.Error +} + +func fetchCollectionBooksQuery(db *gorm.DB, userId uint, collectionId uint) *gorm.DB { + query := fetchBookQueryBuilder(db, userId) + query = query.Joins("left join collection_books on (collection_books.book_id = books.id)") + query = query.Where("collection_books.collection_id = ?", collectionId) + return query +}