Repared update methods
the build failed Details

This commit is contained in:
kolaente 2017-11-28 15:29:51 +01:00
parent c87ce78598
commit 2744214185
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 57 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strings" "strings"
"strconv"
) )
type authorPayload struct { type authorPayload struct {
@ -17,7 +18,7 @@ type authorPayload struct {
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 authorToInsert models.Author var datAuthor models.Author
if authorFromString == "" { if authorFromString == "" {
b := new(authorPayload) b := new(authorPayload)
@ -25,11 +26,11 @@ func AuthorAddOrUpdate(c echo.Context) error {
fmt.Println(err) fmt.Println(err)
return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"})
} }
authorToInsert = b.Author datAuthor = b.Author
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(authorFromString)) dec := json.NewDecoder(strings.NewReader(authorFromString))
err := dec.Decode(&authorToInsert) err := dec.Decode(&datAuthor)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()}) return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()})
@ -37,12 +38,24 @@ func AuthorAddOrUpdate(c echo.Context) error {
} }
// Check if we have at least a Lastname // Check if we have at least a Lastname
if authorToInsert.Lastname == "" && authorToInsert.Forename == "" { if datAuthor.Lastname == "" && datAuthor.Forename == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least one name."}) return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least one name."})
} }
// Check if we have an ID other than the one in the struct
id := c.Param("id")
if id != "" {
// Make int
authorID, err := strconv.ParseInt(id, 10, 64)
// Insert the author if err != nil {
newAuthor, err := models.AddOrUpdateAuthor(authorToInsert) return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id"})
}
datAuthor.ID = authorID
}
// Insert or update the author
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

@ -7,6 +7,7 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strings" "strings"
"strconv"
) )
type bookPayload struct { type bookPayload struct {
@ -17,7 +18,7 @@ type bookPayload struct {
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 bookToInsert models.Book var datBook models.Book
if bookFromString == "" { if bookFromString == "" {
b := new(bookPayload) b := new(bookPayload)
@ -25,24 +26,36 @@ func BookAddOrUpdate(c echo.Context) error {
fmt.Println(err) fmt.Println(err)
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
} }
bookToInsert = b.Book datBook = b.Book
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(bookFromString)) dec := json.NewDecoder(strings.NewReader(bookFromString))
err := dec.Decode(&bookToInsert) 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.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
} }
} }
// Check if we have an ID other than the one in the struct
id := c.Param("id")
if id != "" {
// Make int
bookID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id"})
}
datBook.ID = bookID
}
// Check if we have at least a title // Check if we have at least a title
if bookToInsert.Title == "" { if datBook.Title == "" {
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!"})
} }
// Insert the book // Insert or update the book
newBook, err := models.AddOrUpdateBook(bookToInsert) 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

@ -7,6 +7,7 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strings" "strings"
"strconv"
) )
type publisherPayload struct { type publisherPayload struct {
@ -17,7 +18,7 @@ type publisherPayload struct {
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 publisherToInsert models.Publisher var datPublisher models.Publisher
if publisherFromString == "" { if publisherFromString == "" {
b := new(publisherPayload) b := new(publisherPayload)
@ -25,19 +26,32 @@ func PublisherAddOrUpdate(c echo.Context) error {
fmt.Println(err) fmt.Println(err)
return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"})
} }
publisherToInsert = b.Publisher datPublisher = b.Publisher
} else { } else {
// Decode the JSON // Decode the JSON
dec := json.NewDecoder(strings.NewReader(publisherFromString)) dec := json.NewDecoder(strings.NewReader(publisherFromString))
err := dec.Decode(&publisherToInsert) 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.StatusInternalServerError, models.Message{"Error decoding publisher: " + err.Error()})
} }
} }
// Insert the publisher
newPublisher, err := models.AddOrUpdatePublisher(publisherToInsert) // Check if we have an ID other than the one in the struct
id := c.Param("id")
if id != "" {
// Make int
publisherID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id"})
}
datPublisher.ID = publisherID
}
// Insert or update the publisher
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"})