Added book search.

This commit is contained in:
krzysiej
2022-06-03 15:17:46 +02:00
parent b21d213b19
commit 314f4f2a14
10 changed files with 192 additions and 46 deletions

View File

@@ -6,6 +6,7 @@ use App\Entity\Book;
use App\Entity\File;
use App\Form\BookType;
use App\Form\FileType;
use App\Form\SearchType;
use App\Repository\BookRepository;
use App\Repository\FileRepository;
use App\Service\FileService;
@@ -79,6 +80,21 @@ class BookController extends AbstractController
return new JsonResponse($bookFinder->search($phrase));
}
#[Route('/search/', name: 'app_book_search_book', 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->renderForm('book/search.html.twig', [
'books' => $books,
'searchTerm' => $searchForm->get('search')->getData()
]);
}
#[Route('/info/{urlInBase64}', name: 'app_book_info', methods: ['GET'])]
public function info(string $urlInBase64): JsonResponse
{

View File

@@ -16,7 +16,7 @@ class Book
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $Title;
private $title;
#[ORM\Column(type: 'string', length: 255)]
private $language;
@@ -72,12 +72,12 @@ class Book
public function getTitle(): ?string
{
return $this->Title;
return $this->title;
}
public function setTitle(string $Title): self
public function setTitle(string $title): self
{
$this->Title = $Title;
$this->title = $title;
return $this;
}

35
src/Form/SearchType.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SearchType extends AbstractType
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->setAction($this->router->generate('app_book_search_book'))
->add('search', TextType::class, ['attr' => ['placeholder' => 'Book title']])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
// Configure your form options here
'attr' => ['class' => 'd-flex']
]);
}
}

View File

@@ -39,20 +39,18 @@ class BookRepository extends ServiceEntityRepository
}
}
// /**
// * @return Book[] Returns an array of Book objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('b')
// ->andWhere('b.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('b.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
/**
* @return Book[] Returns an array of Book objects
*/
public function findByTitle($value): array
{
return $this->createQueryBuilder('b')
->andWhere('LOWER(b.title) LIKE :title')
->setParameter('title', '%' . strtolower($value) . '%')
->orderBy('b.id', 'ASC')
->getQuery()
->getResult();
}
// public function findOneBySomeField($value): ?Book
// {

View File

@@ -60,7 +60,7 @@ class FileService
$file = new File();
$file->setFileName(trim($ebook->getClientOriginalName()));
$file->setFileSize($ebook->getSize());
$file->setExtension($ebook->guessClientExtension());
$file->setExtension($ebook->getClientOriginalExtension());
$file->setBook($book);
$this->fileRepository->add($file, true);

View File

@@ -3,16 +3,27 @@
namespace App\Twig;
use App\Form\SearchType;
use Symfony\Component\Form\FormFactoryInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
private $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function getFunctions()
{
return [
new TwigFunction('file_exists', [$this, 'file_exists']),
new TwigFunction('render_search_form', [$this, 'render_search_form']),
];
}
@@ -34,4 +45,9 @@ class AppExtension extends AbstractExtension
$suffix = array("B", "KB", "MB", "GB", "TB")[floor($base)];
return round(pow(1024, $base - floor($base)), 2) . $suffix;
}
public function render_search_form()
{
return $this->formFactory->create(SearchType::class)->createView();
}
}