Improved performance when getting a list of all books
the build failed Details

This commit is contained in:
konrad 2017-12-01 10:18:26 +01:00 committed by kolaente
parent e485218688
commit 6496efd411
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 27 additions and 8 deletions

View File

@ -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