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;";