From c8622b0029a0f8f058f8d5c7077dde4c06038770 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Jul 2018 13:27:25 +0200 Subject: [PATCH] Implemented Delete methods on list --- models/crudable.go | 2 +- models/list_delete.go | 20 ++++++++++++-------- routes/CRUD/CRUD_helper.go | 31 ++++++++++++++++++++++++++++++- routes/api/v1/list_delete.go | 11 ++++++++--- routes/routes.go | 2 +- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/models/crudable.go b/models/crudable.go index 6b6a2966..d009eebb 100644 --- a/models/crudable.go +++ b/models/crudable.go @@ -5,5 +5,5 @@ type CRUDable interface { ReadOne(int64) error ReadAll(*User) (interface{}, error) Update(int64, *User) error - Delete() + Delete(int64, *User) error } diff --git a/models/list_delete.go b/models/list_delete.go index b063b040..68a6b1ec 100644 --- a/models/list_delete.go +++ b/models/list_delete.go @@ -1,25 +1,29 @@ package models -func DeleteListByID(listID int64, doer *User) (err error) { - +func (l *List) Delete(id int64, doer *User) (err error) { // Check if the list exists - list, err := GetListByID(listID) + list, err := GetListByID(id) if err != nil { return } - if list.Owner.ID != doer.ID { - return ErrNeedToBeListAdmin{ListID: listID, UserID: doer.ID} + // Check rights + user, _, err := GetUserByID(doer.ID) + if err != nil { + return + } + + if !list.IsAdmin(&user) { + return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID} } // Delete the list - _, err = x.ID(listID).Delete(&List{}) + _, err = x.ID(id).Delete(&List{}) if err != nil { return } // Delete all todoitems on that list - _, err = x.Where("list_id = ?", listID).Delete(&ListItem{}) - + _, err = x.Where("list_id = ?", id).Delete(&ListItem{}) return } diff --git a/routes/CRUD/CRUD_helper.go b/routes/CRUD/CRUD_helper.go index 7154eaca..ddfb1686 100644 --- a/routes/CRUD/CRUD_helper.go +++ b/routes/CRUD/CRUD_helper.go @@ -63,7 +63,6 @@ func (c *CRUDWebHandler) UpdateWeb(ctx echo.Context) error { } // Get the ID - var err error id, err := models.GetIntURLParam("id", ctx) if err != nil { return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) @@ -109,4 +108,34 @@ func (c *CRUDWebHandler) CreateWeb(ctx echo.Context) error { } return ctx.JSON(http.StatusOK, c.CObject) +} + +// DeleteWeb is the web handler to delete something +func (c *CRUDWebHandler) DeleteWeb(ctx echo.Context) error { + // Get the ID + id, err := models.GetIntURLParam("id", ctx) + if err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // Check if the user has the right to delete + user, err := models.GetCurrentUser(ctx) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError) + } + + err = c.CObject.Delete(id, &user) + if err != nil { + if models.IsErrNeedToBeListAdmin(err) { + return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.") + } + + if models.IsErrListDoesNotExist(err) { + return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.") + } + + return echo.NewHTTPError(http.StatusInternalServerError) + } + + return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."}) } \ No newline at end of file diff --git a/routes/api/v1/list_delete.go b/routes/api/v1/list_delete.go index f0b259a4..c246c8c8 100644 --- a/routes/api/v1/list_delete.go +++ b/routes/api/v1/list_delete.go @@ -1,10 +1,11 @@ package v1 import ( - "git.kolaente.de/konrad/list/models" +// "git.kolaente.de/konrad/list/models" "github.com/labstack/echo" +// "net/http" +// "strconv" "net/http" - "strconv" ) func DeleteListByID(c echo.Context) error { @@ -33,6 +34,7 @@ func DeleteListByID(c echo.Context) error { // "500": // "$ref": "#/responses/Message" + /* // Check if we have our ID id := c.Param("id") // Make int @@ -47,7 +49,7 @@ func DeleteListByID(c echo.Context) error { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } - err = models.DeleteListByID(itemID, &user) +// err = models.DeleteListByID(itemID, &user) if err != nil { if models.IsErrNeedToBeListAdmin(err) { return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."}) @@ -61,4 +63,7 @@ func DeleteListByID(c echo.Context) error { } return c.JSON(http.StatusOK, models.Message{"The list was deleted with success."}) + */ + + return echo.NewHTTPError(http.StatusNotImplemented) } diff --git a/routes/routes.go b/routes/routes.go index 85ad81bd..77fb28b5 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -94,7 +94,7 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/lists/:id", listHandler.ReadOneWeb) a.POST("/lists/:id", listHandler.UpdateWeb) a.PUT("/lists/:id", apiv1.AddListItem) - a.DELETE("/lists/:id", apiv1.DeleteListByID) + a.DELETE("/lists/:id", listHandler.DeleteWeb) a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID) a.POST("/item/:id", apiv1.UpdateListItem)