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 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/Model/Artwork.php (limited to 'src/Model/Artwork.php') 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; + } +} + -- cgit v1.2.3