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 {
|
||||
|
||||
// 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
|
||||
|
|
Loading…
Reference in New Issue