Moving api requests to another controller.

This commit is contained in:
krzysiej
2022-06-06 14:27:02 +02:00
parent 314f4f2a14
commit f715f401e1
5 changed files with 36 additions and 24 deletions

View File

@@ -8,7 +8,6 @@ use App\Form\BookType;
use App\Form\FileType;
use App\Form\SearchType;
use App\Repository\BookRepository;
use App\Repository\FileRepository;
use App\Service\FileService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -17,7 +16,6 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Techtube\Bookinfo\BookFinder;
#[Route('/book')]
class BookController extends AbstractController
@@ -73,21 +71,19 @@ class BookController extends AbstractController
]);
}
#[Route('/search/{phrase}', name: 'app_book_search', methods: ['GET'])]
public function search(string $phrase): JsonResponse
{
$bookFinder = new BookFinder();
return new JsonResponse($bookFinder->search($phrase));
}
#[Route('/search/', name: 'app_book_search_book', methods: ['GET', 'POST'])]
#[Route('/search/', name: 'app_book_search', methods: ['GET', 'POST'])]
public function searchBook(BookRepository $bookRepository, Request $request): Response
{
$searchForm = $this->createForm(SearchType::class);
$searchForm->handleRequest($request);
$books = [];
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$books = $bookRepository->findByTitle($searchForm->get('search')->getData());
return $this->redirectToRoute('app_book_search', ['search' => ($searchForm->get('search')->getData())]);
}
if ($request->query->has('search')) {
$books = $bookRepository->findByTitle($request->query->get('search'));
}
return $this->renderForm('book/search.html.twig', [
'books' => $books,
@@ -95,18 +91,10 @@ class BookController extends AbstractController
]);
}
#[Route('/info/{urlInBase64}', name: 'app_book_info', methods: ['GET'])]
public function info(string $urlInBase64): JsonResponse
{
$bookFinder = new BookFinder();
return new JsonResponse($bookFinder->byUrl(base64_decode($urlInBase64)));
}
#[Route('/{id}', name: 'app_book_show', methods: ['GET', 'POST'])]
public function show(
Book $book,
Request $request,
FileRepository $fileRepository,
FileService $fileService
): Response {
$fileForm = $this->createForm(FileType::class);
@@ -136,7 +124,6 @@ class BookController extends AbstractController
Request $request,
Book $book,
BookRepository $bookRepository,
FileRepository $fileRepository,
FileService $fileService
): Response {
$form = $this->createForm(BookType::class, $book);

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Techtube\Bookinfo\BookFinder;
#[Route('/online')]
class OnlineSourceController extends AbstractController
{
#[Route('/search/{phrase}', name: 'app_online_search', methods: ['GET'])]
public function search(string $phrase, BookFinder $bookFinder): JsonResponse
{
return new JsonResponse($bookFinder->search($phrase));
}
#[Route('/info/{urlInBase64}', name: 'app_online_info', methods: ['GET'])]
public function info(string $urlInBase64, BookFinder $bookFinder): JsonResponse
{
return new JsonResponse($bookFinder->byUrl(base64_decode($urlInBase64)));
}
}