CategoryUpdateFlagController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CategoryModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as v;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class CategoryUpdateFlagController
  9. {
  10. private CategoryModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new CategoryModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string)$request->getBody(), true) ?? [];
  18. try {
  19. v::key('company_id', v::intType()->positive())
  20. ->key('category_name', v::stringType()->notEmpty()->alnum(' '))
  21. ->key('category_flag', v::stringType()->notEmpty())
  22. ->assert($body);
  23. } catch (ValidationException $e) {
  24. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
  25. }
  26. $companyId = $body['company_id'];
  27. $categoryName = $body['category_name'];
  28. $newFlag = $body['category_flag'];
  29. $deleted = $this->model->updateFlag($categoryName, $companyId, $newFlag);
  30. return $deleted
  31. ? ResponseLib::sendOk(['updated flag' => true])
  32. : ResponseLib::sendFail("Failed to Update Category Flag", [], "E_VALIDATE")->withStatus(500);
  33. }
  34. }