Library/models/books_list.go

52 lines
1.2 KiB
Go
Raw Normal View History

package models
import "fmt"
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").
2017-11-07 15:35:10 +00:00
//Join("INNER", "publishers", "books.publisher = publishers.id").
Find(&books)
if err != nil {
fmt.Println("Error getting Books", err)
}
} else {
2017-11-07 15:35:10 +00:00
err = x.Where("title LIKE ?", "%"+searchterm+"%").Find(&books)
if err != nil {
fmt.Println("Error getting Books", err)
}
}
// Get all authors, publishers and quantities
for i, book := range books {
// Get quantities
books[i].Quantity, err = GetQuantityByBook(book.ID)
if err != nil {
fmt.Println("Error getting quantity:", err)
}
// Get publisher
books[i].PublisherFull, _, err = GetPublisherByID(book.Publisher)
if err != nil {
fmt.Println("Error getting publisher:", err)
}
// Get all authors
books[i].Authors, err = GetAuthorsByBook(book.ID)
if err != nil {
fmt.Println("Error getting authors:", err)
}
}
return books, err
}