From 1911ec4d5f2c3c538ac28069d81a1b8f5b6e6506 Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 15 Jan 2018 13:08:17 +0100 Subject: [PATCH] The api now returns more specific error codes --- routes/api/v1/author_delete.go | 10 +++++----- routes/api/v1/authors_add_update.go | 16 ++++++++++++++-- routes/api/v1/book_delete.go | 10 +++++----- routes/api/v1/books_add_update.go | 14 +++++++++++++- routes/api/v1/items_add_update.go | 14 +++++++++++++- routes/api/v1/items_delete.go | 4 ++-- routes/api/v1/publishers_add_update.go | 14 +++++++++++++- routes/api/v1/publishers_delete.go | 6 +++--- 8 files changed, 68 insertions(+), 20 deletions(-) diff --git a/routes/api/v1/author_delete.go b/routes/api/v1/author_delete.go index 5049902..d458a0e 100644 --- a/routes/api/v1/author_delete.go +++ b/routes/api/v1/author_delete.go @@ -16,31 +16,31 @@ func AuthorDelete(c echo.Context) error { authorID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get author infos"}) + return c.JSON(http.StatusBadRequest, models.Message{"Author ID is invalid."}) } // Check if the author exists _, exists, err := models.GetAuthorByID(authorID) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could get author"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get author."}) } if !exists { - return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."}) + return c.JSON(http.StatusNotFound, 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.StatusInternalServerError, models.Message{"Could not delete author."}) } // Log the action err = models.LogAction("Deleted an author", authorID, c) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."}) } return c.JSON(http.StatusOK, models.Message{"success"}) diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 710a0c5..7f1e551 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -17,7 +17,7 @@ func AuthorAddOrUpdate(c echo.Context) error { if authorFromString == "" { if err := c.Bind(&datAuthor); err != nil { - return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) + return c.JSON(http.StatusBadRequest, models.Message{"No author model provided."}) } } else { // Decode the JSON @@ -41,11 +41,23 @@ func AuthorAddOrUpdate(c echo.Context) error { authorID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id."}) } datAuthor.ID = authorID } + // Check if the author exists + if datAuthor.ID != 0 { + _, exists, err := models.GetAuthorByID(datAuthor.ID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the author exists"}) + } + + if !exists { + return c.JSON(http.StatusNotFound, models.Message{"The author does not exist"}) + } + } + // Insert or update the author newAuthor, err := models.AddOrUpdateAuthor(*datAuthor) diff --git a/routes/api/v1/book_delete.go b/routes/api/v1/book_delete.go index 19882e0..2621cff 100644 --- a/routes/api/v1/book_delete.go +++ b/routes/api/v1/book_delete.go @@ -16,31 +16,31 @@ func BookDelete(c echo.Context) error { bookID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book infos"}) + return c.JSON(http.StatusBadRequest, models.Message{"Book ID is invalid."}) } // Check if the book exists _, exists, err := models.GetBookByID(bookID) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could get book"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book."}) } if !exists { - return c.JSON(http.StatusBadRequest, models.Message{"The book does not exist."}) + return c.JSON(http.StatusNotFound, 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.StatusInternalServerError, models.Message{"Could not delete book."}) } // Log the action err = models.LogAction("Deleted a book", bookID, c) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."}) } return c.JSON(http.StatusOK, models.Message{"success"}) diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index de218fc..97546bb 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -25,7 +25,7 @@ func BookAddOrUpdate(c echo.Context) error { err := dec.Decode(&datBook) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()}) + return c.JSON(http.StatusBadRequest, models.Message{"Error decoding book: " + err.Error()}) } } @@ -41,6 +41,18 @@ func BookAddOrUpdate(c echo.Context) error { datBook.ID = bookID } + // Check if the book exists + if datBook.ID != 0 { + _, exists, err := models.GetBookByID(datBook.ID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the book exists"}) + } + + if !exists { + return c.JSON(http.StatusNotFound, models.Message{"The book does not exist"}) + } + } + // Check if we have at least a title if datBook.Title == "" && datBook.ID == 0 { return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"}) diff --git a/routes/api/v1/items_add_update.go b/routes/api/v1/items_add_update.go index 26a0df9..4620560 100644 --- a/routes/api/v1/items_add_update.go +++ b/routes/api/v1/items_add_update.go @@ -25,7 +25,7 @@ func ItemAddOrUpdate(c echo.Context) error { err := dec.Decode(&datItem) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding item: " + err.Error()}) + return c.JSON(http.StatusBadRequest, models.Message{"Error decoding item: " + err.Error()}) } } @@ -41,6 +41,18 @@ func ItemAddOrUpdate(c echo.Context) error { datItem.ID = itemID } + // Check if the item exists + if datItem.ID != 0 { + _, exists, err := models.GetItemByID(datItem.ID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the item exists"}) + } + + if !exists { + return c.JSON(http.StatusNotFound, models.Message{"The item does not exist"}) + } + } + // Insert or update the item newItem, err := models.AddOrUpdateItem(*datItem) diff --git a/routes/api/v1/items_delete.go b/routes/api/v1/items_delete.go index 51751c3..ec52041 100644 --- a/routes/api/v1/items_delete.go +++ b/routes/api/v1/items_delete.go @@ -16,14 +16,14 @@ func ItemDelete(c echo.Context) error { itemID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get item infos"}) + return c.JSON(http.StatusBadRequest, models.Message{"Item ID is invalid."}) } // Check if the item exists _, exists, err := models.GetItemByID(itemID) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could get item"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get item"}) } if !exists { diff --git a/routes/api/v1/publishers_add_update.go b/routes/api/v1/publishers_add_update.go index 55eafc8..c3c85a2 100644 --- a/routes/api/v1/publishers_add_update.go +++ b/routes/api/v1/publishers_add_update.go @@ -26,7 +26,7 @@ func PublisherAddOrUpdate(c echo.Context) error { err := dec.Decode(&datPublisher) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding publisher: " + err.Error()}) + return c.JSON(http.StatusBadRequest, models.Message{"Error decoding publisher: " + err.Error()}) } } @@ -42,6 +42,18 @@ func PublisherAddOrUpdate(c echo.Context) error { datPublisher.ID = publisherID } + // Check if the publisher exists + if datPublisher.ID != 0 { + _, exists, err := models.GetPublisherByID(datPublisher.ID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the publisher exists"}) + } + + if !exists { + return c.JSON(http.StatusNotFound, models.Message{"The publisher does not exist"}) + } + } + // Insert or update the publisher newPublisher, err := models.AddOrUpdatePublisher(*datPublisher) diff --git a/routes/api/v1/publishers_delete.go b/routes/api/v1/publishers_delete.go index 0251fc8..e7e0a91 100644 --- a/routes/api/v1/publishers_delete.go +++ b/routes/api/v1/publishers_delete.go @@ -16,18 +16,18 @@ func PublisherDelete(c echo.Context) error { publisherID, err := strconv.ParseInt(id, 10, 64) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher infos"}) + return c.JSON(http.StatusBadRequest, models.Message{"Publisher ID is invalid"}) } // Check if the publisher exists _, exists, err := models.GetPublisherByID(publisherID) if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could get publisher"}) + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher"}) } if !exists { - return c.JSON(http.StatusBadRequest, models.Message{"The publisher does not exist."}) + return c.JSON(http.StatusNotFound, models.Message{"The publisher does not exist."}) } // Delete it