From bc11eeaa2ccd512fcd5f48ae6b97807a7d0bd3e7 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 21 May 2024 11:15:32 +0400 Subject: [PATCH] add doc to addProductStock and getProductStock, fix return value of addProductStock --- src/models/Store.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/models/Store.php b/src/models/Store.php index 18a96b9..7bb82be 100644 --- a/src/models/Store.php +++ b/src/models/Store.php @@ -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;"; @@ -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;";