Add repository with autowireing.
This commit is contained in:
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Model\Starship;
|
use App\Repository\StarshipRepository;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -11,17 +10,9 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||||||
class StarshipApiController extends AbstractController
|
class StarshipApiController extends AbstractController
|
||||||
{
|
{
|
||||||
#[Route('/api/starships', name: 'starships')]
|
#[Route('/api/starships', name: 'starships')]
|
||||||
public function getCollection(LoggerInterface $logger): Response
|
public function getCollection(StarshipRepository $starshipRepository): Response
|
||||||
{
|
{
|
||||||
$logger->info('Starships get collection');
|
$starships = $starshipRepository->findAll();
|
||||||
$starships = [
|
|
||||||
new Starship(
|
|
||||||
1, 'Starship 01', 'Heavy Starship', 'Human Captain', 'damaged',
|
|
||||||
),
|
|
||||||
new Starship(
|
|
||||||
2, 'Starship 02', 'Light Starship', 'Robot Captain', 'new',
|
|
||||||
),
|
|
||||||
];
|
|
||||||
|
|
||||||
return $this->json($starships);
|
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