From 21743d032c34091cb583e4dcdbf0d3fd905f5c10 Mon Sep 17 00:00:00 2001 From: krzysiej Date: Mon, 6 Jun 2022 15:33:20 +0200 Subject: [PATCH] Added table for tracking progress of a book. --- migrations/Version20220606123515.php | 32 +++++++++++++ src/Entity/Book.php | 43 +++++++++++++++-- src/Entity/Progress.php | 66 +++++++++++++++++++++++++++ src/Repository/ProgressRepository.php | 66 +++++++++++++++++++++++++++ templates/book/show.html.twig | 18 ++++++-- 5 files changed, 216 insertions(+), 9 deletions(-) create mode 100644 migrations/Version20220606123515.php create mode 100644 src/Entity/Progress.php create mode 100644 src/Repository/ProgressRepository.php diff --git a/migrations/Version20220606123515.php b/migrations/Version20220606123515.php new file mode 100644 index 0000000..a613d1f --- /dev/null +++ b/migrations/Version20220606123515.php @@ -0,0 +1,32 @@ +addSql('CREATE TABLE progress (id INT AUTO_INCREMENT NOT NULL, book_id INT NOT NULL, pages INT NOT NULL, date DATE NOT NULL, INDEX IDX_2201F24616A2B381 (book_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE progress ADD CONSTRAINT FK_2201F24616A2B381 FOREIGN KEY (book_id) REFERENCES book (id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE progress'); + } +} diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 11a7499..e393b27 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -49,20 +49,25 @@ class Book private $author; #[ORM\Column(type: 'text', nullable: true)] - private $tags; + private ?string $tags; #[ORM\Column(type: 'integer', nullable: true)] - private $pages; + private ?int $pages; #[ORM\Column(type: 'smallint', nullable: true)] - private $rating; + private ?int $rating; #[ORM\Column(type: 'text', nullable: true)] - private $category; + private ?string $category; + + #[ORM\OneToMany(mappedBy: 'book', targetEntity: Progress::class)] + #[ORM\OrderBy(['date' => 'DESC'])] + private $progress; public function __construct() { $this->files = new ArrayCollection(); + $this->progress = new ArrayCollection(); } public function getId(): ?int @@ -267,4 +272,34 @@ class Book return $this; } + + /** + * @return Collection + */ + public function getProgress(): Collection + { + return $this->progress; + } + + public function addProgress(Progress $progress): self + { + if (!$this->progress->contains($progress)) { + $this->progress[] = $progress; + $progress->setBook($this); + } + + return $this; + } + + public function removeProgress(Progress $progress): self + { + if ($this->progress->removeElement($progress)) { + // set the owning side to null (unless already changed) + if ($progress->getBook() === $this) { + $progress->setBook(null); + } + } + + return $this; + } } diff --git a/src/Entity/Progress.php b/src/Entity/Progress.php new file mode 100644 index 0000000..883cae9 --- /dev/null +++ b/src/Entity/Progress.php @@ -0,0 +1,66 @@ +id; + } + + public function getPages(): ?int + { + return $this->pages; + } + + public function setPages(int $pages): self + { + $this->pages = $pages; + + return $this; + } + + public function getDate(): ?\DateTimeInterface + { + return $this->date; + } + + public function setDate(\DateTimeInterface $date): self + { + $this->date = $date; + + return $this; + } + + public function getBook(): ?Book + { + return $this->book; + } + + public function setBook(?Book $book): self + { + $this->book = $book; + + return $this; + } +} diff --git a/src/Repository/ProgressRepository.php b/src/Repository/ProgressRepository.php new file mode 100644 index 0000000..fc37d4c --- /dev/null +++ b/src/Repository/ProgressRepository.php @@ -0,0 +1,66 @@ + + * + * @method Progress|null find($id, $lockMode = null, $lockVersion = null) + * @method Progress|null findOneBy(array $criteria, array $orderBy = null) + * @method Progress[] findAll() + * @method Progress[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class ProgressRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Progress::class); + } + + public function add(Progress $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(Progress $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + +// /** +// * @return Progress[] Returns an array of Progress objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('p.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?Progress +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/templates/book/show.html.twig b/templates/book/show.html.twig index e915dd3..bf69fd4 100644 --- a/templates/book/show.html.twig +++ b/templates/book/show.html.twig @@ -3,10 +3,13 @@ {% block title %}Book{% endblock %} {% block body %} + +
+
+
+
-
- {% if file_exists(asset('book_covers/cover_' ~ book.id ~ '.jpg')) %} Isbn - {{ book.isbn }} - Pages - {{ book.pages }} + {{ book.isbn }} Tags @@ -54,6 +55,13 @@ Language {{ book.language }} + + Progress + {{ book.getProgress().first().pages }} + Pages + {{ book.pages }} + +