Create new collections from my collections view

This commit is contained in:
2026-04-03 15:51:18 +02:00
parent b1bad80426
commit b48ab1e4de
11 changed files with 185 additions and 27 deletions

View File

@@ -0,0 +1,41 @@
package routes
import (
"net/http"
"git.artlef.fr/bibliomane/internal/appcontext"
"git.artlef.fr/bibliomane/internal/dto"
"git.artlef.fr/bibliomane/internal/model"
"git.artlef.fr/bibliomane/internal/myvalidator"
"github.com/gin-gonic/gin"
)
func PostCollectionHandler(ac appcontext.AppContext) {
var collection dto.CollectionFields
err := ac.C.ShouldBindJSON(&collection)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
user, fetchUserErr := ac.GetAuthenticatedUser()
if fetchUserErr != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
id, err := saveCollectionToDb(ac, &collection, &user)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
ac.C.JSON(http.StatusOK, gin.H{"id": id})
}
func saveCollectionToDb(ac appcontext.AppContext, c *dto.CollectionFields, user *model.User) (uint, error) {
collection := model.Collection{
Name: c.Name,
User: *user,
}
err := ac.Db.Save(&collection).Error
return collection.ID, err
}