diff --git a/migrations/Version20220213005422.php b/migrations/Version20220213005422.php new file mode 100644 index 0000000000000000000000000000000000000000..331b38512a1ee7e69118c004756427f25b69b21e --- /dev/null +++ b/migrations/Version20220213005422.php @@ -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 Version20220213005422 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('ALTER TABLE "user" ADD completion_counter INT NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" DROP completion_counter'); + } +} diff --git a/src/Controller/CalendarController.php b/src/Controller/CalendarController.php index d3d22e7b56c094ee56799a1c7c0ea11214eadfff..3e7257a56d92af709fcc7658548e21d6b1b6d344 100644 --- a/src/Controller/CalendarController.php +++ b/src/Controller/CalendarController.php @@ -55,4 +55,23 @@ class CalendarController extends AbstractController return new Response("all good"); } + + #[Route('/calendar/send_click', name: 'calendar_send_click')] + #[IsGranted('ROLE_USER')] + public function calendarSendClick(EntityManagerInterface $manager) + { + /** @var User */ + $user = $this->getUser(); + if (!$user || !$user instanceof User) + { + throw new \LogicException("The button could not be pressed because there are no existing user."); + } + + $completionCunter = $user->getCompletionCounter(); + $user->setCompletionCounter($completionCunter++); + $manager->flush(); + + return new JsonResponse(["score", $user->calculateScore()]); + } + } diff --git a/src/Entity/User.php b/src/Entity/User.php index 84635275cfa0ed95a2c2ce33c41ff50a3a94b6e1..a68769476890f21b7c3431d58e040e1c0d3f5010 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -29,7 +29,10 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface private $displayName; #[ORM\Column(type: 'json', nullable: true)] - private $CalendarFile = []; + private $calendarFile = []; + + #[ORM\Column(type: 'integer')] + private $completionCounter = 0; public function getId(): ?int { @@ -115,13 +118,30 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface public function getCalendarFile(): ?array { - return $this->CalendarFile; + return $this->calendarFile; } - public function setCalendarFile(?array $CalendarFile): self + public function setCalendarFile(?array $calendarFile): self { - $this->CalendarFile = $CalendarFile; + $this->calendarFile = $calendarFile; return $this; } + + public function getCompletionCounter(): ?int + { + return $this->completionCounter; + } + + public function setCompletionCounter(int $completionCounter): self + { + $this->completionCounter = $completionCounter; + + return $this; + } + + public function calculateScore(): ?float + { + return log($this->completionCounter); + } }