67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package routes
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.artlef.fr/bibliomane/internal/appcontext"
|
|
"git.artlef.fr/bibliomane/internal/i18nresource"
|
|
"git.artlef.fr/bibliomane/internal/model"
|
|
"git.artlef.fr/bibliomane/internal/myvalidator"
|
|
"git.artlef.fr/bibliomane/internal/query"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func DeleteCollectionHandler(ac appcontext.AppContext) {
|
|
collectionId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
user, err := ac.GetAuthenticatedUser()
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
|
|
err = myvalidator.ValidateId(ac.Db, uint(collectionId), &model.Collection{})
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
|
|
collection, err := query.FetchCollectionHeader(ac.Db, uint(collectionId))
|
|
|
|
if collection.UserID != user.ID {
|
|
err := myvalidator.HttpError{
|
|
StatusCode: http.StatusUnauthorized,
|
|
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "Unauthorized")),
|
|
}
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
|
|
collectionDb := model.Collection{}
|
|
collectionDb.ID = uint(collectionId)
|
|
|
|
err = ac.Db.Where("collection_id = ?", collectionId).Delete(&model.CollectionItem{}).Error
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
err = ac.Db.Delete(&collectionDb).Error
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
err = ac.Db.Delete(&collectionDb).Error
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
|
|
ac.C.JSON(http.StatusOK, "Success")
|
|
}
|