diff --git a/src/Controller/BookController.php b/src/Controller/BookController.php index a3ba35a..8568b10 100644 --- a/src/Controller/BookController.php +++ b/src/Controller/BookController.php @@ -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 { diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 5e7f4b7..11a7499 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -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; } diff --git a/src/Form/SearchType.php b/src/Form/SearchType.php new file mode 100644 index 0000000..95cdd09 --- /dev/null +++ b/src/Form/SearchType.php @@ -0,0 +1,35 @@ +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'] + ]); + } +} diff --git a/src/Repository/BookRepository.php b/src/Repository/BookRepository.php index 079cddb..017141f 100644 --- a/src/Repository/BookRepository.php +++ b/src/Repository/BookRepository.php @@ -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 // { diff --git a/src/Service/FileService.php b/src/Service/FileService.php index ed3e1df..f46ff39 100644 --- a/src/Service/FileService.php +++ b/src/Service/FileService.php @@ -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); diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index fabd62c..5eda027 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -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(); + } } \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 203ff9c..636ef31 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -49,10 +49,11 @@ Add new book -
+ {% set search_form = render_search_form() %} + {{ form_start(search_form) }} + {{ form_widget(search_form.search, { 'attr' : { 'class': 'me-2' }}) }} + + {{ form_end(search_form) }} diff --git a/templates/book/index.html.twig b/templates/book/index.html.twig index 98a38e9..c7a58cc 100644 --- a/templates/book/index.html.twig +++ b/templates/book/index.html.twig @@ -16,8 +16,7 @@| Cover | -Id | +Title | Language | Description | @@ -29,19 +28,23 @@|||||
|---|---|---|---|---|---|---|---|---|---|
{% if file_exists(asset('book_covers/cover_' ~ book.id ~ '.jpg')) %} {% endif %}
+ |
+ {% if file_exists(asset('book_covers/cover_' ~ book.id ~ '.jpg')) %}
+
+
+ {% endif %}
|
- {{ book.id }} | -{{ book.Title }} | +{{ book.title }} | {{ book.language }} | {{ book.description | slice(0, 200) }} | {{ book.publisher }} | {{ book.publishDate ? book.publishDate|date('Y-m-d') : '' }} | - show - edit + show + edit |
| + | Title | +Language | +Description | +Publisher | +Publish_date | +actions | +||
|---|---|---|---|---|---|---|---|---|
+ {% if file_exists(asset('book_covers/cover_' ~ book.id ~ '.jpg')) %}
+
+
+ {% endif %}
+ |
+ {{ book.title }} | +{{ book.language }} | +{{ book.description | slice(0, 200) }} | +{{ book.publisher }} | +{{ book.publishDate ? book.publishDate|date('Y-m-d') : '' }} | ++ show + edit + | +||
| no records found | +||||||||
{% endif %}
+ {{ book.author }}
+{% for i in range(1, book.rating) %}⭐{% endfor %}
+| Id | -{{ book.id }} | -||||
|---|---|---|---|---|---|
| Title | -{{ book.Title }} | -||||
| Language | -{{ book.language }} | +Category | +{{ book.category }} | ||
| Description | -{{ book.description | nl2br }} | +{{ book.description | nl2br }} | |||
| Publisher | {{ book.publisher }} | +Publish date | +{{ book.publishDate ? book.publishDate|date('Y-m-d') : '' }} | ||
| Publish_date | -{{ book.publishDate ? book.publishDate|date('Y-m-d') : '' }} | +Isbn | +{{ book.isbn }} | +Pages | +{{ book.pages }} | +
| Tags | +{{ book.tags }} | +||||
| Series | +{{ book.series }} {{ book.volume }} | +||||
| Language | +{{ book.language }} | ||||