1
0

implemented binding url params directly to struct instead of passing them to the method for deleting items

This commit is contained in:
konrad
2018-07-18 08:15:38 +02:00
committed by kolaente
parent f0c003d069
commit 249128a46e
17 changed files with 48 additions and 40 deletions

View File

@ -4,14 +4,19 @@ import (
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo"
"net/http"
"fmt"
)
// DeleteWeb is the web handler to delete something
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
// Get the ID
id, err := models.GetIntURLParam("id", ctx)
/*id, err := models.GetIntURLParam("id", ctx)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}*/
// Bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL param.")
}
// Check if the user has the right to delete
@ -19,12 +24,15 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError)
}
if !c.CObject.CanDelete(&user, id) {
if !c.CObject.CanDelete(&user) {
return echo.NewHTTPError(http.StatusForbidden)
}
err = c.CObject.Delete(id)
err = c.CObject.Delete()
if err != nil {
fmt.Println(err)
if models.IsErrNeedToBeListAdmin(err) {
return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.")
}

View File

@ -93,14 +93,14 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/lists", listHandler.ReadAllWeb)
a.GET("/lists/:id", listHandler.ReadOneWeb)
a.POST("/lists/:id", listHandler.UpdateWeb)
a.DELETE("/lists/:id", listHandler.DeleteWeb)
a.DELETE("/lists/:listid", listHandler.DeleteWeb)
a.PUT("/namespaces/:id/lists", listHandler.CreateWeb)
itemHandler := &crud.WebHandler{
CObject: &models.ListItem{},
}
a.PUT("/lists/:id", itemHandler.CreateWeb)
a.DELETE("/items/:id", itemHandler.DeleteWeb)
a.DELETE("/items/:listitemid", itemHandler.DeleteWeb)
a.POST("/items/:id", itemHandler.UpdateWeb)
namespaceHandler := &crud.WebHandler{
@ -110,7 +110,7 @@ func RegisterRoutes(e *echo.Echo) {
a.PUT("/namespaces", namespaceHandler.CreateWeb)
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
a.DELETE("/namespaces/:id", namespaceHandler.DeleteWeb)
a.DELETE("/namespaces/:nid", namespaceHandler.DeleteWeb)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
namespaceTeamHandler := &crud.WebHandler{
@ -127,5 +127,5 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/teams/:id", teamHandler.ReadOneWeb)
a.PUT("/teams", teamHandler.CreateWeb)
a.POST("/teams/:id", teamHandler.UpdateWeb)
a.DELETE("/teams/:id", teamHandler.DeleteWeb)
a.DELETE("/teams/:teamid", teamHandler.DeleteWeb)
}