diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 8819baa..bbc71a2 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -7,6 +7,7 @@ import ( "github.com/labstack/echo" "net/http" "strings" + "strconv" ) type authorPayload struct { @@ -17,7 +18,7 @@ type authorPayload struct { func AuthorAddOrUpdate(c echo.Context) error { // Check for Request Content authorFromString := c.FormValue("author") - var authorToInsert models.Author + var datAuthor models.Author if authorFromString == "" { b := new(authorPayload) @@ -25,11 +26,11 @@ func AuthorAddOrUpdate(c echo.Context) error { fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"}) } - authorToInsert = b.Author + datAuthor = b.Author } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(authorFromString)) - err := dec.Decode(&authorToInsert) + err := dec.Decode(&datAuthor) if err != nil { 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 - if authorToInsert.Lastname == "" && authorToInsert.Forename == "" { + if datAuthor.Lastname == "" && datAuthor.Forename == "" { 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 - newAuthor, err := models.AddOrUpdateAuthor(authorToInsert) + if err != nil { + 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 { 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 dfe300a..d254684 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -7,6 +7,7 @@ import ( "github.com/labstack/echo" "net/http" "strings" + "strconv" ) type bookPayload struct { @@ -17,7 +18,7 @@ type bookPayload struct { func BookAddOrUpdate(c echo.Context) error { // Check for Request Content bookFromString := c.FormValue("book") - var bookToInsert models.Book + var datBook models.Book if bookFromString == "" { b := new(bookPayload) @@ -25,24 +26,36 @@ func BookAddOrUpdate(c echo.Context) error { fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"}) } - bookToInsert = b.Book + datBook = b.Book } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(bookFromString)) - err := dec.Decode(&bookToInsert) + err := dec.Decode(&datBook) if err != nil { 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 - if bookToInsert.Title == "" { + if datBook.Title == "" { return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"}) } - // Insert the book - newBook, err := models.AddOrUpdateBook(bookToInsert) + // Insert or update the book + newBook, err := models.AddOrUpdateBook(datBook) 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 4567237..0f44150 100644 --- a/routes/api/v1/publishers_add_update.go +++ b/routes/api/v1/publishers_add_update.go @@ -7,6 +7,7 @@ import ( "github.com/labstack/echo" "net/http" "strings" + "strconv" ) type publisherPayload struct { @@ -17,7 +18,7 @@ type publisherPayload struct { func PublisherAddOrUpdate(c echo.Context) error { // Check for Request Content publisherFromString := c.FormValue("publisher") - var publisherToInsert models.Publisher + var datPublisher models.Publisher if publisherFromString == "" { b := new(publisherPayload) @@ -25,19 +26,32 @@ func PublisherAddOrUpdate(c echo.Context) error { fmt.Println(err) return c.JSON(http.StatusBadRequest, models.Message{"No publisher model provided"}) } - publisherToInsert = b.Publisher + datPublisher = b.Publisher } else { // Decode the JSON dec := json.NewDecoder(strings.NewReader(publisherFromString)) - err := dec.Decode(&publisherToInsert) + err := dec.Decode(&datPublisher) if err != nil { 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 { return c.JSON(http.StatusInternalServerError, models.Message{"Error"})