2017-10-10 20:47:54 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2017-11-07 15:35:10 +00:00
|
|
|
"git.mowie.cc/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 {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book infos"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could get book"})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"The book does not exist."})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
err = models.DeleteBookByID(bookID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book"})
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:46:54 +00:00
|
|
|
// Log the action
|
|
|
|
err = models.LogAction("Deleted a book", bookID, c)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:47:54 +00:00
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|