Collections browser: add a button to remove collections

This commit is contained in:
2026-04-25 17:39:01 +02:00
parent b48c42c40c
commit 2f5fc3d0a3
10 changed files with 192 additions and 9 deletions

View File

@@ -0,0 +1,51 @@
package apitest
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.artlef.fr/bibliomane/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestDeleteCollectionHandler_EmptyCollection(t *testing.T) {
collectionId := "8"
status := testDeleteCollectionHandler(collectionId)
assert.Equal(t, http.StatusOK, status)
getStatus, _ := testGetCollection(t, collectionId, "10", "0")
assert.Equal(t, http.StatusNotFound, getStatus)
}
func TestDeleteCollectionHandler_NotEmptyCollection(t *testing.T) {
collectionId := "7"
status := testDeleteCollectionHandler(collectionId)
assert.Equal(t, http.StatusOK, status)
getStatus, _ := testGetCollection(t, collectionId, "10", "0")
assert.Equal(t, http.StatusNotFound, getStatus)
}
func TestDeleteCollectionHandler_NonExistingCollection(t *testing.T) {
collectionId := "425"
status := testDeleteCollectionHandler(collectionId)
assert.Equal(t, http.StatusNotFound, status)
}
func TestDeleteCollectionHandler_ForbiddenCollection(t *testing.T) {
collectionId := "3"
status := testDeleteCollectionHandler(collectionId)
assert.Equal(t, http.StatusUnauthorized, status)
}
func testDeleteCollectionHandler(id string) int {
router := testutils.TestSetup()
w := httptest.NewRecorder()
token := testutils.ConnectDemoUser(router)
req, _ := http.NewRequest("DELETE", "/ws/collection/"+id, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, req)
return w.Code
}

View File

@@ -7,5 +7,4 @@ type Collection struct {
Name string
User User
UserID uint
Items []CollectionItem
}

View File

@@ -0,0 +1,66 @@
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")
}

View File

@@ -9,6 +9,7 @@ import (
"git.artlef.fr/bibliomane/internal/appcontext"
"git.artlef.fr/bibliomane/internal/dto"
"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"
@@ -21,6 +22,12 @@ func GetCollectionHandler(ac appcontext.AppContext) {
return
}
err = myvalidator.ValidateId(ac.Db, uint(collectionId), &model.Collection{})
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
user, err := ac.GetAuthenticatedUser()
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)

View File

@@ -102,6 +102,9 @@ func Setup(config *config.Config) *gin.Engine {
ws.POST("/upload/cover", func(c *gin.Context) {
routes.PostUploadBookCoverHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
})
ws.DELETE("/collection/:id", func(c *gin.Context) {
routes.DeleteCollectionHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
})
ws.DELETE("/collection/item/:id", func(c *gin.Context) {
routes.DeleteCollectionBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
})