Implemented Delete methods on list

This commit is contained in:
konrad 2018-07-10 13:27:25 +02:00 committed by kolaente
parent b1de837c4f
commit c8622b0029
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 52 additions and 14 deletions

View file

@ -5,5 +5,5 @@ type CRUDable interface {
ReadOne(int64) error
ReadAll(*User) (interface{}, error)
Update(int64, *User) error
Delete()
Delete(int64, *User) error
}

View file

@ -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
}

View file

@ -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."})
}

View file

@ -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)
}

View file

@ -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)