42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
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
|
|
}
|