implemented readone with parambinder
This commit is contained in:
@ -24,15 +24,6 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||
}
|
||||
|
||||
// Get an ID if we have one
|
||||
/*var id int64
|
||||
if ctx.Param("id") != "" {
|
||||
id, err = models.GetIntURLParam("id", ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Bad id.")
|
||||
}
|
||||
}*/
|
||||
|
||||
// Check rights
|
||||
if !c.CObject.CanCreate(¤tUser) {
|
||||
return echo.NewHTTPError(http.StatusForbidden)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/labstack/echo"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const paramTagName = "param"
|
||||
@ -21,7 +22,11 @@ func ParamBinder(i interface{}, c echo.Context) (err error) {
|
||||
paramValues := c.ParamValues()
|
||||
paramVars := make(map[string][]string)
|
||||
for in, name := range paramNames {
|
||||
paramVars[name] = append(paramVars[name], paramValues[in])
|
||||
// Hotfix for an echo bug where a param name would show up which dont exist
|
||||
names := strings.Split(name, ",")
|
||||
for _, n := range names {
|
||||
paramVars[n] = append(paramVars[name], paramValues[in])
|
||||
}
|
||||
}
|
||||
|
||||
b := Binder{}
|
||||
|
@ -4,19 +4,19 @@ import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ReadOneWeb is the webhandler to get one object
|
||||
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||
|
||||
// Get the ID
|
||||
id, err := models.GetIntURLParam("id", ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
||||
// Get the object & bind params to struct
|
||||
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||
}
|
||||
|
||||
// Get our object
|
||||
err = c.CObject.ReadOne(id)
|
||||
err := c.CObject.ReadOne()
|
||||
if err != nil {
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
@ -26,6 +26,12 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
if models.IsErrTeamDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.")
|
||||
}
|
||||
|
||||
|
@ -113,27 +113,27 @@ func RegisterRoutes(e *echo.Echo) {
|
||||
CObject: &models.List{},
|
||||
}
|
||||
a.GET("/lists", listHandler.ReadAllWeb)
|
||||
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
||||
a.POST("/lists/:id", listHandler.UpdateWeb)
|
||||
a.DELETE("/lists/:listid", listHandler.DeleteWeb)
|
||||
a.PUT("/namespaces/:nid/lists", listHandler.CreateWeb)
|
||||
a.GET("/lists/:list", listHandler.ReadOneWeb)
|
||||
a.POST("/lists/:list", listHandler.UpdateWeb)
|
||||
a.DELETE("/lists/:list", listHandler.DeleteWeb)
|
||||
a.PUT("/namespaces/:namespace/lists", listHandler.CreateWeb)
|
||||
|
||||
itemHandler := &crud.WebHandler{
|
||||
CObject: &models.ListItem{},
|
||||
}
|
||||
a.PUT("/lists/:listid", itemHandler.CreateWeb)
|
||||
a.DELETE("/items/:listitemid", itemHandler.DeleteWeb)
|
||||
a.POST("/items/:id", itemHandler.UpdateWeb)
|
||||
a.PUT("/lists/:list", itemHandler.CreateWeb)
|
||||
a.DELETE("/items/:listitem", itemHandler.DeleteWeb)
|
||||
a.POST("/items/:listitem", itemHandler.UpdateWeb)
|
||||
|
||||
namespaceHandler := &crud.WebHandler{
|
||||
CObject: &models.Namespace{},
|
||||
}
|
||||
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
||||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
|
||||
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
||||
a.DELETE("/namespaces/:nid", namespaceHandler.DeleteWeb)
|
||||
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
||||
a.GET("/namespaces/:namespace", namespaceHandler.ReadOneWeb)
|
||||
a.POST("/namespaces/:namespace", namespaceHandler.UpdateWeb)
|
||||
a.DELETE("/namespaces/:namespace", namespaceHandler.DeleteWeb)
|
||||
a.GET("/namespaces/:namespace/lists", apiv1.GetListsByNamespaceID)
|
||||
|
||||
namespaceTeamHandler := &crud.WebHandler{
|
||||
CObject: &models.TeamNamespace{},
|
||||
@ -146,8 +146,8 @@ func RegisterRoutes(e *echo.Echo) {
|
||||
CObject: &models.Team{},
|
||||
}
|
||||
a.GET("/teams", teamHandler.ReadAllWeb)
|
||||
a.GET("/teams/:id", teamHandler.ReadOneWeb)
|
||||
a.GET("/teams/:team", teamHandler.ReadOneWeb)
|
||||
a.PUT("/teams", teamHandler.CreateWeb)
|
||||
a.POST("/teams/:id", teamHandler.UpdateWeb)
|
||||
a.DELETE("/teams/:teamid", teamHandler.DeleteWeb)
|
||||
a.POST("/teams/:team", teamHandler.UpdateWeb)
|
||||
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
||||
}
|
||||
|
Reference in New Issue
Block a user