<?php
namespace App\Controller;
use App\Entity\Action;
use App\Entity\ActionComment;
use App\Entity\OrganizationUnit;
use App\Repository\EventRepository;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
/**
* @Route("/aapi")
* @Security("is_granted('ROLE_USER')")
*/
class AdminApiController extends AbstractFOSRestController
{
/**
* @Route("/action/{id}/comment/list", name="api_admin_action_comment_new_n_list", methods={"POST"})
* @Security("is_granted('ROLE_USER')")
*/
public function newNList(Request $request, Action $action, SerializerInterface $serializer): Response
{
if ($request->request->get('content')) {
$comment = new ActionComment();
$comment->setContent($request->request->get('content'));
$action->addComment($comment);
$this->getDoctrine()->getManager()->persist($action);
$this->getDoctrine()->getManager()->flush();
}
return new Response(
$serializer->serialize(
$action->getComments(),
'json',
['groups'=>['aapi','crLog']]
),
RESPONSE::HTTP_OK
);
}
/**
* @Route("/action/{id}/complete", name="api_admin_action_complete", methods={"POST"})
* @Security("is_granted('ROLE_USER')")
*/
public function completeTask(Request $request, Action $action): Response
{
if ( $action->canClose($this->getUser()) || $this->getUser()->hasRole('ROLE_ACTION_ADMIN')) {
$action->setCompleted(true);
$this->getDoctrine()->getManager()->persist($action);
$this->getDoctrine()->getManager()->flush();
} else {
return new Response(
json_encode([]),
RESPONSE::HTTP_BAD_REQUEST
);
}
return new Response(
json_encode([
'newState' => $action->getTaskState(),
'aid' => $action->getId(),
]),
RESPONSE::HTTP_OK
);
}
/**
* @Route("/tasks", name="api_admin_tasks_list", methods={"GET"})
* @Security("is_granted('ROLE_USER')")
*/
public function taskList(Request $request, SerializerInterface $serializer): Response
{
$tasks = $this->getDoctrine()->getRepository(Action::class)->userTasks($this->getUser());
return new Response(
$serializer->serialize(
$tasks,
'json',
['groups'=>['aapi','crLog']]
),
RESPONSE::HTTP_OK
);
}
/**
* @Route("/ou/{id}/events", name="api_admin_ou_events_list", methods={"GET"})
* @Security("is_granted('ROLE_EVENT_VENUE')")
*/
public function eventsList(Request $request, SerializerInterface $serializer, OrganizationUnit $ou, EventRepository $er): Response
{
$qb=$er->createQueryBuilder('e')
->where('e.organizationUnit = :ou' )
->andWhere('e.date>=:start')
->setParameter( 'ou', $ou )
->setParameter( 'start', new \DateTime() )
->orderBy('e.date', 'ASC');
return new Response(
$serializer->serialize(
$qb->getQuery()->getResult(),
'json',
['groups'=>['aapi']]
),
RESPONSE::HTTP_OK
);
}
}