Implemented create and update methods for items
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// AddListItem ...
|
||||
@ -34,17 +32,7 @@ func AddListItem(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
// TODO: return 403 if you dont have the right to add an item to that list
|
||||
|
||||
// Get the list ID
|
||||
id := c.Param("id")
|
||||
// Make int
|
||||
listID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
|
||||
return updateOrCreateListItemHelper(listID, 0, c)
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// UpdateListItem ...
|
||||
@ -74,57 +62,5 @@ func UpdateListItem(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
// Get the item ID
|
||||
id := c.Param("id")
|
||||
// Make int
|
||||
itemID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
|
||||
return updateOrCreateListItemHelper(0, itemID, c)
|
||||
}
|
||||
|
||||
func updateOrCreateListItemHelper(listID, itemID int64, c echo.Context) error {
|
||||
|
||||
// Get the list item
|
||||
var listItem *models.ListItem
|
||||
|
||||
if err := c.Bind(&listItem); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No list model provided."})
|
||||
}
|
||||
|
||||
// Creating
|
||||
if listID != 0 {
|
||||
listItem.ListID = listID
|
||||
|
||||
// Set the user
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
listItem.CreatedBy = user
|
||||
}
|
||||
|
||||
// Updating
|
||||
if itemID != 0 {
|
||||
listItem.ID = itemID
|
||||
}
|
||||
|
||||
finalItem, err := models.CreateOrUpdateListItem(listItem)
|
||||
if err != nil {
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."})
|
||||
}
|
||||
if models.IsErrListItemCannotBeEmpty(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"You must provide at least a list item text."})
|
||||
}
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, finalItem)
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// UpdateWeb is the webhandler to update an object
|
||||
@ -28,6 +29,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
||||
// Do the update
|
||||
err = c.CObject.Update(id, ¤tUser)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if models.IsErrNeedToBeListAdmin(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||
}
|
||||
a.PUT("/lists/:id", itemHandler.CreateWeb)
|
||||
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
|
||||
a.POST("/item/:id", apiv1.UpdateListItem)
|
||||
a.POST("/item/:id", itemHandler.UpdateWeb)
|
||||
|
||||
a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser)
|
||||
a.PUT("/namespaces", apiv1.AddNamespace)
|
||||
|
Reference in New Issue
Block a user