implemented method to delete a book

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-10 22:47:54 +02:00 committed by kolaente
parent 3db67ff8f2
commit 57b6997267
4 changed files with 59 additions and 3 deletions

15
models/book_delete.go Normal file
View File

@ -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
}

View File

@ -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"})
}

View File

@ -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 == "" {

View File

@ -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