The api now returns more specific error codes
the build failed Details

This commit is contained in:
konrad 2018-01-15 13:08:17 +01:00 committed by kolaente
parent 82d596994a
commit 1911ec4d5f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
8 changed files with 68 additions and 20 deletions

View File

@ -16,31 +16,31 @@ func AuthorDelete(c echo.Context) error {
authorID, err := strconv.ParseInt(id, 10, 64) authorID, err := strconv.ParseInt(id, 10, 64)
if err != nil { 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 // Check if the author exists
_, exists, err := models.GetAuthorByID(authorID) _, exists, err := models.GetAuthorByID(authorID)
if err != nil { 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 { 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 // Delete it
err = models.DeleteAuthorByID(authorID) err = models.DeleteAuthorByID(authorID)
if err != nil { 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 // Log the action
err = models.LogAction("Deleted an author", authorID, c) err = models.LogAction("Deleted an author", authorID, c)
if err != nil { 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"}) return c.JSON(http.StatusOK, models.Message{"success"})

View File

@ -17,7 +17,7 @@ func AuthorAddOrUpdate(c echo.Context) error {
if authorFromString == "" { if authorFromString == "" {
if err := c.Bind(&datAuthor); err != nil { 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 { } else {
// Decode the JSON // Decode the JSON
@ -41,11 +41,23 @@ func AuthorAddOrUpdate(c echo.Context) error {
authorID, err := strconv.ParseInt(id, 10, 64) authorID, err := strconv.ParseInt(id, 10, 64)
if err != nil { 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 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 // Insert or update the author
newAuthor, err := models.AddOrUpdateAuthor(*datAuthor) newAuthor, err := models.AddOrUpdateAuthor(*datAuthor)

View File

@ -16,31 +16,31 @@ func BookDelete(c echo.Context) error {
bookID, err := strconv.ParseInt(id, 10, 64) bookID, err := strconv.ParseInt(id, 10, 64)
if err != nil { 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 // Check if the book exists
_, exists, err := models.GetBookByID(bookID) _, exists, err := models.GetBookByID(bookID)
if err != nil { 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 { 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 // Delete it
err = models.DeleteBookByID(bookID) err = models.DeleteBookByID(bookID)
if err != nil { 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 // Log the action
err = models.LogAction("Deleted a book", bookID, c) err = models.LogAction("Deleted a book", bookID, c)
if err != nil { 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"}) return c.JSON(http.StatusOK, models.Message{"success"})

View File

@ -25,7 +25,7 @@ func BookAddOrUpdate(c echo.Context) error {
err := dec.Decode(&datBook) err := dec.Decode(&datBook)
if err != nil { 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 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 // Check if we have at least a title
if datBook.Title == "" && datBook.ID == 0 { if datBook.Title == "" && datBook.ID == 0 {
return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"}) return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"})

View File

@ -25,7 +25,7 @@ func ItemAddOrUpdate(c echo.Context) error {
err := dec.Decode(&datItem) err := dec.Decode(&datItem)
if err != nil { 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 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 // Insert or update the item
newItem, err := models.AddOrUpdateItem(*datItem) newItem, err := models.AddOrUpdateItem(*datItem)

View File

@ -16,14 +16,14 @@ func ItemDelete(c echo.Context) error {
itemID, err := strconv.ParseInt(id, 10, 64) itemID, err := strconv.ParseInt(id, 10, 64)
if err != nil { 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 // Check if the item exists
_, exists, err := models.GetItemByID(itemID) _, exists, err := models.GetItemByID(itemID)
if err != nil { 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 { if !exists {

View File

@ -26,7 +26,7 @@ func PublisherAddOrUpdate(c echo.Context) error {
err := dec.Decode(&datPublisher) err := dec.Decode(&datPublisher)
if err != nil { 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 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 // Insert or update the publisher
newPublisher, err := models.AddOrUpdatePublisher(*datPublisher) newPublisher, err := models.AddOrUpdatePublisher(*datPublisher)

View File

@ -16,18 +16,18 @@ func PublisherDelete(c echo.Context) error {
publisherID, err := strconv.ParseInt(id, 10, 64) publisherID, err := strconv.ParseInt(id, 10, 64)
if err != nil { 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 // Check if the publisher exists
_, exists, err := models.GetPublisherByID(publisherID) _, exists, err := models.GetPublisherByID(publisherID)
if err != nil { 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 { 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 // Delete it