1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Simplex\Quickstart\Module\Demo\Controller; |
4
|
|
|
|
5
|
|
|
use Lukasoppermann\Httpstatus\Httpstatuscodes; |
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
use Simplex\Quickstart\Module\Demo\Command\Register; |
9
|
|
|
use Simplex\Quickstart\Module\Demo\Repository\PersonRepository; |
10
|
|
|
use Simplex\Quickstart\Shared\Controller\AppController; |
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
12
|
|
|
|
13
|
|
|
class DemoController extends AppController |
14
|
|
|
{ |
15
|
|
|
const LOCATION_HEADER_NAME = 'Location'; |
16
|
|
|
|
17
|
|
|
/** @var PersonRepository */ |
18
|
|
|
private $personRepository; |
19
|
|
|
|
20
|
1 |
|
public function __construct(PersonRepository $personRepository) |
21
|
|
|
{ |
22
|
1 |
|
$this->personRepository = $personRepository; |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @Route("/", methods={"GET"}, name="list_people") |
27
|
|
|
*/ |
28
|
1 |
|
public function index(): Response |
29
|
|
|
{ |
30
|
1 |
|
$people = $this->personRepository->findAll(); |
31
|
|
|
|
32
|
1 |
|
return $this->jsonResponse($people); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Route("/person/{id}", methods={"GET"}, name="get_person") |
37
|
|
|
*/ |
38
|
1 |
|
public function get(string $id): Response |
39
|
|
|
{ |
40
|
1 |
|
$person = $this->personRepository->ofId($id); |
41
|
|
|
|
42
|
1 |
|
return $this->jsonResponse($person); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @Route("/person", methods={"POST"}, name="register") |
47
|
|
|
*/ |
48
|
4 |
|
public function post(Request $request): Response |
49
|
|
|
{ |
50
|
4 |
|
$command = $this->map($request, Register::class); |
51
|
|
|
|
52
|
1 |
|
$this->handleCommand($command); |
53
|
|
|
|
54
|
1 |
|
$person = $this->personRepository->ofUsername($command->email); |
55
|
|
|
|
56
|
1 |
|
$this->setHeader(self::LOCATION_HEADER_NAME, $person->getId()->toString()); |
57
|
|
|
|
58
|
1 |
|
return $this->jsonResponse(null, Httpstatuscodes::HTTP_CREATED); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|