From 0652125c252d6fe051348a0f56caae8066ffe5b6 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 13 Jun 2018 12:18:55 +0200 Subject: [PATCH] Fixed returning of user infos when updating a todo item --- Featurecreep.md | 2 +- models/list_items.go | 2 +- models/list_items_create_update.go | 9 ++++++--- models/lists.go | 2 +- models/user.go | 6 +++--- routes/api/v1/list_item_add_update.go | 4 ++-- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Featurecreep.md b/Featurecreep.md index 7131170a..4034b979 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -41,7 +41,7 @@ Ab v0.3 können wir mit clients anfangen. * [x] Löschen * [x] Überall nochmal überprüfen dass der Nutzer auch das Recht hat die Liste zu löschen -* [ ] "Apiformat" Methoden, damit in der Ausgabe zb kein Passwort drin ist..., oder created/updated von Nutzern +* [ ] "Apiformat" Methoden, damit in der Ausgabe zb kein Passwort drin ist..., oder created/updated von Nutzern... oder ownerID nicht drin ist sondern nur das ownerobject * [ ] Swaggerdocs !!!! #### v0.2 diff --git a/models/list_items.go b/models/list_items.go index 9858dbef..658414cb 100644 --- a/models/list_items.go +++ b/models/list_items.go @@ -8,7 +8,7 @@ type ListItem struct { Done bool `json:"done"` DueDateUnix int64 `xorm:"int(11)" json:"dueDate"` ReminderUnix int64 `xorm:"int(11)" json:"reminderDate"` - CreatedByID int64 `xorm:"int(11)" json:"createdByID"` // ID of the user who put that item on the list + CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that item on the list ListID int64 `xorm:"int(11)" json:"listID"` Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` diff --git a/models/list_items_create_update.go b/models/list_items_create_update.go index 60442cda..aa0a1cdc 100644 --- a/models/list_items_create_update.go +++ b/models/list_items_create_update.go @@ -1,7 +1,7 @@ package models // CreateOrUpdateListItem adds or updates a todo item to a list -func CreateOrUpdateListItem(item *ListItem) (err error) { +func CreateOrUpdateListItem(item *ListItem) (newItem *ListItem, err error) { // Check if the list exists _, err = GetListByID(item.ListID) @@ -30,9 +30,12 @@ func CreateOrUpdateListItem(item *ListItem) (err error) { // Check if we have at least a text if item.Text == "" { - return ErrListItemCannotBeEmpty{} + return newItem, ErrListItemCannotBeEmpty{} } } - return + // Get the new/updated item + finalItem, err := GetListItemByID(item.ID) + + return &finalItem, err } diff --git a/models/lists.go b/models/lists.go index 6c59b524..aa93c0e6 100644 --- a/models/lists.go +++ b/models/lists.go @@ -5,7 +5,7 @@ type List struct { ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` Title string `xorm:"varchar(250)" json:"title"` Description string `xorm:"varchar(1000)" json:"description"` - OwnerID int64 `xorm:"int(11)" json:"ownerID"` + OwnerID int64 `xorm:"int(11)" json:"-"` Owner User `xorm:"-" json:"owner"` Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` diff --git a/models/user.go b/models/user.go index 3775f636..fe49d95a 100644 --- a/models/user.go +++ b/models/user.go @@ -16,10 +16,10 @@ type UserLogin struct { type User struct { ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` Username string `xorm:"varchar(250) not null unique" json:"username"` - Password string `xorm:"varchar(250) not null" json:"password"` + Password string `xorm:"varchar(250) not null" json:"-"` Email string `xorm:"varchar(250)" json:"email"` - Created int64 `xorm:"created" json:"created"` - Updated int64 `xorm:"updated" json:"updated"` + Created int64 `xorm:"created" json:"-"` + Updated int64 `xorm:"updated" json:"-"` } // TableName returns the table name for users diff --git a/routes/api/v1/list_item_add_update.go b/routes/api/v1/list_item_add_update.go index ae9965e1..8ddaae09 100644 --- a/routes/api/v1/list_item_add_update.go +++ b/routes/api/v1/list_item_add_update.go @@ -57,7 +57,7 @@ func updateOrCreateListItemHelper(listID, itemID int64, c echo.Context) error { listItem.ID = itemID } - err := models.CreateOrUpdateListItem(listItem) + finalItem, err := models.CreateOrUpdateListItem(listItem) if err != nil { if models.IsErrListDoesNotExist(err) { return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."}) @@ -72,5 +72,5 @@ func updateOrCreateListItemHelper(listID, itemID int64, c echo.Context) error { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) } - return c.JSON(http.StatusOK, listItem) + return c.JSON(http.StatusOK, finalItem) } \ No newline at end of file