From 500007bdae828f1ff37ae067fca566552b1e5ef4 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 11 Oct 2017 17:20:56 +0200 Subject: [PATCH] Added method to add and delete an author Signed-off-by: kolaente --- models/authors_add.go | 6 ++++ models/authors_delete.go | 15 ++++++++ models/{book_delete.go => books_delete.go} | 0 routes/api/v1/author_delete.go | 40 ++++++++++++++++++++++ routes/api/v1/authors_add.go | 36 +++++++++++++++++++ routes/api/v1/books_add.go | 2 +- routes/routes.go | 4 ++- 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 models/authors_add.go create mode 100644 models/authors_delete.go rename models/{book_delete.go => books_delete.go} (100%) create mode 100644 routes/api/v1/author_delete.go create mode 100644 routes/api/v1/authors_add.go diff --git a/models/authors_add.go b/models/authors_add.go new file mode 100644 index 0000000..d8844eb --- /dev/null +++ b/models/authors_add.go @@ -0,0 +1,6 @@ +package models + +func AddAuthor(author Author) (err error){ + _, err = x.Insert(&author) + return err +} \ No newline at end of file diff --git a/models/authors_delete.go b/models/authors_delete.go new file mode 100644 index 0000000..8b35d7f --- /dev/null +++ b/models/authors_delete.go @@ -0,0 +1,15 @@ +package models + +func DeleteAuthorByID(id int64) error { + // Delete the author + _, err := x.Id(id).Delete(&Author{}) + + if err != nil { + return err + } + + // Delete all book relations associated with that author + _, err = x.Delete(&AuthorBook{AuthorID:id}) + + return err +} \ No newline at end of file diff --git a/models/book_delete.go b/models/books_delete.go similarity index 100% rename from models/book_delete.go rename to models/books_delete.go diff --git a/routes/api/v1/author_delete.go b/routes/api/v1/author_delete.go new file mode 100644 index 0000000..2e9b633 --- /dev/null +++ b/routes/api/v1/author_delete.go @@ -0,0 +1,40 @@ +package v1 + +import ( + "github.com/labstack/echo" + "strconv" + "net/http" + "git.mowie.cc/konrad/Library/models" +) + +func AuthorDelete(c echo.Context) error { + + id := c.Param("id") + + // Make int + authorID, err := strconv.ParseInt(id, 10, 64) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get author infos"}) + } + + // Check if the author exists + _, exists, err := models.GetAuthorByID(authorID) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could get author"}) + } + + if !exists { + return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."}) + } + + // Delete it + err = models.DeleteAuthorByID(authorID) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author"}) + } + + return c.JSON(http.StatusOK, models.Message{"success"}) +} \ No newline at end of file diff --git a/routes/api/v1/authors_add.go b/routes/api/v1/authors_add.go new file mode 100644 index 0000000..784553e --- /dev/null +++ b/routes/api/v1/authors_add.go @@ -0,0 +1,36 @@ +package v1 + +import ( + "github.com/labstack/echo" + "net/http" + "git.mowie.cc/konrad/Library/models" + "encoding/json" + "strings" + "fmt" +) + +func AuthorAdd(c echo.Context) error { + // Check for Request Content + author := c.FormValue("author") + if author == "" { + return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) + } + + // Decode the JSON + var authorstruct models.Author + dec := json.NewDecoder(strings.NewReader(author)) + + err := dec.Decode(&authorstruct) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()}) + } + + // Insert the author + err = models.AddAuthor(authorstruct) + + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) + } + + return c.JSON(http.StatusOK, models.Message{"success"}) +} diff --git a/routes/api/v1/books_add.go b/routes/api/v1/books_add.go index 196aaa8..bff8b38 100644 --- a/routes/api/v1/books_add.go +++ b/routes/api/v1/books_add.go @@ -21,7 +21,7 @@ func BookAdd(c echo.Context) error { err := dec.Decode(&bookstruct) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()}) } // Insert the book diff --git a/routes/routes.go b/routes/routes.go index 4a53e0b..534bb9a 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -55,6 +55,8 @@ func RegisterRoutes(e *echo.Echo) { a.DELETE("/books/:id", apiv1.BookDelete) // Manage Authors + a.PUT("/authors", apiv1.AuthorAdd) + a.DELETE("/authors/:id", apiv1.AuthorDelete) // Manage Publishers @@ -67,7 +69,7 @@ func RegisterRoutes(e *echo.Echo) { Routes: GET / - entweder übersicht anzeigen (wenn der nutzer eingeloggt ist) oder auf /login weiterleiten - POST /login - Einloggen + POST /login - ✔ Einloggen POST /logout - ausloggen GET /books/:id - ✔ Buch anzeigen