<?php
namespace Whater\UI\WebBundle\Controller\Whater\Api;
use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Whater\UI\WebBundle\Controller\AbstractBusController;
use Hateoas\Representation\Factory\PagerfantaFactory;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Whater\Application\UseCase\Whater\CommandRequest\AppListMyOrganizationWhaterPointsCommand;
use Whater\Application\UseCase\Whater\CommandRequest\AppListMyWhaterPointsCommand;
use Whater\Domain\User\Model\Role;
use Whater\Infrastructure\CommonBundle\Pagination\PagerTrait;
use Whater\Infrastructure\WhaterBundle\Form\Type\AppListMyOrganizationWhaterPointsType;
use Whater\Infrastructure\WhaterBundle\Form\Type\AppListMyWhaterPointsType;
/**
* @Route("/web-api/app/whater-point")
*/
class AppWhaterPointApiController extends AbstractBusController
{
use PagerTrait;
/**
* @Route("/my-whaterpoints/list", name="web_api_app_list_my_whater_points", defaults={"_format" = "json"})
* @View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default", "AppApiWhaterPointList"})
*/
public function listMyWhaterPointsAction(Request $request)
{
try {
// User
$grantUser = $this->getGrantedUser();
if ($grantUser == null) {
return $this->sendBadRequestResponse('access_not_allowed!');
}
if (empty($grantUser)) {
throw new AccessDeniedHttpException();
}
$form = $this->getFormFactory()->create(
AppListMyWhaterPointsType::class,
AppListMyWhaterPointsCommand::convertToDTO(),
array(
'csrf_protection' => false,
)
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$command = AppListMyWhaterPointsCommand::fromDTO($form->getData());
$pager = $this->handle($command);
return (new PagerfantaFactory())->createRepresentation($pager, new \Hateoas\Configuration\Route('web_api_app_list_my_whater_points'));
} else {
return $this->sendBadRequestResponse('whater_point.exception.invalid_query');
}
} catch (\Exception $e) {
return $this->sendBadRequestResponse($e->getMessage());
}
}
/**
* @Route("/my-organization-whaterpoints/list", name="web_api_app_list_my_organization_whater_points", defaults={"_format" = "json"})
* @View(serializerEnableMaxDepthChecks=true, serializerGroups={"Default", "AppApiWhaterPointList"})
*/
public function listMyOrganizationWhaterPointsAction(Request $request)
{
try {
// User
$grantUser = $this->getGrantedUser();
if (!($grantUser->hasRole(Role::ROLE_ADMIN)) && !($grantUser->whaterOrganization())) {
return $this->sendBadRequestResponse('access_not_allowed!');
}
if (empty($grantUser)) {
throw new AccessDeniedHttpException();
}
$form = $this->getFormFactory()->create(
AppListMyOrganizationWhaterPointsType::class,
AppListMyOrganizationWhaterPointsCommand::convertToDTO(),
array(
'csrf_protection' => false,
)
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$command = AppListMyOrganizationWhaterPointsCommand::fromDTO($form->getData());
$pager = $this->handle($command);
return (new PagerfantaFactory())->createRepresentation($pager, new \Hateoas\Configuration\Route('web_api_app_list_my_organization_whater_points'));
} else {
return $this->sendBadRequestResponse('whater_point.exception.invalid_query');
}
} catch (\Exception $e) {
return $this->sendBadRequestResponse($e->getMessage());
}
}
}