diff --git a/internal/dto/out.go b/internal/dto/out.go index ae9d6b9..bb48407 100644 --- a/internal/dto/out.go +++ b/internal/dto/out.go @@ -45,10 +45,9 @@ type BookItemGet struct { } type CollectionGet struct { - Name string `json:"name"` - Count int64 `json:"count"` - Books []BookItemGet `json:"books"` - UserID uint `json:"-"` + Name string `json:"name"` + Count int64 `json:"count"` + Books []BookItemGet `json:"books"` } type CollectionItemsGet struct { diff --git a/internal/query/querycollections.go b/internal/query/querycollections.go index 418ce19..1f6f83f 100644 --- a/internal/query/querycollections.go +++ b/internal/query/querycollections.go @@ -6,9 +6,14 @@ import ( "gorm.io/gorm" ) +type CollectionHeader struct { + Name string + UserID uint +} + // collection header without the books -func FetchCollectionHeader(db *gorm.DB, collectionId uint) (dto.CollectionGet, error) { - var collection dto.CollectionGet +func FetchCollectionHeader(db *gorm.DB, collectionId uint) (CollectionHeader, error) { + var collection CollectionHeader query := db.Model(&model.Collection{}) query = query.Select("collections.name, collections.user_id") query = query.Where("collections.id = ?", collectionId) diff --git a/internal/routes/collectionget.go b/internal/routes/collectionget.go index 0645e29..2084681 100644 --- a/internal/routes/collectionget.go +++ b/internal/routes/collectionget.go @@ -6,6 +6,7 @@ import ( "strconv" "git.artlef.fr/bibliomane/internal/appcontext" + "git.artlef.fr/bibliomane/internal/dto" "git.artlef.fr/bibliomane/internal/i18nresource" "git.artlef.fr/bibliomane/internal/myvalidator" "git.artlef.fr/bibliomane/internal/query" @@ -35,8 +36,8 @@ func GetCollectionHandler(ac appcontext.AppContext) { myvalidator.ReturnErrorsAsJsonResponse(&ac, err) return } - collection, err := query.FetchCollectionHeader(ac.Db, uint(collectionId)) - if collection.UserID != user.ID { + collectionHeader, err := query.FetchCollectionHeader(ac.Db, uint(collectionId)) + if collectionHeader.UserID != user.ID { err := myvalidator.HttpError{ StatusCode: http.StatusUnauthorized, Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "Unauthorized")), @@ -44,6 +45,7 @@ func GetCollectionHandler(ac appcontext.AppContext) { myvalidator.ReturnErrorsAsJsonResponse(&ac, err) return } + collection := dto.CollectionGet{Name: collectionHeader.Name} if err != nil { myvalidator.ReturnErrorsAsJsonResponse(&ac, err) return