Reimplement checkout item removal. All existing tests now pass
This commit is contained in:
+29
-4
@@ -13,10 +13,21 @@ class Checkout
|
||||
|
||||
raise InvalidItem unless product
|
||||
|
||||
product_in_checkout = new_or_existing_product_in_checkout(product)
|
||||
product_in_checkout.add_quantity(quantity)
|
||||
new_or_existing_product_in_checkout(product).add_quantity(quantity)
|
||||
end
|
||||
|
||||
@items.push product_in_checkout
|
||||
def remove(product_id:, quantity: 1)
|
||||
product = @shop.product_by_id(product_id)
|
||||
|
||||
raise InvalidItem unless product
|
||||
|
||||
product_in_checkout = existing_product_in_checkout(product)
|
||||
|
||||
return false unless product_in_checkout
|
||||
|
||||
product_in_checkout.remove_quantity(quantity)
|
||||
|
||||
trim_empty_items
|
||||
end
|
||||
|
||||
def items
|
||||
@@ -37,7 +48,21 @@ class Checkout
|
||||
|
||||
private
|
||||
|
||||
def existing_product_in_checkout(product)
|
||||
@items.find { |item| item.product_id == product.id }
|
||||
end
|
||||
|
||||
def new_or_existing_product_in_checkout(product)
|
||||
@items.find { |item| item.product_id == product.id } || ProductInCheckout.new(product: product)
|
||||
product_in_checkout = existing_product_in_checkout(product)
|
||||
unless product_in_checkout
|
||||
product_in_checkout = ProductInCheckout.new(product: product)
|
||||
@items.push(product_in_checkout)
|
||||
end
|
||||
|
||||
product_in_checkout
|
||||
end
|
||||
|
||||
def trim_empty_items
|
||||
@items = @items.filter { |item| item.quantity > 0 }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user