42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Service\GoogleBooksService;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
class APISearchController extends AbstractController
|
|
{
|
|
private GoogleBooksService $googleBooksService;
|
|
|
|
public function __construct(GoogleBooksService $googleBooksService)
|
|
{
|
|
$this->googleBooksService = $googleBooksService;
|
|
}
|
|
|
|
public function search(Request $request): array
|
|
{
|
|
// Récupérer le paramètre "q" depuis la requête
|
|
$query = $request->query->get('q');
|
|
|
|
// Appeler le service GoogleBooks avec la requête
|
|
return $this->googleBooksService->searchBooks($query);
|
|
}
|
|
|
|
#[Route('/api/search', name: 'api_search')]
|
|
public function index(Request $request): Response
|
|
{
|
|
// Appeler la méthode search et récupérer les résultats
|
|
$datas = $this->search($request);
|
|
|
|
// Afficher les résultats dans le template
|
|
return $this->render('apiSearch/index.html.twig', [
|
|
'controller_name' => 'APISearchController',
|
|
'datas' => $datas,
|
|
]);
|
|
}
|
|
}
|