Finished Book update method
the build failed
Details
the build failed
Details
This commit is contained in:
parent
3a24e8a1b9
commit
c160f5720b
|
@ -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!'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"})
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue