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
|
// Finally Send it
|
||||||
// If we want to newly insert it, make a different request
|
// If we want to newly insert it, make a different request
|
||||||
if (this.edit) {
|
if (this.edit) {
|
||||||
HTTP.put('books/' + this.book.ID, {book: this.book})
|
HTTP.post('books/' + this.book.ID, {book: this.book})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
this.success = 'The book was successfully updated!'
|
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
|
// 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 AddBook(book Book) (newBook Book, err error) {
|
func AddOrUpdateBook(book Book) (newBook Book, err error) {
|
||||||
|
|
||||||
// Take Publisher, check if it exists. If not, insert it
|
// Take Publisher, check if it exists. If not, insert it
|
||||||
exists := false
|
exists := false
|
||||||
|
@ -36,24 +36,31 @@ func AddBook(book Book) (newBook Book, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
bookToBeInserted := new(Publisher)
|
newPublisher, err := AddPublisher(Publisher{Name:book.PublisherFull.Name})
|
||||||
bookToBeInserted.Name = book.PublisherFull.Name
|
|
||||||
_, err = x.Insert(bookToBeInserted)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Book{}, err
|
return Book{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
book.Publisher = bookToBeInserted.ID
|
book.Publisher = newPublisher.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the quantity for later use
|
// Save the quantity for later use
|
||||||
qty := book.Quantity
|
qty := book.Quantity
|
||||||
|
|
||||||
// Insert the Book
|
// If we have an ID, try to update the book, otherwise create it
|
||||||
_, err = x.Insert(&book)
|
if book.ID == 0 {
|
||||||
|
// Insert the book
|
||||||
|
_, err = x.Insert(&book)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Book{}, err
|
return Book{}, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Update the book
|
||||||
|
_, err := x.Id(book.ID).Update(book)
|
||||||
|
if err != nil {
|
||||||
|
return Book{}, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the Quantity
|
// Set the Quantity
|
||||||
|
|
|
@ -37,7 +37,7 @@ func BookAdd(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the book
|
// Insert the book
|
||||||
newBook, err := models.AddBook(bookToInsert)
|
newBook, err := models.AddOrUpdateBook(bookToInsert)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||||
|
|
|
@ -86,7 +86,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
// Manage Books
|
// Manage Books
|
||||||
a.PUT("/books", apiv1.BookAdd)
|
a.PUT("/books", apiv1.BookAdd)
|
||||||
a.DELETE("/books/:id", apiv1.BookDelete)
|
a.DELETE("/books/:id", apiv1.BookDelete)
|
||||||
a.POST("/books/:id", apiv1.BookDelete)
|
a.POST("/books/:id", apiv1.BookAdd)
|
||||||
|
|
||||||
// Manage Authors
|
// Manage Authors
|
||||||
a.PUT("/authors", apiv1.AuthorAdd)
|
a.PUT("/authors", apiv1.AuthorAdd)
|
||||||
|
|
Loading…
Reference in New Issue