Add repository with autowireing.

This commit is contained in:
Krzysztof Płaczek
2024-10-17 21:31:31 +02:00
parent 303d742659
commit 1a2b801cd9
2 changed files with 30 additions and 12 deletions

View File

@@ -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);
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Repository;
use App\Model\Starship;
use Psr\Log\LoggerInterface;
class StarshipRepository
{
public function __construct(private LoggerInterface $logger)
{
}
public function findAll(): array
{
$this->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',
),
];
}
}