Optimized quantity handling
This commit is contained in:
parent
c93b2f433c
commit
7fba68f812
|
@ -70,30 +70,21 @@ func SetQuantity(itemID, quantity int64) (err error) {
|
|||
|
||||
// Get item quantity with relation
|
||||
func (item Item) getQuantity() (quantity int64, err error) {
|
||||
// get the quantity relation for the item
|
||||
qtyID, _, err := item.getQuantityRelation()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
qty := Quantity{}
|
||||
_, err = x.Table("quantities").
|
||||
Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created").
|
||||
Join("INNER", "quantity_relations", "quantities.item_id = quantity_relations.id").
|
||||
Where("quantity_relations.item_id = ?", item.ID).
|
||||
Desc("quantities.created").Get(&qty)
|
||||
|
||||
return GetQuantity(qtyID)
|
||||
}
|
||||
|
||||
func (item Item) getQuantityRelation() (qtyID int64, exists bool, err error) {
|
||||
// get the quantity relation for the item
|
||||
qty := quantityRelation{ItemID: item.ID}
|
||||
has, err := x.Get(&qty)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
return qty.ID, has, nil
|
||||
return qty.Quantity, err
|
||||
}
|
||||
|
||||
// Set item quantity with relation
|
||||
func (item Item) setQuantity(quantity int64) (err error) {
|
||||
// Check if the relation already exists, if not, create a new one
|
||||
qtyItemID, exists, err := item.getQuantityRelation()
|
||||
qty := quantityRelation{ItemID: item.ID}
|
||||
exists, err := x.Get(&qty)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -105,42 +96,32 @@ func (item Item) setQuantity(quantity int64) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
qtyItemID = rel.ID
|
||||
qty.ID = rel.ID
|
||||
}
|
||||
|
||||
// Insert the new quantity
|
||||
return SetQuantity(qtyItemID, quantity)
|
||||
return SetQuantity(qty.ID, quantity)
|
||||
}
|
||||
|
||||
// ===== BOOKS =====
|
||||
|
||||
// Get book quantity with relation
|
||||
func (book Book) getQuantity() (quantity int64, err error) {
|
||||
// get the quantity relation for the item
|
||||
qtyID, _, err := book.getQuantityRelation()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
qty := Quantity{}
|
||||
_, err = x.Table("quantities").
|
||||
Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created").
|
||||
Join("INNER", "quantity_relations", "quantities.item_id = quantity_relations.id").
|
||||
Where("quantity_relations.book_id = ?", book.ID).
|
||||
Desc("quantities.created").Get(&qty)
|
||||
|
||||
return GetQuantity(qtyID)
|
||||
}
|
||||
|
||||
// Get the quantity relation
|
||||
func (book Book) getQuantityRelation() (qtyID int64, exists bool, err error) {
|
||||
// get the quantity relation for the item
|
||||
qty := quantityRelation{BookID: book.ID}
|
||||
has, err := x.Get(&qty)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
return qty.ID, has, nil
|
||||
return qty.Quantity, err
|
||||
}
|
||||
|
||||
// Set book quantity with relation
|
||||
func (book Book) setQuantity(quantity int64) (err error) {
|
||||
// Check if the relation already exists, if not, create a new one
|
||||
qtyItemID, exists, err := book.getQuantityRelation()
|
||||
qty := quantityRelation{BookID: book.ID}
|
||||
exists, err := x.Get(&qty)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -152,9 +133,9 @@ func (book Book) setQuantity(quantity int64) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
qtyItemID = rel.ID
|
||||
qty.ID = rel.ID
|
||||
}
|
||||
|
||||
// Insert the new quantity
|
||||
return SetQuantity(qtyItemID, quantity)
|
||||
return SetQuantity(qty.ID, quantity)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue