From 57b69972677be010cc4766244a76d4559bfa7076 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 22:47:54 +0200 Subject: [PATCH] implemented method to delete a book Signed-off-by: kolaente --- models/book_delete.go | 15 ++++++++++++++ routes/api/v1/book_delete.go | 40 ++++++++++++++++++++++++++++++++++++ routes/api/v1/books_add.go | 2 +- routes/routes.go | 5 +++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 models/book_delete.go create mode 100644 routes/api/v1/book_delete.go diff --git a/models/book_delete.go b/models/book_delete.go new file mode 100644 index 0000000..3f437bd --- /dev/null +++ b/models/book_delete.go @@ -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 +} \ No newline at end of file diff --git a/routes/api/v1/book_delete.go b/routes/api/v1/book_delete.go new file mode 100644 index 0000000..cfd7a69 --- /dev/null +++ b/routes/api/v1/book_delete.go @@ -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"}) +} \ No newline at end of file diff --git a/routes/api/v1/books_add.go b/routes/api/v1/books_add.go index 76039a2..196aaa8 100644 --- a/routes/api/v1/books_add.go +++ b/routes/api/v1/books_add.go @@ -8,7 +8,7 @@ import ( "strings" ) -func Add(c echo.Context) error { +func BookAdd(c echo.Context) error { // Check for Request Content book := c.FormValue("book") if book == "" { diff --git a/routes/routes.go b/routes/routes.go index c350d5e..7d0c2a2 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -51,7 +51,8 @@ func RegisterRoutes(e *echo.Echo) { a.POST("/tokenTest", apiv1.CheckToken) // Manage Books - a.POST("/books/add", apiv1.Add) + a.PUT("/books", apiv1.BookAdd) + a.DELETE("/books/:id", apiv1.BookDelete) // Manage Authors @@ -71,7 +72,7 @@ func RegisterRoutes(e *echo.Echo) { /books/:id - ✔ Buch anzeigen /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/list - ✔ Auflisten /books/add - ✔ |Hinzufügen