2017-10-09 16:29:46 +00:00
|
|
|
package models
|
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// Book holds a book
|
2017-10-09 16:29:46 +00:00
|
|
|
type Book struct {
|
2017-11-07 15:35:10 +00:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
|
|
|
Title string `xorm:"varchar(250) not null"`
|
|
|
|
Isbn string `xorm:"varchar(30)"`
|
|
|
|
Year int64 `xorm:"int(11)"`
|
|
|
|
Price float64 `xorm:"double"`
|
|
|
|
Status int64 `xorm:"int(11)"`
|
|
|
|
Publisher int64 `xorm:"int(11)"`
|
|
|
|
Created int64 `xorm:"created"`
|
|
|
|
Updated int64 `xorm:"updated"`
|
2017-10-10 10:06:34 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
Quantity int64 `xorm:"-"`
|
2017-10-10 10:06:34 +00:00
|
|
|
PublisherFull Publisher `xorm:"-"`
|
2017-11-07 15:35:10 +00:00
|
|
|
Authors []Author `xorm:"-"`
|
2017-10-09 16:29:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// TableName returns the name for the books table
|
2017-11-07 15:35:10 +00:00
|
|
|
func (Book) TableName() string {
|
2017-10-09 16:29:46 +00:00
|
|
|
return "books"
|
2017-10-10 15:01:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:51:23 +00:00
|
|
|
// Quantity is the quantity for a book
|
|
|
|
type Quantity struct {
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
|
|
|
BookID int64 `xorm:"int(11) not null"`
|
|
|
|
Quantity int64 `xorm:"int(11) not null"`
|
|
|
|
Created int64 `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the name for the quantites table
|
|
|
|
func (Quantity) TableName() string {
|
|
|
|
return "books_quantities"
|
|
|
|
}
|
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// GetBookByID gets a Book by its ID
|
|
|
|
func GetBookByID(ID int64) (book Book, exists bool, err error) {
|
2017-10-10 15:14:51 +00:00
|
|
|
// Get the Book
|
2017-10-10 16:11:09 +00:00
|
|
|
has, err := x.ID(ID).Get(&book)
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
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
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
// 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
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
book.Authors = authors
|
|
|
|
}
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-10-10 16:11:09 +00:00
|
|
|
return book, has, err
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|