2017-10-09 16:29:46 +00:00
|
|
|
package models
|
|
|
|
|
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-29 15:43:58 +00:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
|
|
|
Title string `xorm:"varchar(250) not null" json:"title"`
|
|
|
|
Description string `xorm:"varchar(750)" json:"description"`
|
|
|
|
Isbn string `xorm:"varchar(30)" json:"isbn"`
|
|
|
|
Year int64 `xorm:"int(11)" json:"year"`
|
|
|
|
Price float64 `xorm:"double" json:"price"`
|
|
|
|
Status int64 `xorm:"int(11)" json:"status"`
|
|
|
|
PublisherID int64 `xorm:"int(11)" json:"publisherID"`
|
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
|
|
|
|
|
|
|
Quantity int64 `xorm:"-" json:"quantity"`
|
|
|
|
Publisher Publisher `xorm:"-" json:"publisher"`
|
|
|
|
Authors []Author `xorm:"-" json:"authors"`
|
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-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 {
|
2017-11-17 10:26:24 +00:00
|
|
|
// Get the books quantity. We can't join it because xorm ignores the Quantity option in struct
|
2017-11-29 11:25:52 +00:00
|
|
|
book.Quantity, err = book.getQuantity()
|
2017-11-16 18:07:32 +00:00
|
|
|
if err != nil {
|
2017-11-30 14:48:03 +00:00
|
|
|
return Book{}, false, err
|
2017-11-16 18:07:32 +00:00
|
|
|
}
|
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
|
2017-11-28 09:43:23 +00:00
|
|
|
book.Publisher, _, err = GetPublisherByID(book.PublisherID)
|
2017-11-16 18:07:32 +00:00
|
|
|
if err != nil {
|
2017-11-30 14:48:03 +00:00
|
|
|
return Book{}, false, err
|
2017-11-16 18:07:32 +00:00
|
|
|
}
|
2017-10-10 15:01:33 +00:00
|
|
|
|
2017-11-16 18:07:32 +00:00
|
|
|
// Get all authors
|
2017-11-24 13:36:40 +00:00
|
|
|
book.Authors, err = GetAuthorsByBook(book)
|
2017-11-16 18:07:32 +00:00
|
|
|
if err != nil {
|
2017-11-30 14:48:03 +00:00
|
|
|
return Book{}, false, err
|
2017-11-16 18:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|