implemented method to delete a book
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
3db67ff8f2
commit
57b6997267
|
@ -0,0 +1,15 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
func DeleteBookByID(id int64) error {
|
||||||
|
// Delete the book
|
||||||
|
_, err := x.Id(id).Delete(&Book{})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all authors associated with that book
|
||||||
|
_, err = x.Delete(&AuthorBook{BookID:id})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package v1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
"strconv"
|
||||||
|
"net/http"
|
||||||
|
"git.mowie.cc/konrad/Library/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"})
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Add(c echo.Context) error {
|
func BookAdd(c echo.Context) error {
|
||||||
// Check for Request Content
|
// Check for Request Content
|
||||||
book := c.FormValue("book")
|
book := c.FormValue("book")
|
||||||
if book == "" {
|
if book == "" {
|
||||||
|
|
|
@ -51,7 +51,8 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.POST("/tokenTest", apiv1.CheckToken)
|
a.POST("/tokenTest", apiv1.CheckToken)
|
||||||
|
|
||||||
// Manage Books
|
// Manage Books
|
||||||
a.POST("/books/add", apiv1.Add)
|
a.PUT("/books", apiv1.BookAdd)
|
||||||
|
a.DELETE("/books/:id", apiv1.BookDelete)
|
||||||
|
|
||||||
// Manage Authors
|
// Manage Authors
|
||||||
|
|
||||||
|
@ -71,7 +72,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
|
|
||||||
/books/:id - ✔ Buch anzeigen
|
/books/:id - ✔ Buch anzeigen
|
||||||
/books/:id/edit - |Buch bearbeiten (inkl mengen)
|
/books/:id/edit - |Buch bearbeiten (inkl mengen)
|
||||||
/books/:id/delete - |Buch löschen
|
/books/:id/delete - ✔ |Buch löschen (+alle einträge in authors_books löschen dessen Bush dazu gehört)
|
||||||
/books/search?s=se - ✔ Suchen
|
/books/search?s=se - ✔ Suchen
|
||||||
/books/list - ✔ Auflisten
|
/books/list - ✔ Auflisten
|
||||||
/books/add - ✔ |Hinzufügen
|
/books/add - ✔ |Hinzufügen
|
||||||
|
|
Loading…
Reference in New Issue