diff --git a/models/books_list.go b/models/books_list.go index 1b17e50..e4cf551 100644 --- a/models/books_list.go +++ b/models/books_list.go @@ -22,19 +22,38 @@ func ListBooks(searchterm string) (books []*Book, err error) { } } - // Get all authors, publishers and quantities + // Get all publishers and quantities to afterwards loop though them (less sql queries -> more performance) + // Publishers + pubs, err := ListPublishers("") + if err != nil { + return []*Book{}, err + } + + // Quantites + allq := []Quantity{} + err = x.Table("quantities"). + Select("quantities.id, quantity_relations.book_id AS item_id, quantities.quantity, quantities.created"). + Join("INNER", "quantity_relations", "quantities.item_id = quantity_relations.id"). + Where("quantity_relations.book_id != 0"). + Desc("quantities.created"). + Find(&allq) + + // Link them for i, book := range books { - // Get quantities - books[i].Quantity, err = book.getQuantity() - if err != nil { - return []*Book{}, err + // Set quantities + for _, qy := range allq { + if qy.ItemID == book.ID { + books[i].Quantity = qy.Quantity + break // Take the first quantity you find and exit + } } // Get publisher - books[i].Publisher, _, err = GetPublisherByID(book.PublisherID) - if err != nil { - return []*Book{}, err + for _, pub := range pubs { + if pub.ID == book.PublisherID { + books[i].Publisher = pub + } } // Get all authors