From 1a2b801cd93c9700fcd150e50ac24d172ea5f51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Thu, 17 Oct 2024 21:31:31 +0200 Subject: [PATCH] Add repository with autowireing. --- src/Controller/StarshipApiController.php | 15 +++---------- src/Repository/StarshipRepository.php | 27 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 src/Repository/StarshipRepository.php diff --git a/src/Controller/StarshipApiController.php b/src/Controller/StarshipApiController.php index 469e6d0..c5cc075 100644 --- a/src/Controller/StarshipApiController.php +++ b/src/Controller/StarshipApiController.php @@ -2,8 +2,7 @@ namespace App\Controller; -use App\Model\Starship; -use Psr\Log\LoggerInterface; +use App\Repository\StarshipRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -11,17 +10,9 @@ use Symfony\Component\Routing\Attribute\Route; class StarshipApiController extends AbstractController { #[Route('/api/starships', name: 'starships')] - public function getCollection(LoggerInterface $logger): Response + public function getCollection(StarshipRepository $starshipRepository): Response { - $logger->info('Starships get collection'); - $starships = [ - new Starship( - 1, 'Starship 01', 'Heavy Starship', 'Human Captain', 'damaged', - ), - new Starship( - 2, 'Starship 02', 'Light Starship', 'Robot Captain', 'new', - ), - ]; + $starships = $starshipRepository->findAll(); return $this->json($starships); } diff --git a/src/Repository/StarshipRepository.php b/src/Repository/StarshipRepository.php new file mode 100644 index 0000000..5f3b436 --- /dev/null +++ b/src/Repository/StarshipRepository.php @@ -0,0 +1,27 @@ +logger->info('Starship Repository: Finding Starships'); + + return [ + new Starship( + 1, 'Starship 01', 'Heavy Starship', 'Human Captain', 'damaged', + ), + new Starship( + 2, 'Starship 02', 'Light Starship', 'Robot Captain', 'new', + ), + ]; + } +}