|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Controller\Attendee; |
| 6 | + |
| 7 | +use App\Domain\AttendeeCreator; |
| 8 | +use App\Domain\Model\CreateAttendeeModel; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | +use Symfony\Component\Routing\Annotation\Route; |
| 12 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 13 | +use Symfony\Component\Serializer\SerializerInterface; |
| 14 | + |
| 15 | +#[Route('/attendees', name: 'create_attendee', methods: ['POST'])] |
| 16 | +final class CreateController |
| 17 | +{ |
| 18 | + public function __construct( |
| 19 | + private AttendeeCreator $attendeeCreator, |
| 20 | + private SerializerInterface $serializer, |
| 21 | + private UrlGeneratorInterface $urlGenerator, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function __invoke(Request $request, CreateAttendeeModel $createAttendeeModel) |
| 26 | + { |
| 27 | + $createdAttendee = $this->attendeeCreator->create($createAttendeeModel); |
| 28 | + |
| 29 | + $serializedCreatedAttendee = $this->serializer->serialize($createdAttendee, $request->getRequestFormat()); |
| 30 | + |
| 31 | + return new Response($serializedCreatedAttendee, Response::HTTP_CREATED, [ |
| 32 | + 'Location' => $this->urlGenerator->generate('read_attendee', [ |
| 33 | + 'identifier' => $createdAttendee->getIdentifier(), |
| 34 | + ], UrlGeneratorInterface::ABSOLUTE_URL), |
| 35 | + ]); |
| 36 | + } |
| 37 | +} |
0 commit comments