1
0

Implemented Create and Update methods on list

This commit is contained in:
kolaente
2018-07-09 23:17:19 +02:00
parent 9e8f13edf6
commit b1de837c4f
12 changed files with 152 additions and 29 deletions

View File

@ -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, &currentUser)
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(&currentUser)
if err != nil {
fmt.Println(err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return ctx.JSON(http.StatusOK, c.CObject)
}

View File

@ -49,7 +49,7 @@ func DeleteListByID(c echo.Context) error {
err = models.DeleteListByID(itemID, &user)
if err != nil {
if models.IsErrNeedToBeListOwner(err) {
if models.IsErrNeedToBeListAdmin(err) {
return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."})
}

View File

@ -92,7 +92,7 @@ func RegisterRoutes(e *echo.Echo) {
}
a.GET("/lists", listHandler.ReadAllWeb)
a.GET("/lists/:id", listHandler.ReadOneWeb)
a.POST("/lists/:id", apiv1.UpdateList)
a.POST("/lists/:id", listHandler.UpdateWeb)
a.PUT("/lists/:id", apiv1.AddListItem)
a.DELETE("/lists/:id", apiv1.DeleteListByID)
@ -105,5 +105,5 @@ func RegisterRoutes(e *echo.Echo) {
a.POST("/namespaces/:id", apiv1.UpdateNamespace)
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
a.PUT("/namespaces/:id/lists", apiv1.AddList)
a.PUT("/namespaces/:id/lists", listHandler.CreateWeb)
}