Implemented create and update methods for items

This commit is contained in:
konrad 2018-07-11 02:39:55 +02:00 committed by kolaente
parent 592dc20af4
commit ee4a904e35
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 34 additions and 114 deletions

View file

@ -1,64 +1,42 @@
package models package models
// CreateOrUpdateListItem adds or updates a todo item to a list
func CreateOrUpdateListItem(item *ListItem) (newItem *ListItem, err error) {
// Check if the list exists
_, err = GetListByID(item.ListID)
if err != nil {
return
}
// Check if the user exists
item.CreatedBy, _, err = GetUserByID(item.CreatedBy.ID)
if err != nil {
return
}
item.CreatedByID = item.CreatedBy.ID
if item.ID != 0 {
_, err = x.ID(item.ID).Update(item)
if err != nil {
return
}
} else {
// Check if we have at least a text
if item.Text == "" {
return newItem, ErrListItemCannotBeEmpty{}
}
_, err = x.Insert(item)
if err != nil {
return
}
}
// Get the new/updated item
finalItem, err := GetListItemByID(item.ID)
return &finalItem, err
}
// Create is the implementation to create a list item // Create is the implementation to create a list item
func (i *ListItem) Create(doer *User, lID int64) (err error) { func (i *ListItem) Create(doer *User, lID int64) (err error) {
i.ListID = lID i.ListID = lID
i.ID = 0
return createOrUpdateListItem(i, doer, lID)
}
// Update updates a list item
func (i *ListItem) Update(ID int64, doer *User) (err error) {
i.ID = ID
// Get the full item
fullItem, err := GetListItemByID(ID)
if err != nil {
return
}
return createOrUpdateListItem(i, doer, fullItem.ListID)
}
// Helper function for creation or updating of new lists as both methods share most of their logic
func createOrUpdateListItem(i *ListItem, doer *User, lID int64) (err error) {
// Check rights // Check rights
user, _, err := GetUserByID(doer.ID) user, _, err := GetUserByID(doer.ID)
if err != nil { if err != nil {
return return
} }
i.CreatedBy = user // Needed because we return the full item object
i.CreatedByID = user.ID
// Get the list to check if the user has the right to write to that list // Get the list to check if the user has the right to write to that list
list, err := GetListByID(lID) list, err := GetListByID(lID) // TODO: Get the list with one query by item ID
if err != nil { if err != nil {
return return
} }
if !list.CanWrite(&user) { if !list.CanWrite(&user) {
return ErrNeedToBeListWriter{ListID: lID, UserID: user.ID} return ErrNeedToBeListWriter{ListID: i.ListID, UserID: user.ID}
} }
// Check if we have at least a text // Check if we have at least a text
@ -66,9 +44,13 @@ func (i *ListItem) Create(doer *User, lID int64) (err error) {
return ErrListItemCannotBeEmpty{} return ErrListItemCannotBeEmpty{}
} }
// Do the update
if i.ID != 0 {
_, err = x.ID(i.ID).Update(i)
} else {
i.CreatedByID = user.ID
i.CreatedBy = user
_, err = x.Insert(i) _, err = x.Insert(i)
if err != nil {
return
} }
return return

View file

@ -1,10 +1,8 @@
package v1 package v1
import ( import (
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strconv"
) )
// AddListItem ... // AddListItem ...
@ -34,17 +32,7 @@ func AddListItem(c echo.Context) error {
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// TODO: return 403 if you dont have the right to add an item to that list return echo.NewHTTPError(http.StatusNotImplemented)
// 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)
} }
// UpdateListItem ... // UpdateListItem ...
@ -74,57 +62,5 @@ func UpdateListItem(c echo.Context) error {
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// Get the item ID return echo.NewHTTPError(http.StatusNotImplemented)
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)
} }

View file

@ -4,6 +4,7 @@ import (
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"fmt"
) )
// UpdateWeb is the webhandler to update an object // UpdateWeb is the webhandler to update an object
@ -28,6 +29,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
// Do the update // Do the update
err = c.CObject.Update(id, &currentUser) err = c.CObject.Update(id, &currentUser)
if err != nil { if err != nil {
fmt.Println(err)
if models.IsErrNeedToBeListAdmin(err) { if models.IsErrNeedToBeListAdmin(err) {
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.") return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
} }

View file

@ -101,7 +101,7 @@ func RegisterRoutes(e *echo.Echo) {
} }
a.PUT("/lists/:id", itemHandler.CreateWeb) a.PUT("/lists/:id", itemHandler.CreateWeb)
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID) a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
a.POST("/item/:id", apiv1.UpdateListItem) a.POST("/item/:id", itemHandler.UpdateWeb)
a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser) a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser)
a.PUT("/namespaces", apiv1.AddNamespace) a.PUT("/namespaces", apiv1.AddNamespace)