diff --git a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php index 490d50da1..fae54b6ae 100644 --- a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php +++ b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php @@ -13,6 +13,7 @@ namespace Chill\TaskBundle\Controller; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface; +use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface; use Chill\MainBundle\Timeline\TimelineBuilder; @@ -31,6 +32,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormFactoryInterface; +use Symfony\Component\HttpFoundation\Exception\BadRequestException; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -431,10 +434,15 @@ final class SingleTaskController extends AbstractController * @return Response * @Route( * "/{_locale}/task/single-task/list/my", - * name="chill_task_singletask_my_tasks" + * name="chill_task_singletask_my_tasks", + * defaults={"_format": "html"} + * ) + * @Route( + * "/api/1.0/task/single-task/list/my", + * defaults={"_format": "json"} * ) */ - public function myTasksAction() + public function myTasksAction(string $_format) { $this->denyAccessUnlessGranted('ROLE_USER'); @@ -459,11 +467,23 @@ final class SingleTaskController extends AbstractController ] ); - return $this->render('@ChillTask/SingleTask/List/index_my_tasks.html.twig', [ - 'tasks' => $tasks, - 'paginator' => $paginator, - 'filter_order' => $filterOrder, - ]); + switch ($_format) { + case 'html': + return $this->render('@ChillTask/SingleTask/List/index_my_tasks.html.twig', [ + 'tasks' => $tasks, + 'paginator' => $paginator, + 'filter_order' => $filterOrder, + ]); + + case 'json': + $collection = new Collection($tasks, $paginator); + + return $this->json($collection, JsonResponse::HTTP_OK, [], + ['groups' => ['read']]); + + default: + throw new BadRequestException("format not supported: $format"); + } } /** diff --git a/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php b/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php index 8b44ebf8f..a4b154d2b 100644 --- a/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php +++ b/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php @@ -18,6 +18,7 @@ use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Validator\Constraints as Assert; use function array_fill_keys; @@ -27,6 +28,9 @@ use function array_keys; * AbstractTask. * * @ORM\MappedSuperclass + * @Serializer\DiscriminatorMap(typeProperty="type", mapping={ + * "single_task": SingleTask::class + * }) */ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface { @@ -35,6 +39,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * @ORM\ManyToOne( * targetEntity="\Chill\MainBundle\Entity\User" * ) + * @Serializer\Groups({"read"}) */ private $assignee; @@ -49,12 +54,14 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface /** * @var bool * @ORM\Column(name="closed", type="boolean", options={ "default": false }) + * @Serializer\Groups({"read"}) */ private $closed = false; /** * @var AccompanyingPeriod * @ORM\ManyToOne(targetEntity="\Chill\PersonBundle\Entity\AccompanyingPeriod") + * @Serializer\Groups({"read"}) */ private $course; @@ -62,6 +69,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * @var json * * @ORM\Column(name="current_states", type="json") + * @Serializer\Groups({"read"}) */ private $currentStates = []; @@ -69,6 +77,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * @var string * * @ORM\Column(name="description", type="text") + * @Serializer\Groups({"read"}) */ private $description = ''; @@ -77,6 +86,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * @ORM\ManyToOne( * targetEntity="\Chill\PersonBundle\Entity\Person" * ) + * @Serializer\Groups({"read"}) */ private $person; @@ -85,6 +95,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * * @ORM\Column(name="title", type="text") * @Assert\NotBlank + * @Serializer\Groups({"read"}) */ private $title = ''; @@ -92,6 +103,7 @@ abstract class AbstractTask implements HasCenterInterface, HasScopeInterface * @var string * * @ORM\Column(name="type", type="string", length=255) + * @Serializer\Groups({"read"}) */ private $type;