@@ -194,16 +194,24 @@ use Patchlevel\EventSourcing\Attribute\Create;
194194use Patchlevel\EventSourcing\Attribute\Drop;
195195use Patchlevel\EventSourcing\Attribute\Handle;
196196use Patchlevel\EventSourcing\EventBus\Message;
197- use Patchlevel\EventSourcing\Projection\Projection ;
197+ use Patchlevel\EventSourcing\Projection\Projector\Projector ;
198198
199- final class HotelProjection implements Projection
199+ final class HotelProjection implements Projector
200200{
201201 private Connection $db;
202202
203203 public function __construct(Connection $db)
204204 {
205205 $this->db = $db;
206206 }
207+
208+ /**
209+ * @return list<array {id: string, name: string, guests: int} >
210+ */
211+ public function getHotels(): array
212+ {
213+ return $this->db->fetchAllAssociative('SELECT id, name, guests FROM hotel;')
214+ }
207215
208216 #[Handle(HotelCreated::class)]
209217 public function handleHotelCreated(Message $message): void
@@ -252,7 +260,7 @@ final class HotelProjection implements Projection
252260
253261!!! warning
254262
255- autoconfigure need to be enabled, otherwise you need add the `event_sourcing.projection ` tag.
263+ autoconfigure need to be enabled, otherwise you need add the `event_sourcing.projector ` tag.
256264
257265!!! note
258266
@@ -336,27 +344,38 @@ final class HotelController
336344{
337345 /** @var Repository<Hotel > */
338346 private Repository $hotelRepository;
347+ private HotelProjection $hotelProjection;
339348
340- public function __construct(RepositoryManager $repositoryManager)
341- {
349+ public function __construct(
350+ RepositoryManager $repositoryManager,
351+ HotelProjection $hotelProjection
352+ ) {
342353 $this->hotelRepository = $repositoryManager->get(Hotel::class);
354+ $this->hotelProjection = $hotelProjection;
355+ }
356+
357+ #[Route("/", methods:["GET"])]
358+ public function listAction(): JsonResponse
359+ {
360+ return new JsonResponse(
361+ $this->hotelProjection->getHotels()
362+ );
343363 }
344364
345365 #[Route("/create", methods:["POST"])]
346366 public function createAction(Request $request): JsonResponse
347367 {
348- $name = $request->request->get('name'); // need validation!
368+ $hotelName = $request->request->get('name'); // need validation!
349369 $id = Uuid::v4();
350370
351- $hotel = Hotel::create($id, $name);
352-
371+ $hotel = Hotel::create($id, $hotelName);
353372 $this->hotelRepository->save($hotel);
354373
355374 return new JsonResponse(['id' => $id->toString()]);
356375 }
357376
358377 #[Route("/:id/check-in", methods:["POST"])]
359- public function createAction (string $id, Request $request): JsonResponse
378+ public function checkInAction (string $id, Request $request): JsonResponse
360379 {
361380 $id = Uuid::fromString($id);
362381 $guestName = $request->request->get('name'); // need validation!
@@ -365,11 +384,11 @@ final class HotelController
365384 $hotel->checkIn($guestName);
366385 $this->hotelRepository->save($hotel);
367386
368- return new JsonResponse(['id' => $id->toString()] );
387+ return new JsonResponse();
369388 }
370389
371390 #[Route("/:id/check-out", methods:["POST"])]
372- public function createAction (string $id, Request $request): JsonResponse
391+ public function checkOutAction (string $id, Request $request): JsonResponse
373392 {
374393 $id = Uuid::fromString($id);
375394 $guestName = $request->request->get('name'); // need validation!
@@ -378,7 +397,7 @@ final class HotelController
378397 $hotel->checkOut($guestName);
379398 $this->hotelRepository->save($hotel);
380399
381- return new JsonResponse(['id' => $id->toString()] );
400+ return new JsonResponse();
382401 }
383402}
384403```
0 commit comments