From fc974fd87f2384a9632ecabe897eea33fddb4a2f Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 12 Jun 2018 19:44:47 +0200 Subject: [PATCH] Added endpoint to be able to edit list items --- Featurecreep.md | 6 +-- models/list_items_create_update.go | 10 ++--- routes/api/v1/list_item_add_update.go | 56 ++++++++++++++++++++------- routes/routes.go | 3 +- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/Featurecreep.md b/Featurecreep.md index e48eef38..17d8b461 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -35,12 +35,12 @@ Ab v0.3 können wir mit clients anfangen. * [x] Erstellen * [x] Bearbeiten * [x] Löschen -* [ ] Todopunkte hinzufügen/abhaken/löschen +* [x] Todopunkte hinzufügen/abhaken/löschen * [x] Erstellen - * [ ] Bearbeiten (abhaken) + * [x] Bearbeiten (abhaken) * [x] Löschen -* [ ] Überall nochmal überprüfen dass der Nutzer auch das Recht hat die Liste zu löschen +* [x] Überall nochmal überprüfen dass der Nutzer auch das Recht hat die Liste zu löschen * [ ] Swaggerdocs !!!! diff --git a/models/list_items_create_update.go b/models/list_items_create_update.go index 96313acb..f5f2b9d6 100644 --- a/models/list_items_create_update.go +++ b/models/list_items_create_update.go @@ -3,11 +3,6 @@ package models // CreateOrUpdateListItem adds or updates a todo item to a list func CreateOrUpdateListItem(item *ListItem) (err error) { - // Check if we have at least a text - if item.Text == "" { - return ErrListItemCannotBeEmpty{} - } - // Check if the list exists _, err = GetListByID(item.ListID) if err != nil { @@ -31,6 +26,11 @@ func CreateOrUpdateListItem(item *ListItem) (err error) { if err != nil { return } + + // Check if we have at least a text + if item.Text == "" { + return ErrListItemCannotBeEmpty{} + } } return diff --git a/routes/api/v1/list_item_add_update.go b/routes/api/v1/list_item_add_update.go index 17e8e6b1..ae9965e1 100644 --- a/routes/api/v1/list_item_add_update.go +++ b/routes/api/v1/list_item_add_update.go @@ -7,14 +7,7 @@ import ( "strconv" ) -func AddOrUpdateListItem(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."}) - } - +func AddListItem(c echo.Context) error { // Get the list ID id := c.Param("id") // Make int @@ -22,16 +15,49 @@ func AddOrUpdateListItem(c echo.Context) error { if err != nil { return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) } - listItem.ListID = listID - // Set the user - user, err := models.GetCurrentUser(c) + return updateOrCreateListItemHelper(listID, 0, c) +} + +func UpdateListItem(c echo.Context) error { + // Get the item ID + id := c.Param("id") + // Make int + itemID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) } - listItem.CreatedBy = user - err = models.CreateOrUpdateListItem(listItem) + 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 + } + + err := models.CreateOrUpdateListItem(listItem) if err != nil { if models.IsErrListDoesNotExist(err) { return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."}) @@ -47,4 +73,4 @@ func AddOrUpdateListItem(c echo.Context) error { } return c.JSON(http.StatusOK, listItem) -} +} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 379826d7..4814a605 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -57,8 +57,9 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/lists", apiv1.GetListsByUser) a.GET("/lists/:id", apiv1.GetListByID) a.POST("/lists/:id", apiv1.AddOrUpdateList) - a.PUT("/lists/:id", apiv1.AddOrUpdateListItem) + a.PUT("/lists/:id", apiv1.AddListItem) a.DELETE("/lists/:id", apiv1.DeleteListByID) a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID) + a.POST("/item/:id", apiv1.UpdateListItem) }