Browse Source

src: Add the code to handle the current stock

master
Elis Hirwing 3 years ago
parent
commit
7d640a72c3
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 29
      src/Etu/Controller/Inventory.php

29
src/Etu/Controller/Inventory.php

@ -22,5 +22,34 @@ class Inventory
public function get(Request $request, Response $response, array $args)
{
// Prepare query
$stmt = $this->db->prepare('SELECT * FROM inventory WHERE item_name = :item_name');
// Bind value
$stmt->bindValue('item_name', $args['item'], PDO::PARAM_STR);
// Execute and fetch result
$stmt->execute();
$items = $stmt->fetchAll();
// No such item
if (count($items) === 0) {
return $response->withJson(['status' => 'item not found'], 404);
}
$item = array_shift($items);
$this->logger->info('Selected {item} from database and current stock is {stock}', [
'id' => $item->item_id,
'item' => $item->item_name,
'stock' => $item->item_stock,
]);
// If we're out of items
if ((int) $item->item_stock <= 0) {
return $response->withJson(['status' => 'out of stock', 'stock' => 0]);
}
return $response->withJson(['status' => 'in stock', 'stock' => (int) $item->item_stock]);
}
}

Loading…
Cancel
Save