Getting JSON Objects improvements
the build was successful Details

This commit is contained in:
kolaente 2017-12-07 16:17:18 +01:00 committed by kolaente
parent f41a197cc5
commit 78fe10094e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 13 additions and 36 deletions

View File

@ -9,22 +9,16 @@ import (
"strings" "strings"
) )
type authorPayload struct {
Author models.Author `json:"author" form:"author"`
}
// AuthorAddOrUpdate is the handler to add or update an author // AuthorAddOrUpdate is the handler to add or update an author
func AuthorAddOrUpdate(c echo.Context) error { func AuthorAddOrUpdate(c echo.Context) error {
// Check for Request Content // Check for Request Content
authorFromString := c.FormValue("author") authorFromString := c.FormValue("author")
var datAuthor models.Author var datAuthor *models.Author
if authorFromString == "" { if authorFromString == "" {
b := new(authorPayload) if err := c.Bind(&datAuthor); err != nil {
if err := c.Bind(b); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"})
} }
datAuthor = b.Author
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(authorFromString)) dec := json.NewDecoder(strings.NewReader(authorFromString))
@ -53,7 +47,7 @@ func AuthorAddOrUpdate(c echo.Context) error {
} }
// Insert or update the author // Insert or update the author
newAuthor, err := models.AddOrUpdateAuthor(datAuthor) newAuthor, err := models.AddOrUpdateAuthor(*datAuthor)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})

View File

@ -9,22 +9,16 @@ import (
"strings" "strings"
) )
type bookPayload struct {
Book models.Book `json:"book" form:"book"`
}
// BookAddOrUpdate is the handler to add a book // BookAddOrUpdate is the handler to add a book
func BookAddOrUpdate(c echo.Context) error { func BookAddOrUpdate(c echo.Context) error {
// Check for Request Content // Check for Request Content
bookFromString := c.FormValue("book") bookFromString := c.FormValue("book")
var datBook models.Book var datBook *models.Book
if bookFromString == "" { if bookFromString == "" {
b := new(bookPayload) if err := c.Bind(&datBook); err != nil {
if err := c.Bind(b); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
} }
datBook = b.Book
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(bookFromString)) dec := json.NewDecoder(strings.NewReader(bookFromString))
@ -53,7 +47,7 @@ func BookAddOrUpdate(c echo.Context) error {
} }
// Insert or update the book // Insert or update the book
newBook, err := models.AddOrUpdateBook(datBook) newBook, err := models.AddOrUpdateBook(*datBook)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})

View File

@ -9,22 +9,16 @@ import (
"strings" "strings"
) )
type itemPayload struct {
Item models.Item `json:"item" form:"item"`
}
// ItemAddOrUpdate is the handler to add a item // ItemAddOrUpdate is the handler to add a item
func ItemAddOrUpdate(c echo.Context) error { func ItemAddOrUpdate(c echo.Context) error {
// Check for Request Content // Check for Request Content
itemFromString := c.FormValue("item") itemFromString := c.FormValue("item")
var datItem models.Item var datItem *models.Item
if itemFromString == "" { if itemFromString == "" {
b := new(itemPayload) if err := c.Bind(&datItem); err != nil {
if err := c.Bind(b); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No item model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No item model provided"})
} }
datItem = b.Item
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(itemFromString)) dec := json.NewDecoder(strings.NewReader(itemFromString))
@ -48,7 +42,7 @@ func ItemAddOrUpdate(c echo.Context) error {
} }
// Insert or update the item // Insert or update the item
newItem, err := models.AddOrUpdateItem(datItem) newItem, err := models.AddOrUpdateItem(*datItem)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})

View File

@ -9,22 +9,17 @@ import (
"strings" "strings"
) )
type publisherPayload struct {
Publisher models.Publisher `json:"publisher" form:"publisher"`
}
// PublisherAddOrUpdate is the handler to add a publisher // PublisherAddOrUpdate is the handler to add a publisher
func PublisherAddOrUpdate(c echo.Context) error { func PublisherAddOrUpdate(c echo.Context) error {
// Check for Request Content // Check for Request Content
publisherFromString := c.FormValue("publisher") publisherFromString := c.FormValue("publisher")
var datPublisher models.Publisher var datPublisher *models.Publisher
if publisherFromString == "" { if publisherFromString == "" {
b := new(publisherPayload) // b := new(models.Publisher)
if err := c.Bind(b); err != nil { if err := c.Bind(&datPublisher); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"})
} }
datPublisher = b.Publisher
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(publisherFromString)) dec := json.NewDecoder(strings.NewReader(publisherFromString))
@ -48,7 +43,7 @@ func PublisherAddOrUpdate(c echo.Context) error {
} }
// Insert or update the publisher // Insert or update the publisher
newPublisher, err := models.AddOrUpdatePublisher(datPublisher) newPublisher, err := models.AddOrUpdatePublisher(*datPublisher)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) return c.JSON(http.StatusInternalServerError, models.Message{"Error"})