Browse Source

src: Make inventory only fetch one item

master
Elis Hirwing 3 years ago
parent
commit
175f31bf70
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 6
      src/Etu/Controller/Inventory.php
  2. 4
      src/Etu/Helpers/Inventory.php

6
src/Etu/Controller/Inventory.php

@ -22,15 +22,13 @@ class Inventory
public function get(Request $request, Response $response, array $args)
{
$items = $this->inventoryHelper->get($args['item']);
$item = $this->inventoryHelper->get($args['item']);
// No such item
if (count($items) === 0) {
if (empty($item)) {
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,

4
src/Etu/Helpers/Inventory.php

@ -13,7 +13,7 @@ class Inventory
$this->db = $db;
}
public function get(string $name) : array
public function get(string $name) : \stdClass
{
// Prepare query
$stmt = $this->db->prepare('SELECT * FROM inventory WHERE item_name = :item_name');
@ -24,6 +24,6 @@ class Inventory
// Execute and fetch result
$stmt->execute();
return $stmt->fetchAll();
return $stmt->fetch() ?? ((object) []);
}
}

Loading…
Cancel
Save