Skip to content

Commit

Permalink
add addProductStock method to Store model and update `OrderProduc…
Browse files Browse the repository at this point in the history
…tTest` and `OrderTest` to use it
  • Loading branch information
Divyeshhhh committed May 20, 2024
1 parent 9327d5f commit ca690c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/models/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public function validate(): array
return $errors;
}

public function addProductStock(int $product_id, int $quantity): bool
{
$query = "INSERT INTO store_product (store_id, product_id, stock_level) VALUES (:store_id, :product_id, :quantity)
ON DUPLICATE KEY UPDATE stock_level = stock_level + :quantity";
$params = ['store_id' => $this->store_id, 'product_id' => $product_id, 'quantity' => $quantity];
$result = self::query($query, $params);

return $result;
}

public function getProductStock(int $product_id): int
{
$query = "SELECT stock_level FROM store_product WHERE store_id = :store_id AND product_id = :product_id;";
Expand Down
3 changes: 3 additions & 0 deletions tests/OrderProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function setUp(): void
throw new Exception('Unable to save product');
}

// Update stock level for the product
$this->dummy_store->addProductStock($this->dummy_product->getProductID(), 10);

// Create dummy order line items
$this->line_items = [
new OrderProduct($this->dummy_product->getProductID(), "medium", "oat", 2, 5.0)
Expand Down
4 changes: 4 additions & 0 deletions tests/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function setUp(): void
throw new Exception('Unable to save product 2');
}

// Add stock to the store for the products
$this->dummy_store->addProductStock($product1->getProductID(), 10);
$this->dummy_store->addProductStock($product2->getProductID(), 10);

// Create dummy order line items
$this->line_items = [
new OrderProduct($product1->getProductID(), "medium", "oat", 2, 5.0),
Expand Down

0 comments on commit ca690c3

Please sign in to comment.