mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
Merge branch 'bootstrap-serializer' into 'master'
Bootstrap serializer See merge request Chill-Projet/chill-bundles!17
This commit is contained in:
@@ -4,17 +4,21 @@ namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use Chill\PersonBundle\Privacy\AccompanyingPeriodPrivacyEvent;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Class AccompanyingCourseController
|
||||
@@ -23,6 +27,21 @@ use Symfony\Component\Serializer\SerializerInterface;
|
||||
*/
|
||||
class AccompanyingCourseController extends Controller
|
||||
{
|
||||
protected SerializerInterface $serializer;
|
||||
|
||||
protected EventDispatcherInterface $dispatcher;
|
||||
|
||||
protected ValidatorInterface $validator;
|
||||
|
||||
public function __construct(
|
||||
SerializerInterface $serializer,
|
||||
EventDispatcherInterface $dispatcher,
|
||||
ValidatorInterface $validator
|
||||
) {
|
||||
$this->serializer = $serializer;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
/**
|
||||
* Homepage of Accompanying Course section
|
||||
*
|
||||
@@ -68,45 +87,75 @@ class AccompanyingCourseController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Sérialise temporairement quelques données pour donner à manger au composant vuejs
|
||||
* Get API Data for showing endpoint
|
||||
*
|
||||
* @Route(
|
||||
* "/{_locale}/api/parcours/{accompanying_period_id}/show",
|
||||
* name="chill_person_accompanying_course_api_show")
|
||||
* "/{_locale}/person/api/1.0/accompanying-course/{accompanying_period_id}/show.{_format}",
|
||||
* name="chill_person_accompanying_course_api_show"
|
||||
* )
|
||||
* @ParamConverter("accompanyingCourse", options={"id": "accompanying_period_id"})
|
||||
* @param SerializerInterface $serializer
|
||||
*/
|
||||
public function showAPI(AccompanyingPeriod $accompanyingCourse): Response
|
||||
public function showAPI(AccompanyingPeriod $accompanyingCourse, $_format): Response
|
||||
{
|
||||
$persons = [];
|
||||
foreach ($accompanyingCourse->getParticipations() as $k => $participation ) {
|
||||
/**
|
||||
* @var AccompanyingPeriodParticipation $participation
|
||||
* @var Person $person
|
||||
*/
|
||||
$person = $participation->getPerson();
|
||||
$persons[$k] = [
|
||||
'id' => $person->getId(),
|
||||
'firstname' => $person->getFirstName(),
|
||||
'lastname' => $person->getLastName(),
|
||||
'email' => $person->getEmail(),
|
||||
'phone' => $person->getPhonenumber(),
|
||||
'startdate' => ($participation->getStartDate()) ? $participation->getStartDate()->format('Y-m-d') : null,
|
||||
'enddate' => ($participation->getEndDate()) ? $participation->getEndDate()->format('Y-m-d') : null
|
||||
];
|
||||
}
|
||||
$data = [
|
||||
'id' => $accompanyingCourse->getId(),
|
||||
'remark' => $accompanyingCourse->getRemark(),
|
||||
'closing_motive' => $accompanyingCourse->getClosingMotive() ? $accompanyingCourse->getClosingMotive()->getName()['fr'] : null,
|
||||
'opening_date' => ($accompanyingCourse->getOpeningDate()) ? $accompanyingCourse->getOpeningDate()->format('Y-m-d') : null,
|
||||
'closing_date' => ($accompanyingCourse->getClosingDate()) ? $accompanyingCourse->getClosingDate()->format('Y-m-d') : null,
|
||||
'persons' => $persons
|
||||
];
|
||||
// TODO check ACL on AccompanyingPeriod
|
||||
|
||||
$this->dispatcher->dispatch(
|
||||
AccompanyingPeriodPrivacyEvent::ACCOMPANYING_PERIOD_PRIVACY_EVENT,
|
||||
new AccompanyingPeriodPrivacyEvent($accompanyingCourse, [
|
||||
'action' => 'showApi'
|
||||
])
|
||||
);
|
||||
|
||||
switch ($_format) {
|
||||
case 'json':
|
||||
return $this->json($accompanyingCourse);
|
||||
default:
|
||||
throw new BadRequestException('Unsupported format');
|
||||
}
|
||||
|
||||
$serialized = \json_encode($data);
|
||||
$response = new Response($serialized);
|
||||
$response->headers->set('Content-Type', 'application/json');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get API Data for showing endpoint
|
||||
*
|
||||
* @Route(
|
||||
* "/{_locale}/person/api/1.0/accompanying-course/{accompanying_period_id}/participation.{_format}",
|
||||
* name="chill_person_accompanying_course_api_add_participation",
|
||||
* methods={"POST"},
|
||||
* format="json",
|
||||
* requirements={
|
||||
* "_format": "json",
|
||||
* }
|
||||
* )
|
||||
* @ParamConverter("accompanyingCourse", options={"id": "accompanying_period_id"})
|
||||
*/
|
||||
public function addParticipationAPI(Request $request, AccompanyingPeriod $accompanyingCourse, $_format): Response
|
||||
{
|
||||
switch ($_format) {
|
||||
case 'json':
|
||||
$person = $this->serializer->deserialize($request->getContent(), Person::class, $_format, [
|
||||
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
throw new BadRequestException('Unsupported format');
|
||||
}
|
||||
|
||||
if (NULL === $person) {
|
||||
throw new BadRequestException('person id not found');
|
||||
}
|
||||
|
||||
// TODO add acl
|
||||
$accompanyingCourse->addPerson($person);
|
||||
$errors = $this->validator->validate($accompanyingCourse);
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
// only format accepted
|
||||
return $this->json($errors);
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return new JsonResponse();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2015-2021 Champs-Libres Coopérative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
|
||||
class ApiPersonController extends Controller
|
||||
{
|
||||
public function viewAction($id, $_format)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user