From 78fe10094e7870200f09225bbd4e1a6b3618d8c9 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 7 Dec 2017 16:17:18 +0100 Subject: [PATCH] Getting JSON Objects improvements --- routes/api/v1/authors_add_update.go | 12 +++--------- routes/api/v1/books_add_update.go | 12 +++--------- routes/api/v1/items_add_update.go | 12 +++--------- routes/api/v1/publishers_add_update.go | 13 ++++--------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index fae51ff..710a0c5 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -9,22 +9,16 @@ import ( "strings" ) -type authorPayload struct { - Author models.Author `json:"author" form:"author"` -} - // AuthorAddOrUpdate is the handler to add or update an author func AuthorAddOrUpdate(c echo.Context) error { // Check for Request Content authorFromString := c.FormValue("author") - var datAuthor models.Author + var datAuthor *models.Author if authorFromString == "" { - b := new(authorPayload) - if err := c.Bind(b); err != nil { + if err := c.Bind(&datAuthor); err != nil { return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) } - datAuthor = b.Author } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(authorFromString)) @@ -53,7 +47,7 @@ func AuthorAddOrUpdate(c echo.Context) error { } // Insert or update the author - newAuthor, err := models.AddOrUpdateAuthor(datAuthor) + newAuthor, err := models.AddOrUpdateAuthor(*datAuthor) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index e84fcd9..de218fc 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -9,22 +9,16 @@ import ( "strings" ) -type bookPayload struct { - Book models.Book `json:"book" form:"book"` -} - // BookAddOrUpdate is the handler to add a book func BookAddOrUpdate(c echo.Context) error { // Check for Request Content bookFromString := c.FormValue("book") - var datBook models.Book + var datBook *models.Book if bookFromString == "" { - b := new(bookPayload) - if err := c.Bind(b); err != nil { + if err := c.Bind(&datBook); err != nil { return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"}) } - datBook = b.Book } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(bookFromString)) @@ -53,7 +47,7 @@ func BookAddOrUpdate(c echo.Context) error { } // Insert or update the book - newBook, err := models.AddOrUpdateBook(datBook) + newBook, err := models.AddOrUpdateBook(*datBook) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) diff --git a/routes/api/v1/items_add_update.go b/routes/api/v1/items_add_update.go index 7b9bd0c..26a0df9 100644 --- a/routes/api/v1/items_add_update.go +++ b/routes/api/v1/items_add_update.go @@ -9,22 +9,16 @@ import ( "strings" ) -type itemPayload struct { - Item models.Item `json:"item" form:"item"` -} - // ItemAddOrUpdate is the handler to add a item func ItemAddOrUpdate(c echo.Context) error { // Check for Request Content itemFromString := c.FormValue("item") - var datItem models.Item + var datItem *models.Item if itemFromString == "" { - b := new(itemPayload) - if err := c.Bind(b); err != nil { + if err := c.Bind(&datItem); err != nil { return c.JSON(http.StatusBadRequest, models.Message{"No item model provided"}) } - datItem = b.Item } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(itemFromString)) @@ -48,7 +42,7 @@ func ItemAddOrUpdate(c echo.Context) error { } // Insert or update the item - newItem, err := models.AddOrUpdateItem(datItem) + newItem, err := models.AddOrUpdateItem(*datItem) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) diff --git a/routes/api/v1/publishers_add_update.go b/routes/api/v1/publishers_add_update.go index 57611e2..55eafc8 100644 --- a/routes/api/v1/publishers_add_update.go +++ b/routes/api/v1/publishers_add_update.go @@ -9,22 +9,17 @@ import ( "strings" ) -type publisherPayload struct { - Publisher models.Publisher `json:"publisher" form:"publisher"` -} - // PublisherAddOrUpdate is the handler to add a publisher func PublisherAddOrUpdate(c echo.Context) error { // Check for Request Content publisherFromString := c.FormValue("publisher") - var datPublisher models.Publisher + var datPublisher *models.Publisher if publisherFromString == "" { - b := new(publisherPayload) - if err := c.Bind(b); err != nil { + // b := new(models.Publisher) + if err := c.Bind(&datPublisher); err != nil { return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) } - datPublisher = b.Publisher } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(publisherFromString)) @@ -48,7 +43,7 @@ func PublisherAddOrUpdate(c echo.Context) error { } // Insert or update the publisher - newPublisher, err := models.AddOrUpdatePublisher(datPublisher) + newPublisher, err := models.AddOrUpdatePublisher(*datPublisher) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error"})