Library/models/books_list.go

49 lines
1008 B
Go

package models
// BookPublisher struct to join books with publishers
type BookPublisher struct {
Book `xorm:"extends"`
Publisher `xorm:"extends"`
}
// 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 {
return []*Book{}, err
}
} else {
err = x.Where("title LIKE ?", "%"+searchterm+"%").Find(&books)
if err != nil {
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 {
return []*Book{}, err
}
// Get publisher
books[i].Publisher, _, err = GetPublisherByID(book.PublisherID)
if err != nil {
return []*Book{}, err
}
// Get all authors
books[i].Authors, err = GetAuthorsByBook(*book)
if err != nil {
return []*Book{}, err
}
}
return books, err
}