2017-10-10 20:47:54 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-03-05 11:53:12 +00:00
|
|
|
"git.kolaente.de/konrad/Library/models"
|
2017-10-10 20:47:54 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
2017-11-07 15:35:10 +00:00
|
|
|
"strconv"
|
2017-10-10 20:47:54 +00:00
|
|
|
)
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// BookDelete is the handler to delete a book
|
2017-10-10 20:47:54 +00:00
|
|
|
func BookDelete(c echo.Context) error {
|
|
|
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
// Make int
|
|
|
|
bookID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Book ID is invalid."})
|
2017-10-10 20:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the book exists
|
2017-11-08 09:57:38 +00:00
|
|
|
_, exists, err := models.GetBookByID(bookID)
|
2017-10-10 20:47:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book."})
|
2017-10-10 20:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"The book does not exist."})
|
2017-10-10 20:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
err = models.DeleteBookByID(bookID)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-24 11:58:00 +00:00
|
|
|
if models.IsErrIDCannotBeZero(err) {
|
2018-01-24 12:15:28 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Id cannot be 0"})
|
2018-01-24 11:58:00 +00:00
|
|
|
}
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book."})
|
2017-10-10 20:47:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:46:54 +00:00
|
|
|
// Log the action
|
|
|
|
err = models.LogAction("Deleted a book", bookID, c)
|
|
|
|
if err != nil {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
|
2017-12-05 13:46:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 20:47:54 +00:00
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|