| 1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\CommodityModel;
- use Psr\Http\Message\ServerRequestInterface;
- class CommodityUpdateController
- {
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- $id = isset($body['commodities_id']) ? (int)$body['commodities_id'] : 0;
- $name = array_key_exists('name', $body) ? trim((string)$body['name']) : null;
- $flag = array_key_exists('flag', $body) ? (string)$body['flag'] : null;
- if ($id <= 0) {
- return ResponseLib::sendFail('Validation failed: invalid commodities_id', [], 'E_VALIDATE')->withStatus(400);
- }
- if ($name === null && $flag === null) {
- return ResponseLib::sendFail('Validation failed: nothing to update', [], 'E_VALIDATE')->withStatus(400);
- }
- if ($name !== null && $name === '') {
- return ResponseLib::sendFail('Validation failed: name cannot be empty', [], 'E_VALIDATE')->withStatus(400);
- }
- $model = new CommodityModel();
- $updated = $model->update($id, $name, $flag);
- if (!$updated) {
- return ResponseLib::sendFail('Commodity Not Found or Not Updated', [], 'E_DATABASE')->withStatus(204);
- }
- return ResponseLib::sendOk($updated, 'S_UPDATED');
- }
- }
|