Library/models/books_list.go

49 lines
1008 B
Go
Raw Normal View History

package models
2017-11-08 09:55:17 +00:00
// BookPublisher struct to join books with publishers
type BookPublisher struct {
2017-11-07 15:35:10 +00:00
Book `xorm:"extends"`
Publisher `xorm:"extends"`
}
2017-11-08 09:55:17 +00:00
// ListBooks returns a list with all books, filtered by an optional searchstring
func ListBooks(searchterm string) (books []*Book, err error) {
if searchterm == "" {
err = x.Table("books").
Find(&books)
if err != nil {
2017-11-30 14:48:03 +00:00
return []*Book{}, err
}
} else {
2017-11-07 15:35:10 +00:00
err = x.Where("title LIKE ?", "%"+searchterm+"%").Find(&books)
if err != nil {
2017-11-30 14:48:03 +00:00
return []*Book{}, err
}
}
// Get all authors, publishers and quantities
for i, book := range books {
// Get quantities
books[i].Quantity, err = book.getQuantity()
if err != nil {
2017-11-30 14:48:03 +00:00
return []*Book{}, err
}
// Get publisher
books[i].Publisher, _, err = GetPublisherByID(book.PublisherID)
if err != nil {
2017-11-30 14:48:03 +00:00
return []*Book{}, err
}
// Get all authors
2017-11-24 13:36:40 +00:00
books[i].Authors, err = GetAuthorsByBook(*book)
if err != nil {
2017-11-30 14:48:03 +00:00
return []*Book{}, err
}
}
return books, err
}