2017-10-11 16:06:51 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-03-05 11:53:12 +00:00
|
|
|
"git.kolaente.de/konrad/Library/models"
|
2017-10-11 16:06:51 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
2017-11-07 15:35:10 +00:00
|
|
|
"strconv"
|
2017-10-11 16:06:51 +00:00
|
|
|
)
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// PublisherDelete is the handler to delete a publisher
|
2017-10-11 16:06:51 +00:00
|
|
|
func PublisherDelete(c echo.Context) error {
|
|
|
|
|
|
|
|
id := c.Param("id")
|
|
|
|
|
|
|
|
// Make int
|
|
|
|
publisherID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Publisher ID is invalid."})
|
2017-10-11 16:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the publisher exists
|
|
|
|
_, exists, err := models.GetPublisherByID(publisherID)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher."})
|
2017-10-11 16:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"The publisher does not exist."})
|
2017-10-11 16:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
err = models.DeletePublisherByID(publisherID)
|
|
|
|
|
|
|
|
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:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete publisher."})
|
2017-10-11 16:06:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:46:54 +00:00
|
|
|
// Log the action
|
|
|
|
err = models.LogAction("Deleted a publisher", publisherID, c)
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
|
2017-12-05 13:46:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:06:51 +00:00
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|