diff --git a/frontend/src/components/BooksAddEdit.vue b/frontend/src/components/BooksAddEdit.vue index 17d0f16..7a9439a 100644 --- a/frontend/src/components/BooksAddEdit.vue +++ b/frontend/src/components/BooksAddEdit.vue @@ -257,7 +257,7 @@ // Finally Send it // If we want to newly insert it, make a different request if (this.edit) { - HTTP.put('books/' + this.book.ID, {book: this.book}) + HTTP.post('books/' + this.book.ID, {book: this.book}) .then(response => { this.loading = false this.success = 'The book was successfully updated!' diff --git a/models/books_add.go b/models/books_add.go index f46e3b4..f9937e3 100644 --- a/models/books_add.go +++ b/models/books_add.go @@ -15,8 +15,8 @@ sie in die Datenbank eingetragen und mit dem Buch verknüpft. */ -// AddBook adds a new book, it takes a book struct with author and publisher. Inserts them if they don't already exist -func AddBook(book Book) (newBook Book, err error) { +// AddOrUpdateBook adds a new book or updates an existing one, it takes a book struct with author and publisher. Inserts them if they don't already exist +func AddOrUpdateBook(book Book) (newBook Book, err error) { // Take Publisher, check if it exists. If not, insert it exists := false @@ -36,24 +36,31 @@ func AddBook(book Book) (newBook Book, err error) { } if !exists { - bookToBeInserted := new(Publisher) - bookToBeInserted.Name = book.PublisherFull.Name - _, err = x.Insert(bookToBeInserted) + newPublisher, err := AddPublisher(Publisher{Name:book.PublisherFull.Name}) if err != nil { return Book{}, err } - book.Publisher = bookToBeInserted.ID + book.Publisher = newPublisher.ID } // Save the quantity for later use qty := book.Quantity - // Insert the Book - _, err = x.Insert(&book) + // If we have an ID, try to update the book, otherwise create it + if book.ID == 0 { + // Insert the book + _, err = x.Insert(&book) - if err != nil { - return Book{}, err + if err != nil { + return Book{}, err + } + } else { + // Update the book + _, err := x.Id(book.ID).Update(book) + if err != nil { + return Book{}, err + } } // Set the Quantity diff --git a/routes/api/v1/books_add.go b/routes/api/v1/books_add.go index 3a4b90d..e04356b 100644 --- a/routes/api/v1/books_add.go +++ b/routes/api/v1/books_add.go @@ -37,7 +37,7 @@ func BookAdd(c echo.Context) error { } // Insert the book - newBook, err := models.AddBook(bookToInsert) + newBook, err := models.AddOrUpdateBook(bookToInsert) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) diff --git a/routes/routes.go b/routes/routes.go index a208153..f986eec 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -86,7 +86,7 @@ func RegisterRoutes(e *echo.Echo) { // Manage Books a.PUT("/books", apiv1.BookAdd) a.DELETE("/books/:id", apiv1.BookDelete) - a.POST("/books/:id", apiv1.BookDelete) + a.POST("/books/:id", apiv1.BookAdd) // Manage Authors a.PUT("/authors", apiv1.AuthorAdd)