2018-06-12 18:07:47 +02:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-06-13 13:45:22 +02:00
|
|
|
"git.kolaente.de/konrad/list/models"
|
2018-06-12 18:07:47 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
2018-06-13 13:45:22 +02:00
|
|
|
"strconv"
|
2018-06-12 18:07:47 +02:00
|
|
|
)
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// DeleteListItemByIDtemByID is the web handler to delete a list item
|
2018-06-12 18:07:47 +02:00
|
|
|
func DeleteListItemByIDtemByID(c echo.Context) error {
|
2018-06-13 13:45:22 +02:00
|
|
|
// swagger:operation DELETE /item/{itemID} lists deleteListItem
|
|
|
|
// ---
|
|
|
|
// summary: Deletes a list item
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: itemID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the list item to delete
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "500":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
|
2018-06-12 18:07:47 +02:00
|
|
|
// Check if we have our 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."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:46:59 +02:00
|
|
|
// Check if the user has the right to delete that list item
|
2018-06-12 18:35:36 +02:00
|
|
|
user, err := models.GetCurrentUser(c)
|
2018-06-12 18:07:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:35:36 +02:00
|
|
|
err = models.DeleteListItemByID(itemID, &user)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrListItemDoesNotExist(err) {
|
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"List item does not exist."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:49:56 +02:00
|
|
|
if models.IsErrNeedToBeItemOwner(err) {
|
|
|
|
return c.JSON(http.StatusForbidden, models.Message{"You need to own the list item in order to be able to delete it."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:35:36 +02:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:07:47 +02:00
|
|
|
return c.JSON(http.StatusOK, models.Message{"The item was deleted with success."})
|
2018-06-13 13:45:22 +02:00
|
|
|
}
|