Skip to content

Commit

Permalink
add doc to addProductStock and getProductStock, fix return value of a…
Browse files Browse the repository at this point in the history
…ddProductStock
  • Loading branch information
creme332 committed May 21, 2024
1 parent 94e6f40 commit bc11eea
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/models/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,30 @@ public function validate(): array
return $errors;
}

/**
* Increments stock of a product
* @param int $product_id ID of product whose stock will increase
* @param int $quantity Amount by which stock increases
* @return bool Success or not
*/
public function addProductStock(int $product_id, int $quantity): bool
{
$conn = self::connect();
$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);
$stm = $conn->prepare($query);
$stm->execute($params);

return $result;
$rows_affected = $stm->rowCount();
$conn = null;
return $rows_affected === 1;
}

/**
* @param int $product_id
* @return int Stock level of product. Defaults to 0.
*/
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 All @@ -169,6 +183,9 @@ public function getProductStock(int $product_id): int
}
}

/**
* @return Product[] Array of products which store sells.
*/
public function getProducts(): array
{
$query = "SELECT p.* FROM product p JOIN store_product sp ON p.product_id = sp.product_id WHERE sp.store_id = :store_id;";
Expand Down

0 comments on commit bc11eea

Please sign in to comment.