Added table for tracking progress of a book.

This commit is contained in:
krzysiej
2022-06-06 15:33:20 +02:00
parent f715f401e1
commit 21743d032c
5 changed files with 216 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220606123515 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View File

@@ -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<int, Progress>
*/
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;
}
}

66
src/Entity/Progress.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace App\Entity;
use App\Repository\ProgressRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProgressRepository::class)]
class Progress
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'integer')]
private $pages;
#[ORM\Column(type: 'date')]
private $date;
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'progress')]
#[ORM\JoinColumn(nullable: false)]
private $book;
public function getId(): ?int
{
return $this->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;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Progress;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Progress>
*
* @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()
// ;
// }
}

View File

@@ -3,10 +3,13 @@
{% block title %}Book{% endblock %}
{% block body %}
<div class="progress" style="height: 2px;">
<div class="progress-bar" role="progressbar" style="width: {{ (book.getProgress().first().pages / book.pages*100) | round }}%"></div>
<div class="progress-bar bg-secondary bg-opacity-10" role="progressbar" style="width: {{ 100-(book.getProgress().first().pages / book.pages*100) | round }}%"></div>
</div>
<div class="d-flex">
<div>
{% if file_exists(asset('book_covers/cover_' ~ book.id ~ '.jpg')) %}
<img class="img-thumbnail"
style="max-width: 80px"
@@ -38,9 +41,7 @@
</tr>
<tr>
<th>Isbn</th>
<td>{{ book.isbn }}</td>
<th>Pages</th>
<td>{{ book.pages }}</td>
<td colspan="3">{{ book.isbn }}</td>
</tr>
<tr>
<th>Tags</th>
@@ -54,6 +55,13 @@
<th>Language</th>
<td colspan="3">{{ book.language }}</td>
</tr>
<tr>
<th>Progress</th>
<td>{{ book.getProgress().first().pages }}</td>
<th>Pages</th>
<td>{{ book.pages }}</td>
</tr>
</tbody>
</table>