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