Last active
January 5, 2018 22:01
-
-
Save daniel-bytes/495f1da18d90d23fce1b379810ea35b8 to your computer and use it in GitHub Desktop.
Mutation to update an existing product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Products | |
class Update < Mutations::Command | |
required do | |
model :product | |
integer :adjust_inventory | |
end | |
def validate | |
if adjust_inventory == 0 | |
return add_error(:adjust_inventory, :invalid, 'Inventory adjustment factory cannot be zero') | |
end | |
if adjust_inventory < 0 && ( (product.inventory + adjust_inventory) < 0 ) | |
return add_error(:adjust_inventory, :invalid, 'Cannot set inventory below zero') | |
end | |
if product.discountinued? | |
return add_error(:product, :discountinued, 'Product is discountinued') | |
end | |
end | |
def execute | |
product.inventory += adjust_inventory | |
product.save | |
if adjust_inventory > 0 | |
product.add_inventory! | |
else | |
product.reduce_inventory! | |
end | |
product | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment