src/UI/WebBundle/Controller/Whater/Api/AppWhaterPointApiController.php line 52

Open in your IDE?
  1. <?php
  2. namespace Whater\UI\WebBundle\Controller\Whater\Api;
  3. use FOS\RestBundle\Controller\Annotations\View;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Whater\UI\WebBundle\Controller\AbstractBusController;
  7. use Hateoas\Representation\Factory\PagerfantaFactory;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. use Whater\Application\UseCase\Whater\CommandRequest\AppListMyOrganizationWhaterPointsCommand;
  10. use Whater\Application\UseCase\Whater\CommandRequest\AppListMyWhaterPointsCommand;
  11. use Whater\Domain\User\Model\Role;
  12. use Whater\Infrastructure\CommonBundle\Pagination\PagerTrait;
  13. use Whater\Infrastructure\WhaterBundle\Form\Type\AppListMyOrganizationWhaterPointsType;
  14. use Whater\Infrastructure\WhaterBundle\Form\Type\AppListMyWhaterPointsType;
  15. /**
  16. * @Route("/web-api/app/whater-point")
  17. */
  18. class AppWhaterPointApiController extends AbstractBusController
  19. {
  20. use PagerTrait;
  21. /**
  22. * @Route("/my-whaterpoints/list", name="web_api_app_list_my_whater_points", defaults={"_format" = "json"})
  23. * @View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default", "AppApiWhaterPointList"})
  24. */
  25. public function listMyWhaterPointsAction(Request $request)
  26. {
  27. try {
  28. // User
  29. $grantUser = $this->getGrantedUser();
  30. if ($grantUser == null) {
  31. return $this->sendBadRequestResponse('access_not_allowed!');
  32. }
  33. if (empty($grantUser)) {
  34. throw new AccessDeniedHttpException();
  35. }
  36. $form = $this->getFormFactory()->create(
  37. AppListMyWhaterPointsType::class,
  38. AppListMyWhaterPointsCommand::convertToDTO(),
  39. array(
  40. 'csrf_protection' => false,
  41. )
  42. );
  43. $form->handleRequest($request);
  44. if ($form->isSubmitted() && $form->isValid()) {
  45. $command = AppListMyWhaterPointsCommand::fromDTO($form->getData());
  46. $pager = $this->handle($command);
  47. return (new PagerfantaFactory())->createRepresentation($pager, new \Hateoas\Configuration\Route('web_api_app_list_my_whater_points'));
  48. } else {
  49. return $this->sendBadRequestResponse('whater_point.exception.invalid_query');
  50. }
  51. } catch (\Exception $e) {
  52. return $this->sendBadRequestResponse($e->getMessage());
  53. }
  54. }
  55. /**
  56. * @Route("/my-organization-whaterpoints/list", name="web_api_app_list_my_organization_whater_points", defaults={"_format" = "json"})
  57. * @View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default", "AppApiWhaterPointList"})
  58. */
  59. public function listMyOrganizationWhaterPointsAction(Request $request)
  60. {
  61. try {
  62. // User
  63. $grantUser = $this->getGrantedUser();
  64. if (!($grantUser->hasRole(Role::ROLE_ADMIN)) && !($grantUser->whaterOrganization())) {
  65. return $this->sendBadRequestResponse('access_not_allowed!');
  66. }
  67. if (empty($grantUser)) {
  68. throw new AccessDeniedHttpException();
  69. }
  70. $form = $this->getFormFactory()->create(
  71. AppListMyOrganizationWhaterPointsType::class,
  72. AppListMyOrganizationWhaterPointsCommand::convertToDTO(),
  73. array(
  74. 'csrf_protection' => false,
  75. )
  76. );
  77. $form->handleRequest($request);
  78. if ($form->isSubmitted() && $form->isValid()) {
  79. $command = AppListMyOrganizationWhaterPointsCommand::fromDTO($form->getData());
  80. $pager = $this->handle($command);
  81. return (new PagerfantaFactory())->createRepresentation($pager, new \Hateoas\Configuration\Route('web_api_app_list_my_organization_whater_points'));
  82. } else {
  83. return $this->sendBadRequestResponse('whater_point.exception.invalid_query');
  84. }
  85. } catch (\Exception $e) {
  86. return $this->sendBadRequestResponse($e->getMessage());
  87. }
  88. }
  89. }