Browse Source

src: Add delete endpoint for purchaces

master
Elis Hirwing 3 years ago
parent
commit
408aa39252
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 4
      public/index.php
  2. 27
      src/Etu/Controller/Inventory.php

4
public/index.php

@ -16,6 +16,10 @@ $app->get('/inventory/{item}', function (Request $request, Response $response, a
return $this[InventoryController::class]->get($request, $response, $args);
});
$app->delete('/inventory/{item}/{amount}', function (Request $request, Response $response, array $args) {
return $this[InventoryController::class]->delete($request, $response, $args);
});
/**
* Set up dependencies
*/

27
src/Etu/Controller/Inventory.php

@ -42,4 +42,31 @@ class Inventory
return $response->withJson(['status' => 'in stock', 'stock' => (int) $item->item_stock]);
}
public function delete(Request $request, Response $response, array $args)
{
$item = $this->inventoryHelper->get($args['item']);
// No such item
if (!$item) {
return $response->withJson(['status' => 'item not found'], 404);
}
// Amount not avaiable
if ((int) $item->item_stock < (int) $args['amount']) {
return $response->withJson([
'status' => 'requested amount not available',
'available_amount' => $item->item_stock,
]);
}
if ((int) $args['amount'] < 1) {
return $response->withJson(['status' => 'invalid amount value'], 400);
}
// Set new amount in database
$this->inventoryHelper->setAmount($item->item_name, ($item->item_stock - $args['amount']));
return $response->withJson(['status' => 'success']);
}
}

Loading…
Cancel
Save