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

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

View File

@@ -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)

View File

@@ -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