From f2899b968cbb2a2473c54a7c7d516202818710ca Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Tue, 7 Apr 2026 16:16:11 +0200 Subject: [PATCH] Use usual book widget for collection form view --- front/src/CollectionForm.vue | 4 +-- front/src/CollectionFormBookItem.vue | 37 ---------------------- internal/adapter/adapter.go | 17 ++-------- internal/dto/out.go | 8 ++--- internal/query/query.go | 47 ++++++++++++++++++++++------ internal/routes/collectionget.go | 33 ++++++++++--------- 6 files changed, 63 insertions(+), 83 deletions(-) delete mode 100644 front/src/CollectionFormBookItem.vue diff --git a/front/src/CollectionForm.vue b/front/src/CollectionForm.vue index dbd9042..163fce9 100644 --- a/front/src/CollectionForm.vue +++ b/front/src/CollectionForm.vue @@ -1,7 +1,7 @@ - - - - diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 75f0318..4ff2ad6 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -10,19 +10,6 @@ import ( "gorm.io/gorm" ) -func CollectionQueryToCollectionDto(collectionsQueryResult []query.CollectionsQueryResult) dto.CollectionGet { - var collection dto.CollectionGet - for _, collectionsDb := range collectionsQueryResult { - collection.Name = collectionsDb.Name - book := collectionDbToBookItem(&collectionsDb) - if book != nil { - collection.Books = append(collection.Books, *book) - } - collection.UserID = collectionsDb.UserID - } - return collection -} - func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.CollectionsQueryResult) []dto.CollectionItemGet { var collections []dto.CollectionItemGet for _, collectionDb := range collectionsQueryResult { @@ -35,7 +22,7 @@ func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.Collectio //current collection is the last element i = len(collections) - 1 } - book := collectionDbToBookItem(&collectionDb) + book := collectionDbToCollectionBookItem(&collectionDb) if book != nil { collections[i].Books = append(collections[i].Books, *book) } @@ -43,7 +30,7 @@ func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.Collectio return collections } -func collectionDbToBookItem(collectionDb *query.CollectionsQueryResult) *dto.CollectionBookItemGet { +func collectionDbToCollectionBookItem(collectionDb *query.CollectionsQueryResult) *dto.CollectionBookItemGet { if collectionDb.BookId > 0 { bookItem := dto.CollectionBookItemGet{ ID: collectionDb.BookId, diff --git a/internal/dto/out.go b/internal/dto/out.go index b58f89f..ae9d6b9 100644 --- a/internal/dto/out.go +++ b/internal/dto/out.go @@ -45,10 +45,10 @@ type BookItemGet struct { } type CollectionGet struct { - Name string `json:"name"` - Count int64 `json:"count"` - Books []CollectionBookItemGet `json:"books"` - UserID uint `json:"-"` + Name string `json:"name"` + Count int64 `json:"count"` + Books []BookItemGet `json:"books"` + UserID uint `json:"-"` } type CollectionItemsGet struct { diff --git a/internal/query/query.go b/internal/query/query.go index 3047ec5..827d75b 100644 --- a/internal/query/query.go +++ b/internal/query/query.go @@ -184,6 +184,16 @@ 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 @@ -206,7 +216,7 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col } for _, collectionId := range collectionIds { //only takes first 5 books - queryResults, err := FetchCollectionBooks(db, collectionId.ID, 5, 0) + queryResults, err := fetchCollectionItemBook(db, collectionId.ID, 5, 0) if err != nil { return collections, res.Error } @@ -215,22 +225,16 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col return collections, res.Error } -func FetchCollectionBooks(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) { +func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) { var collections []CollectionsQueryResult - query := fetchCollectionBooksQuery(db, collectionId) + query := fetchCollectionItemBooksQuery(db, collectionId) query = query.Limit(limit) query = query.Offset(offset) res := query.Find(&collections) return collections, res.Error } -func FetchCollectionBooksCount(db *gorm.DB, collectionId uint) (int64, error) { - var count int64 - res := fetchCollectionBooksQuery(db, collectionId).Where("book_id IS NOT NULL").Count(&count) - return count, res.Error -} - -func fetchCollectionBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB { +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)") @@ -250,6 +254,29 @@ 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/routes/collectionget.go b/internal/routes/collectionget.go index 64ea0ab..0645e29 100644 --- a/internal/routes/collectionget.go +++ b/internal/routes/collectionget.go @@ -5,7 +5,6 @@ import ( "net/http" "strconv" - "git.artlef.fr/bibliomane/internal/adapter" "git.artlef.fr/bibliomane/internal/appcontext" "git.artlef.fr/bibliomane/internal/i18nresource" "git.artlef.fr/bibliomane/internal/myvalidator" @@ -36,18 +35,7 @@ func GetCollectionHandler(ac appcontext.AppContext) { myvalidator.ReturnErrorsAsJsonResponse(&ac, err) return } - collectionBooksDb, err := query.FetchCollectionBooks(ac.Db, uint(collectionId), limit, offset) - if err != nil { - myvalidator.ReturnErrorsAsJsonResponse(&ac, err) - return - } - collection := adapter.CollectionQueryToCollectionDto(collectionBooksDb) - count, err := query.FetchCollectionBooksCount(ac.Db, uint(collectionId)) - if err != nil { - myvalidator.ReturnErrorsAsJsonResponse(&ac, err) - return - } - collection.Count = count + collection, err := query.FetchCollectionHeader(ac.Db, uint(collectionId)) if collection.UserID != user.ID { err := myvalidator.HttpError{ StatusCode: http.StatusUnauthorized, @@ -55,7 +43,22 @@ func GetCollectionHandler(ac appcontext.AppContext) { } myvalidator.ReturnErrorsAsJsonResponse(&ac, err) return - } else { - ac.C.JSON(http.StatusOK, collection) } + if err != nil { + myvalidator.ReturnErrorsAsJsonResponse(&ac, err) + return + } + books, err := query.FetchCollectionBooks(ac.Db, user.ID, uint(collectionId), limit, offset) + if err != nil { + myvalidator.ReturnErrorsAsJsonResponse(&ac, err) + return + } + collection.Books = books + count, err := query.FetchCollectionBooksCount(ac.Db, user.ID, uint(collectionId)) + if err != nil { + myvalidator.ReturnErrorsAsJsonResponse(&ac, err) + return + } + collection.Count = count + ac.C.JSON(http.StatusOK, collection) }