Implemented Create and Update methods on list
This commit is contained in:
@ -5,19 +5,21 @@ import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
|
||||
type CRUDWebHandler struct {
|
||||
CObject interface{ models.CRUDable }
|
||||
CObject interface{
|
||||
models.CRUDable
|
||||
models.Rights
|
||||
}
|
||||
}
|
||||
|
||||
// This does json, handles the request
|
||||
func (c *CRUDWebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||
|
||||
// Get the ID
|
||||
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
||||
id, err := models.GetIntURLParam("id", ctx)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
@ -37,7 +39,7 @@ func (c *CRUDWebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusOK, c.CObject)
|
||||
}
|
||||
|
||||
//
|
||||
// ReadAllWeb returns all elements of a type
|
||||
func (c *CRUDWebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||
currentUser, err := models.GetCurrentUser(ctx)
|
||||
if err != nil {
|
||||
@ -52,3 +54,59 @@ func (c *CRUDWebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||
|
||||
return ctx.JSON(http.StatusOK, lists)
|
||||
}
|
||||
|
||||
// UpdateWeb is the webhandler to update an object
|
||||
func (c *CRUDWebHandler) UpdateWeb(ctx echo.Context) error {
|
||||
// Get the object
|
||||
if err := ctx.Bind(&c.CObject); err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."})
|
||||
}
|
||||
|
||||
// Get the ID
|
||||
var err error
|
||||
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 do that
|
||||
currentUser, err := models.GetCurrentUser(ctx)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
|
||||
}
|
||||
|
||||
// Do the update
|
||||
err = c.CObject.Update(id, ¤tUser)
|
||||
if err != nil {
|
||||
if models.IsErrNeedToBeListAdmin(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, c.CObject)
|
||||
}
|
||||
|
||||
// CreateWeb is the handler to create an object
|
||||
func (c *CRUDWebHandler) CreateWeb(ctx echo.Context) error {
|
||||
// Get the object
|
||||
if err := ctx.Bind(&c.CObject); err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."})
|
||||
}
|
||||
|
||||
// Get the user to pass for later checks
|
||||
currentUser, err := models.GetCurrentUser(ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||
}
|
||||
|
||||
// Create
|
||||
err = c.CObject.Create(¤tUser)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, c.CObject)
|
||||
}
|
Reference in New Issue
Block a user