Implemented book quantity insert when a book is inserted
the build was successful Details

+ Books can now either send through a direct json payload or via one form value
+ lint & gofmt
This commit is contained in:
konrad 2017-11-16 19:07:32 +01:00 committed by kolaente
parent 19852d0b6a
commit fb305e5b39
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 88 additions and 27 deletions

View File

@ -1,6 +1,8 @@
package models
import "fmt"
import (
"fmt"
)
// Book holds a book
type Book struct {
@ -14,6 +16,7 @@ type Book struct {
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
Quantity int64 `xorm:"-"`
PublisherFull Publisher `xorm:"-"`
Authors []Author `xorm:"-"`
}
@ -29,7 +32,6 @@ type Quantity struct {
BookID int64 `xorm:"int(11) not null"`
Quantity int64 `xorm:"int(11) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
}
// TableName returns the name for the quantites table
@ -37,33 +39,65 @@ func (Quantity) TableName() string {
return "books_quantities"
}
// GetQuantityByBook returns the current quantity for a book
func GetQuantityByBook(bookID int64) (quantity int64, err error) {
bq := Quantity{BookID: bookID}
has, err := x.Desc("id").Get(&bq)
if err != nil {
return 0, err
}
if has {
quantity = bq.Quantity
}
return quantity, nil
}
// SetBookQuantity sets a new quantity for a book
func (book Book) setBookQuantity(quantity int64) (err error) {
q := Quantity{BookID: book.ID, Quantity: quantity}
_, err = x.Insert(q)
return err
}
// GetBookByID gets a Book by its ID
func GetBookByID(ID int64) (book Book, exists bool, err error) {
// Get the Book
has, err := x.ID(ID).Get(&book)
// Get publisher
publisher := Publisher{ID: book.Publisher}
_, err = x.Get(&publisher)
if err != nil {
fmt.Println("Error getting publisher:", err)
if has {
// Get the books quantity. We can't join it because xorm ignores the Quantity option in struc
qt := Quantity{BookID: ID}
has, err = x.Desc("id").Get(&qt)
if err != nil {
fmt.Println("Error getting quantity:", err)
}
book.Quantity = qt.Quantity
// Get publisher. We can't join it because xorm ignores the PublisherID option in struct
publisher := Publisher{ID: book.Publisher}
_, err = x.Get(&publisher)
if err != nil {
fmt.Println("Error getting publisher:", err)
}
book.PublisherFull = publisher
// Get all authors
var authors []Author
err = x.
Table("authors_books").
Select("authors.*").
Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", book.ID).
Find(&authors)
if err != nil {
fmt.Println("Error getting authors:", err)
}
book.Authors = authors
}
book.PublisherFull = publisher
// Get all authors
var authors []Author
err = x.
Table("authors_books").
Select("authors.*").
Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", book.ID).
Find(&authors)
if err != nil {
fmt.Println("Error getting authors:", err)
}
book.Authors = authors
return book, has, err
}

View File

@ -46,6 +46,9 @@ func AddBook(book Book) (newBook Book, err error) {
book.Publisher = bookToBeInserted.ID
}
// Save the quantity for later use
qty := book.Quantity
// Insert the Book
_, err = x.Insert(&book)
@ -53,6 +56,12 @@ func AddBook(book Book) (newBook Book, err error) {
return Book{}, err
}
// Set the Quantity
err = book.setBookQuantity(qty)
if err != nil {
return Book{}, err
}
// Take the authors, look if they exist, if they don't create them
authorBookRelation := make([]AuthorBook, 0)

View File

@ -1,9 +1,12 @@
package v1
import (
"encoding/json"
"fmt"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"strings"
)
type bookPayload struct {
@ -13,13 +16,28 @@ type bookPayload struct {
// BookAdd is the handler to add a book
func BookAdd(c echo.Context) error {
// Check for Request Content
b := new(bookPayload)
if err := c.Bind(b); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
bookFromString := c.FormValue("book")
var bookToInsert models.Book
if bookFromString == "" {
b := new(bookPayload)
if err := c.Bind(b); err != nil {
fmt.Println(err)
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
}
bookToInsert = b.Book
} else {
// Decode the JSON
dec := json.NewDecoder(strings.NewReader(bookFromString))
err := dec.Decode(&bookToInsert)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
}
}
// Insert the book
newBook, err := models.AddBook(b.Book)
newBook, err := models.AddBook(bookToInsert)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})