Add repository with autowireing.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
27
src/Repository/StarshipRepository.php
Normal file
27
src/Repository/StarshipRepository.php
Normal 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',
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user