package v1 import ( "git.mowie.cc/konrad/Library/models" "github.com/labstack/echo" "net/http" "strconv" ) // BookDelete is the handler to delete a book 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 _, exists, err := models.GetBookById(bookID) 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"}) } return c.JSON(http.StatusOK, models.Message{"success"}) }