From 2c113e9e23c8ce059faca3b9964111063ef50fe8 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 20 Nov 2019 11:24:54 +0100 Subject: [PATCH] :zap: routine deletion --- src/Controller/DefaultController.php | 36 +++++++++++++++++++++++ src/Entity/Owner.php | 6 ++-- src/Entity/Poll.php | 44 ++++++++++++++-------------- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 4c9be99..2f177a9 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -151,6 +151,42 @@ class DefaultController extends AbstractController { 200 ); } + /** + * Delete all expired polls and their children + * @Get( + * path = "/clean-polls", + * name = "clean_expired_polls", + * ) + */ + public function cleanExpiredPolls() { + $em = $this->getDoctrine()->getManager(); + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + + $queryFind = $em->createQuery( + 'SELECT p + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + $queryDelete = $em->createQuery( + 'DELETE + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + + $foundPolls = $queryFind->getResult(); + var_dump( count( $foundPolls ) ); + + $em->flush(); + + return $this->json( [ + 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), + 'data' => [ + 'count' => count( $foundPolls ), + ], + ], + 200 ); + } + /** * @Put( * path = "/poll/{id}", diff --git a/src/Entity/Owner.php b/src/Entity/Owner.php index ab9757a..efebe2a 100644 --- a/src/Entity/Owner.php +++ b/src/Entity/Owner.php @@ -30,18 +30,18 @@ class Owner { */ private $email; /** - * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner",cascade={"persist","remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner",cascade={"persist","remove"},orphanRemoval=true) * @Serializer\Type("App\Entity\Poll") */ private $polls; /** - * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="owner", cascade={"persist","remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="owner", cascade={"persist","remove"},orphanRemoval=true) */ private $comments; /** - * @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="owner", cascade={"persist","remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="owner", cascade={"persist","remove"},orphanRemoval=true) */ private $stackOfVotes; diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 00313cf..3306619 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -110,7 +110,7 @@ class Poll { */ public $choices; /** - * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll") + * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"}) * @Serializer\Type("App\Entity\Comment") */ public $comments; @@ -147,7 +147,7 @@ class Poll { public function setTitle( string $title ): self { $this->title = $title; - + return $this; } @@ -157,13 +157,13 @@ class Poll { public function setCreationDate( DateTimeInterface $creationDate ): self { $this->creationDate = $creationDate; - + return $this; } public function setExpiracyDate( DateTimeInterface $expiracyDate ): self { $this->expiracyDate = $expiracyDate; - + return $this; } @@ -173,7 +173,7 @@ class Poll { public function setOwner( ?Owner $owner ): self { $this->owner = $owner; - + return $this; } @@ -191,7 +191,7 @@ class Poll { public function setAdminKey( string $adminKey ): self { $this->adminKey = $adminKey; - + return $this; } @@ -201,7 +201,7 @@ class Poll { public function setDescription( string $description ): self { $this->description = $description; - + return $this; } @@ -211,7 +211,7 @@ class Poll { public function setKind( string $kind ): self { $this->kind = $kind; - + return $this; } @@ -221,7 +221,7 @@ class Poll { public function setCustomUrl( string $customUrl ): self { $this->customUrl = $customUrl; - + return $this; } @@ -231,7 +231,7 @@ class Poll { public function setPassword( string $password ): self { $this->password = $password; - + return $this; } @@ -241,7 +241,7 @@ class Poll { public function setModificationPolicy( string $modificationPolicy ): self { $this->modificationPolicy = $modificationPolicy; - + return $this; } @@ -251,7 +251,7 @@ class Poll { public function setMailOnComment( bool $mailOnComment ): self { $this->mailOnComment = $mailOnComment; - + return $this; } @@ -261,7 +261,7 @@ class Poll { public function setMailOnVote( bool $mailOnVote ): self { $this->mailOnVote = $mailOnVote; - + return $this; } @@ -271,7 +271,7 @@ class Poll { public function setHideResults( bool $hideResults ): self { $this->hideResults = $hideResults; - + return $this; } @@ -281,7 +281,7 @@ class Poll { public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self { $this->showResultEvenIfPasswords = $showResultEvenIfPasswords; - + return $this; } @@ -298,7 +298,7 @@ class Poll { $this->comments[] = $comment; $comment->setPoll( $this ); } - + return $this; } @@ -310,7 +310,7 @@ class Poll { $comment->setPoll( null ); } } - + return $this; } @@ -320,7 +320,7 @@ class Poll { public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self { $this->stacksOfVotes = $stacksOfVotes; - + return $this; } @@ -336,7 +336,7 @@ class Poll { $this->stackOfVotes[] = $stackOfVote; $stackOfVote->setPoll( $this ); } - + return $this; } @@ -348,7 +348,7 @@ class Poll { $stackOfVote->setPoll( null ); } } - + return $this; } @@ -361,7 +361,7 @@ class Poll { $this->votes[] = $vote; $vote->setPoll( $this ); } - + return $this; } @@ -373,7 +373,7 @@ class Poll { $vote->setPoll( null ); } } - + return $this; }