Implemented book quantity insert when a book is inserted
the build was successful
Details
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:
parent
19852d0b6a
commit
fb305e5b39
|
@ -1,6 +1,8 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// Book holds a book
|
// Book holds a book
|
||||||
type Book struct {
|
type Book struct {
|
||||||
|
@ -14,6 +16,7 @@ type Book struct {
|
||||||
Created int64 `xorm:"created"`
|
Created int64 `xorm:"created"`
|
||||||
Updated int64 `xorm:"updated"`
|
Updated int64 `xorm:"updated"`
|
||||||
|
|
||||||
|
Quantity int64 `xorm:"-"`
|
||||||
PublisherFull Publisher `xorm:"-"`
|
PublisherFull Publisher `xorm:"-"`
|
||||||
Authors []Author `xorm:"-"`
|
Authors []Author `xorm:"-"`
|
||||||
}
|
}
|
||||||
|
@ -29,7 +32,6 @@ type Quantity struct {
|
||||||
BookID int64 `xorm:"int(11) not null"`
|
BookID int64 `xorm:"int(11) not null"`
|
||||||
Quantity int64 `xorm:"int(11) not null"`
|
Quantity int64 `xorm:"int(11) not null"`
|
||||||
Created int64 `xorm:"created"`
|
Created int64 `xorm:"created"`
|
||||||
Updated int64 `xorm:"updated"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName returns the name for the quantites table
|
// TableName returns the name for the quantites table
|
||||||
|
@ -37,33 +39,65 @@ func (Quantity) TableName() string {
|
||||||
return "books_quantities"
|
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
|
// GetBookByID gets a Book by its ID
|
||||||
func GetBookByID(ID int64) (book Book, exists bool, err error) {
|
func GetBookByID(ID int64) (book Book, exists bool, err error) {
|
||||||
// Get the Book
|
// Get the Book
|
||||||
has, err := x.ID(ID).Get(&book)
|
has, err := x.ID(ID).Get(&book)
|
||||||
|
|
||||||
// Get publisher
|
if has {
|
||||||
publisher := Publisher{ID: book.Publisher}
|
// Get the books quantity. We can't join it because xorm ignores the Quantity option in struc
|
||||||
_, err = x.Get(&publisher)
|
qt := Quantity{BookID: ID}
|
||||||
if err != nil {
|
has, err = x.Desc("id").Get(&qt)
|
||||||
fmt.Println("Error getting publisher:", err)
|
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
|
return book, has, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,9 @@ func AddBook(book Book) (newBook Book, err error) {
|
||||||
book.Publisher = bookToBeInserted.ID
|
book.Publisher = bookToBeInserted.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the quantity for later use
|
||||||
|
qty := book.Quantity
|
||||||
|
|
||||||
// Insert the Book
|
// Insert the Book
|
||||||
_, err = x.Insert(&book)
|
_, err = x.Insert(&book)
|
||||||
|
|
||||||
|
@ -53,6 +56,12 @@ func AddBook(book Book) (newBook Book, err error) {
|
||||||
return Book{}, err
|
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
|
// Take the authors, look if they exist, if they don't create them
|
||||||
authorBookRelation := make([]AuthorBook, 0)
|
authorBookRelation := make([]AuthorBook, 0)
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"git.mowie.cc/konrad/Library/models"
|
"git.mowie.cc/konrad/Library/models"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type bookPayload struct {
|
type bookPayload struct {
|
||||||
|
@ -13,13 +16,28 @@ type bookPayload struct {
|
||||||
// BookAdd is the handler to add a book
|
// BookAdd is the handler to add a book
|
||||||
func BookAdd(c echo.Context) error {
|
func BookAdd(c echo.Context) error {
|
||||||
// Check for Request Content
|
// Check for Request Content
|
||||||
b := new(bookPayload)
|
bookFromString := c.FormValue("book")
|
||||||
if err := c.Bind(b); err != nil {
|
var bookToInsert models.Book
|
||||||
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
|
|
||||||
|
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
|
// Insert the book
|
||||||
newBook, err := models.AddBook(b.Book)
|
newBook, err := models.AddBook(bookToInsert)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||||
|
|
Loading…
Reference in New Issue