Removed encore added books crud
This commit is contained in:
126
src/Controller/BookController.php
Normal file
126
src/Controller/BookController.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Book;
|
||||
use App\Form\BookType;
|
||||
use App\Repository\BookRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
#[Route('/book')]
|
||||
class BookController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_book_index', methods: ['GET'])]
|
||||
public function index(BookRepository $bookRepository): Response
|
||||
{
|
||||
return $this->render('book/index.html.twig', [
|
||||
'books' => $bookRepository->findAll(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_book_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, BookRepository $bookRepository): Response
|
||||
{
|
||||
$book = new Book();
|
||||
$form = $this->createForm(BookType::class, $book);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$bookRepository->add($book, true);
|
||||
|
||||
$cover = $request->files->get('book')['cover'];
|
||||
if ($cover) {
|
||||
$cover->move(
|
||||
$this->getParameter('book_covers'),
|
||||
'cover_' . $book->getId() . '.' . $cover->guessClientExtension()
|
||||
);
|
||||
}
|
||||
|
||||
$ebook = $request->files->get('book')['ebook'];
|
||||
if ($ebook) {
|
||||
$ebook->move(
|
||||
$this->getParameter('book_files'),
|
||||
'ebook_' . $book->getId() . '.' . $ebook->guessClientExtension()
|
||||
);
|
||||
}
|
||||
|
||||
dd($book->getId());
|
||||
|
||||
return $this->redirectToRoute('app_book_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->renderForm('book/new.html.twig', [
|
||||
'book' => $book,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_book_show', methods: ['GET'])]
|
||||
public function show(Book $book): Response
|
||||
{
|
||||
return $this->render('book/show.html.twig', [
|
||||
'book' => $book,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/edit', name: 'app_book_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Book $book, BookRepository $bookRepository): Response
|
||||
{
|
||||
$form = $this->createForm(BookType::class, $book);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$cover = $request->files->get('book')['cover'];
|
||||
if ($cover) {
|
||||
$cover->move(
|
||||
$this->getParameter('book_covers'),
|
||||
'cover_' . $book->getId() . '.jpg'
|
||||
);
|
||||
}
|
||||
|
||||
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $ebook */
|
||||
$ebook = $request->files->get('book')['ebook'];
|
||||
if ($ebook) {
|
||||
$ebook->move(
|
||||
$this->getParameter('book_files'),
|
||||
'ebook_' . $book->getId() . '.' . $ebook->guessClientExtension()
|
||||
);
|
||||
}
|
||||
|
||||
$book->setOriginalFilename($ebook->getClientOriginalName());
|
||||
$bookRepository->add($book, true);
|
||||
|
||||
|
||||
return $this->redirectToRoute('app_book_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->renderForm('book/edit.html.twig', [
|
||||
'book' => $book,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_book_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Book $book, BookRepository $bookRepository): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete' . $book->getId(), $request->request->get('_token'))) {
|
||||
$bookRepository->remove($book, true);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_book_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
#[Route('/download/{id}', name: 'app_book_download', methods: ['GET'])]
|
||||
public function downloadBook(Book $book): BinaryFileResponse
|
||||
{
|
||||
return $this->file(
|
||||
$this->getParameter('book_files') . '/' . 'ebook_' . $book->getId() . '.'.pathinfo($book->getOriginalFilename(), PATHINFO_EXTENSION),
|
||||
$book->getOriginalFilename()
|
||||
);
|
||||
}
|
||||
}
|
||||
140
src/Entity/Book.php
Normal file
140
src/Entity/Book.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\BookRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: BookRepository::class)]
|
||||
class Book
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private $id;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private $Title;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private $language;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private $description;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private $publisher;
|
||||
|
||||
#[ORM\Column(type: 'date', nullable: true)]
|
||||
private $publish_date;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private $filename;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private $original_filename;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private $subtitle;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getTitle(): ?string
|
||||
{
|
||||
return $this->Title;
|
||||
}
|
||||
|
||||
public function setTitle(string $Title): self
|
||||
{
|
||||
$this->Title = $Title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLanguage(): ?string
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
public function setLanguage(string $language): self
|
||||
{
|
||||
$this->language = $language;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setDescription(?string $description): self
|
||||
{
|
||||
$this->description = $description;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPublisher(): ?string
|
||||
{
|
||||
return $this->publisher;
|
||||
}
|
||||
|
||||
public function setPublisher(?string $publisher): self
|
||||
{
|
||||
$this->publisher = $publisher;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPublishDate(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->publish_date;
|
||||
}
|
||||
|
||||
public function setPublishDate(?\DateTimeInterface $publish_date): self
|
||||
{
|
||||
$this->publish_date = $publish_date;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFilename(): ?string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
public function setFilename(?string $filename): self
|
||||
{
|
||||
$this->filename = $filename;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOriginalFilename(): ?string
|
||||
{
|
||||
return $this->original_filename;
|
||||
}
|
||||
|
||||
public function setOriginalFilename(?string $original_filename): self
|
||||
{
|
||||
$this->original_filename = $original_filename;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSubtitle(): ?string
|
||||
{
|
||||
return $this->subtitle;
|
||||
}
|
||||
|
||||
public function setSubtitle(?string $subtitle): self
|
||||
{
|
||||
$this->subtitle = $subtitle;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
32
src/Form/BookType.php
Normal file
32
src/Form/BookType.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Book;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class BookType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('Title')
|
||||
->add('language')
|
||||
->add('description')
|
||||
->add('publisher')
|
||||
->add('publish_date')
|
||||
->add('ebook', FileType::class, ['mapped' => false, 'data_class' => null, 'required' => false])
|
||||
->add('cover', FileType::class, ['mapped' => false, 'data_class' => null, 'required' => false])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Book::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
66
src/Repository/BookRepository.php
Normal file
66
src/Repository/BookRepository.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Book;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Book>
|
||||
*
|
||||
* @method Book|null find($id, $lockMode = null, $lockVersion = null)
|
||||
* @method Book|null findOneBy(array $criteria, array $orderBy = null)
|
||||
* @method Book[] findAll()
|
||||
* @method Book[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
|
||||
*/
|
||||
class BookRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Book::class);
|
||||
}
|
||||
|
||||
public function add(Book $entity, bool $flush = false): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(Book $entity, bool $flush = false): void
|
||||
{
|
||||
$this->getEntityManager()->remove($entity);
|
||||
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @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()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Book
|
||||
// {
|
||||
// return $this->createQueryBuilder('b')
|
||||
// ->andWhere('b.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
22
src/Twig/AppExtension.php
Normal file
22
src/Twig/AppExtension.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('file_exists', [$this, 'file_exists']),
|
||||
];
|
||||
}
|
||||
|
||||
public function file_exists(string $file): bool
|
||||
{
|
||||
return file_exists(ltrim($file, '/'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user