Moving api requests to another controller.
This commit is contained in:
@@ -53,13 +53,13 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
search: function () {
|
search: function () {
|
||||||
axios.get('/book/search/' + this.searchTerm).then(response => {
|
axios.get('/online/search/' + this.searchTerm).then(response => {
|
||||||
this.books = response.data;
|
this.books = response.data;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getBookDetails: function (url) {
|
getBookDetails: function (url) {
|
||||||
this.manualMode = true;
|
this.manualMode = true;
|
||||||
axios.get('/book/info/' + btoa(url)).then(response => {
|
axios.get('/online/info/' + btoa(url)).then(response => {
|
||||||
const book = this.selectedBook = response.data;
|
const book = this.selectedBook = response.data;
|
||||||
this.$el.querySelector('#book_title').value = book.title;
|
this.$el.querySelector('#book_title').value = book.title;
|
||||||
this.$el.querySelector('#book_author').value = book.author;
|
this.$el.querySelector('#book_author').value = book.author;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ services:
|
|||||||
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
|
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
|
||||||
|
|
||||||
|
|
||||||
|
Techtube\Bookinfo\BookFinder:
|
||||||
|
|
||||||
# makes classes in src/ available to be used as services
|
# makes classes in src/ available to be used as services
|
||||||
# this creates a service per class whose id is the fully-qualified class name
|
# this creates a service per class whose id is the fully-qualified class name
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use App\Form\BookType;
|
|||||||
use App\Form\FileType;
|
use App\Form\FileType;
|
||||||
use App\Form\SearchType;
|
use App\Form\SearchType;
|
||||||
use App\Repository\BookRepository;
|
use App\Repository\BookRepository;
|
||||||
use App\Repository\FileRepository;
|
|
||||||
use App\Service\FileService;
|
use App\Service\FileService;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
@@ -17,7 +16,6 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||||
use Techtube\Bookinfo\BookFinder;
|
|
||||||
|
|
||||||
#[Route('/book')]
|
#[Route('/book')]
|
||||||
class BookController extends AbstractController
|
class BookController extends AbstractController
|
||||||
@@ -73,21 +71,19 @@ class BookController extends AbstractController
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/search/{phrase}', name: 'app_book_search', methods: ['GET'])]
|
#[Route('/search/', name: 'app_book_search', methods: ['GET', 'POST'])]
|
||||||
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'])]
|
|
||||||
public function searchBook(BookRepository $bookRepository, Request $request): Response
|
public function searchBook(BookRepository $bookRepository, Request $request): Response
|
||||||
{
|
{
|
||||||
$searchForm = $this->createForm(SearchType::class);
|
$searchForm = $this->createForm(SearchType::class);
|
||||||
$searchForm->handleRequest($request);
|
$searchForm->handleRequest($request);
|
||||||
$books = [];
|
$books = [];
|
||||||
|
|
||||||
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
|
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', [
|
return $this->renderForm('book/search.html.twig', [
|
||||||
'books' => $books,
|
'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'])]
|
#[Route('/{id}', name: 'app_book_show', methods: ['GET', 'POST'])]
|
||||||
public function show(
|
public function show(
|
||||||
Book $book,
|
Book $book,
|
||||||
Request $request,
|
Request $request,
|
||||||
FileRepository $fileRepository,
|
|
||||||
FileService $fileService
|
FileService $fileService
|
||||||
): Response {
|
): Response {
|
||||||
$fileForm = $this->createForm(FileType::class);
|
$fileForm = $this->createForm(FileType::class);
|
||||||
@@ -136,7 +124,6 @@ class BookController extends AbstractController
|
|||||||
Request $request,
|
Request $request,
|
||||||
Book $book,
|
Book $book,
|
||||||
BookRepository $bookRepository,
|
BookRepository $bookRepository,
|
||||||
FileRepository $fileRepository,
|
|
||||||
FileService $fileService
|
FileService $fileService
|
||||||
): Response {
|
): Response {
|
||||||
$form = $this->createForm(BookType::class, $book);
|
$form = $this->createForm(BookType::class, $book);
|
||||||
|
|||||||
25
src/Controller/OnlineSourceController.php
Normal file
25
src/Controller/OnlineSourceController.php
Normal 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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ class SearchType extends AbstractType
|
|||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
{
|
{
|
||||||
$builder
|
$builder
|
||||||
->setAction($this->router->generate('app_book_search_book'))
|
->setAction($this->router->generate('app_book_search'))
|
||||||
->add('search', TextType::class, ['attr' => ['placeholder' => 'Book title']])
|
->add('search', TextType::class, ['attr' => ['placeholder' => 'Book title']])
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,6 @@ class SearchType extends AbstractType
|
|||||||
public function configureOptions(OptionsResolver $resolver): void
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
{
|
{
|
||||||
$resolver->setDefaults([
|
$resolver->setDefaults([
|
||||||
// Configure your form options here
|
|
||||||
'attr' => ['class' => 'd-flex']
|
'attr' => ['class' => 'd-flex']
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user