Improved performance when getting a list of all books
the build failed
Details
the build failed
Details
This commit is contained in:
parent
e485218688
commit
6496efd411
|
@ -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 {
|
for i, book := range books {
|
||||||
|
|
||||||
// Get quantities
|
// Set quantities
|
||||||
books[i].Quantity, err = book.getQuantity()
|
for _, qy := range allq {
|
||||||
if err != nil {
|
if qy.ItemID == book.ID {
|
||||||
return []*Book{}, err
|
books[i].Quantity = qy.Quantity
|
||||||
|
break // Take the first quantity you find and exit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get publisher
|
// Get publisher
|
||||||
books[i].Publisher, _, err = GetPublisherByID(book.PublisherID)
|
for _, pub := range pubs {
|
||||||
if err != nil {
|
if pub.ID == book.PublisherID {
|
||||||
return []*Book{}, err
|
books[i].Publisher = pub
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all authors
|
// Get all authors
|
||||||
|
|
Loading…
Reference in New Issue