Skip to content

Commit

Permalink
refactor: Update stock calculation in product_update_stock.py
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsonferovante committed Jul 12, 2024
1 parent 6ffd477 commit 0b5fc8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions catalog_microservice/data/use_cases/product_update_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def __validate_stock(self, stock):

def __update_stock(self, product_id, stock):
try:
product = self.product_repository.select_product(product_id)

if product.stock - stock < 0:
raise ValueError("Stock cannot be negative for product with id {}".format(product_id))

self.product_repository.update_stock(product_id, stock)
except ProductNotFoundError as e:
raise HttpNotFoundError(str(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def update_stock(cls, product_id: str, stock: int):
product = db_connection.session.query(ProductsEntity).filter(ProductsEntity.id == product_id).first()
if not product:
raise ProductNotFoundError(product_id=product_id)
product.stock = stock
product.stock = product.stock - stock
db_connection.session.commit()
except ProductNotFoundError as e:
db_connection.session.rollback()
Expand Down

0 comments on commit 0b5fc8d

Please sign in to comment.