From 4cacc94240944ff316104bfd1b5e8e00fad14517 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 14 Jan 2022 18:21:36 +0100 Subject: Add Artworks, Votes and better routing --- src/Model/Artwork.php | 101 ++++++++++++++++++++++++++++++++++++++++++++++ src/Model/Card.php | 49 ++++++++++++++++++++++ src/Model/Vote.php | 30 ++++++++++++++ src/Model/VoteArtwork.php | 26 ++++++++++++ src/Model/VoteCard.php | 26 ++++++++++++ 5 files changed, 232 insertions(+) create mode 100644 src/Model/Artwork.php create mode 100644 src/Model/Vote.php create mode 100644 src/Model/VoteArtwork.php create mode 100644 src/Model/VoteCard.php (limited to 'src/Model') diff --git a/src/Model/Artwork.php b/src/Model/Artwork.php new file mode 100644 index 0000000..25f6ee5 --- /dev/null +++ b/src/Model/Artwork.php @@ -0,0 +1,101 @@ +path = $path; + $this->votes = new ArrayCollection(); + } + + /** + * @param UploadedFile $file + * + * @return self + */ + public static function fromUploadedFile(UploadedFile $file): self + { + $artworksDir = dirname(dirname(__DIR__)) . '/public/artworks/'; + $path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $artworksDir . $file->getClientOriginalName()); + $file->move($artworksDir, $file->getClientOriginalName()); + + return new self($path); + } + + /** + * @param VoteArtwork $vote + */ + public function addVote(VoteArtwork $vote) + { + $vote->artwork = $this; + $this->votes[] = $vote; + } + + /** + * @return int + */ + public function getVotesTotal(): int + { + $result = DB::$entityManager + ->createQuery( + 'SELECT sum(v.value) as total + FROM Elements\Model\VoteArtwork v + WHERE v.artwork = :artwork' + ) + ->setParameter('artwork', $this) + ->getOneOrNullResult(); + + return $result['total'] ?? 0; + } +} + diff --git a/src/Model/Card.php b/src/Model/Card.php index ab421ab..9707e16 100644 --- a/src/Model/Card.php +++ b/src/Model/Card.php @@ -37,12 +37,26 @@ class Card */ public Collection|ArrayCollection|PersistentCollection $meta; + #[OneToMany(targetEntity: Artwork::class, mappedBy: 'card')] + /** + * @OneToMany(targetEntity="Artwork", mappedBy="card") + */ + public Collection|ArrayCollection|PersistentCollection $artworks; + + #[OneToMany(targetEntity: VoteCard::class, mappedBy: 'card')] + /** + * @OneToMany(targetEntity="VoteCard", mappedBy="card") + */ + public Collection|ArrayCollection|PersistentCollection $votes; + /** * Card constructor. */ public function __construct() { $this->meta = new ArrayCollection(); + $this->artworks = new ArrayCollection(); + $this->votes = new ArrayCollection(); } /** @@ -54,6 +68,24 @@ class Card $this->meta[] = $meta; } + /** + * @param Artwork $artwork + */ + public function addArtwork(Artwork $artwork) + { + $artwork->card = $this; + $this->artworks[] = $artwork; + } + + /** + * @param VoteCard $vote + */ + public function addVote(VoteCard $vote) + { + $vote->card = $this; + $this->votes[] = $vote; + } + /** * @param string $key * @@ -83,5 +115,22 @@ class Card return $result['value'] ?? null; } + + /** + * @return int + */ + public function getVotesTotal(): int + { + $result = DB::$entityManager + ->createQuery( + 'SELECT sum(v.value) as total + FROM Elements\Model\VoteCard v + WHERE v.card = :card' + ) + ->setParameter('card', $this) + ->getOneOrNullResult(); + + return $result['total'] ?? 0; + } } diff --git a/src/Model/Vote.php b/src/Model/Vote.php new file mode 100644 index 0000000..c5415aa --- /dev/null +++ b/src/Model/Vote.php @@ -0,0 +1,30 @@ +