1
0

implemented readone with parambinder

This commit is contained in:
konrad
2018-07-21 15:08:46 +02:00
committed by kolaente
parent d06ed68125
commit 9e75e9b73b
9 changed files with 44 additions and 42 deletions

View File

@ -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(&currentUser) {
return echo.NewHTTPError(http.StatusForbidden)

View File

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

View File

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