2018-06-10 19:49:40 +02:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.kolaente.de/konrad/list/models"
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2018-06-12 19:44:47 +02:00
|
|
|
func AddListItem(c echo.Context) error {
|
2018-06-13 13:45:22 +02:00
|
|
|
// swagger:operation PUT /lists/{listID} lists addListItem
|
|
|
|
// ---
|
|
|
|
// summary: Adds an item to a list
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: listID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the list to use
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ListItem"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/ListItem"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "500":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
|
|
|
|
// TODO: return 403 if you dont have the right to add an item to that list
|
|
|
|
|
2018-06-10 19:49:40 +02:00
|
|
|
// 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."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 19:44:47 +02:00
|
|
|
return updateOrCreateListItemHelper(listID, 0, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateListItem(c echo.Context) error {
|
2018-06-13 13:45:22 +02:00
|
|
|
// swagger:operation PUT /item/{itemID} lists updateListItem
|
|
|
|
// ---
|
|
|
|
// summary: Updates a list item
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: itemID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the item to update
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ListItem"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/ListItem"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "500":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
|
2018-06-12 19:44:47 +02:00
|
|
|
// Get the item ID
|
|
|
|
id := c.Param("id")
|
|
|
|
// Make int
|
|
|
|
itemID, err := strconv.ParseInt(id, 10, 64)
|
2018-06-10 19:49:40 +02:00
|
|
|
if err != nil {
|
2018-06-12 19:44:47 +02:00
|
|
|
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."})
|
2018-06-10 19:49:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-12 19:44:47 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-06-13 12:18:55 +02:00
|
|
|
finalItem, err := models.CreateOrUpdateListItem(listItem)
|
2018-06-10 19:49:40 +02:00
|
|
|
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."})
|
|
|
|
}
|
|
|
|
|
2018-06-13 12:18:55 +02:00
|
|
|
return c.JSON(http.StatusOK, finalItem)
|
2018-06-13 13:45:22 +02:00
|
|
|
}
|