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
|
// Get item quantity with relation
|
||||||
func (item Item) getQuantity() (quantity int64, err error) {
|
func (item Item) getQuantity() (quantity int64, err error) {
|
||||||
// get the quantity relation for the item
|
qty := Quantity{}
|
||||||
qtyID, _, err := item.getQuantityRelation()
|
_, err = x.Table("quantities").
|
||||||
if err != nil {
|
Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created").
|
||||||
return 0, err
|
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)
|
return qty.Quantity, err
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set item quantity with relation
|
// Set item quantity with relation
|
||||||
func (item Item) setQuantity(quantity int64) (err error) {
|
func (item Item) setQuantity(quantity int64) (err error) {
|
||||||
// Check if the relation already exists, if not, create a new one
|
// 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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -105,42 +96,32 @@ func (item Item) setQuantity(quantity int64) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
qtyItemID = rel.ID
|
qty.ID = rel.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the new quantity
|
// Insert the new quantity
|
||||||
return SetQuantity(qtyItemID, quantity)
|
return SetQuantity(qty.ID, quantity)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== BOOKS =====
|
// ===== BOOKS =====
|
||||||
|
|
||||||
// Get book quantity with relation
|
// Get book quantity with relation
|
||||||
func (book Book) getQuantity() (quantity int64, err error) {
|
func (book Book) getQuantity() (quantity int64, err error) {
|
||||||
// get the quantity relation for the item
|
qty := Quantity{}
|
||||||
qtyID, _, err := book.getQuantityRelation()
|
_, err = x.Table("quantities").
|
||||||
if err != nil {
|
Select("quantities.id, quantity_relations.item_id, quantities.quantity, quantities.created").
|
||||||
return 0, err
|
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)
|
return qty.Quantity, err
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set book quantity with relation
|
// Set book quantity with relation
|
||||||
func (book Book) setQuantity(quantity int64) (err error) {
|
func (book Book) setQuantity(quantity int64) (err error) {
|
||||||
// Check if the relation already exists, if not, create a new one
|
// 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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -152,9 +133,9 @@ func (book Book) setQuantity(quantity int64) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
qtyItemID = rel.ID
|
qty.ID = rel.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the new quantity
|
// Insert the new quantity
|
||||||
return SetQuantity(qtyItemID, quantity)
|
return SetQuantity(qty.ID, quantity)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue