Refactor collection header query to remove warning

This commit is contained in:
2026-04-08 15:35:00 +02:00
parent dbf0face76
commit aca2a2c339
3 changed files with 14 additions and 8 deletions

View File

@@ -45,10 +45,9 @@ type BookItemGet struct {
} }
type CollectionGet struct { type CollectionGet struct {
Name string `json:"name"` Name string `json:"name"`
Count int64 `json:"count"` Count int64 `json:"count"`
Books []BookItemGet `json:"books"` Books []BookItemGet `json:"books"`
UserID uint `json:"-"`
} }
type CollectionItemsGet struct { type CollectionItemsGet struct {

View File

@@ -6,9 +6,14 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type CollectionHeader struct {
Name string
UserID uint
}
// collection header without the books // collection header without the books
func FetchCollectionHeader(db *gorm.DB, collectionId uint) (dto.CollectionGet, error) { func FetchCollectionHeader(db *gorm.DB, collectionId uint) (CollectionHeader, error) {
var collection dto.CollectionGet var collection CollectionHeader
query := db.Model(&model.Collection{}) query := db.Model(&model.Collection{})
query = query.Select("collections.name, collections.user_id") query = query.Select("collections.name, collections.user_id")
query = query.Where("collections.id = ?", collectionId) query = query.Where("collections.id = ?", collectionId)

View File

@@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"git.artlef.fr/bibliomane/internal/appcontext" "git.artlef.fr/bibliomane/internal/appcontext"
"git.artlef.fr/bibliomane/internal/dto"
"git.artlef.fr/bibliomane/internal/i18nresource" "git.artlef.fr/bibliomane/internal/i18nresource"
"git.artlef.fr/bibliomane/internal/myvalidator" "git.artlef.fr/bibliomane/internal/myvalidator"
"git.artlef.fr/bibliomane/internal/query" "git.artlef.fr/bibliomane/internal/query"
@@ -35,8 +36,8 @@ func GetCollectionHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return return
} }
collection, err := query.FetchCollectionHeader(ac.Db, uint(collectionId)) collectionHeader, err := query.FetchCollectionHeader(ac.Db, uint(collectionId))
if collection.UserID != user.ID { if collectionHeader.UserID != user.ID {
err := myvalidator.HttpError{ err := myvalidator.HttpError{
StatusCode: http.StatusUnauthorized, StatusCode: http.StatusUnauthorized,
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "Unauthorized")), Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "Unauthorized")),
@@ -44,6 +45,7 @@ func GetCollectionHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return return
} }
collection := dto.CollectionGet{Name: collectionHeader.Name}
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return return