48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package v1
|
|
|
|
import (
|
|
"git.mowie.cc/konrad/Library/models"
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// ItemDelete is the handler to delete a item
|
|
func ItemDelete(c echo.Context) error {
|
|
|
|
id := c.Param("id")
|
|
|
|
// Make int
|
|
itemID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Item ID is invalid."})
|
|
}
|
|
|
|
// Check if the item exists
|
|
_, exists, err := models.GetItemByID(itemID)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get item."})
|
|
}
|
|
|
|
if !exists {
|
|
return c.JSON(http.StatusBadRequest, models.Message{"The item does not exist."})
|
|
}
|
|
|
|
// Delete it
|
|
err = models.DeleteItemByID(itemID)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete item."})
|
|
}
|
|
|
|
// Log the action
|
|
err = models.LogAction("Deleted an item", itemID, c)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
|
}
|