From 0313cbf2c44978307fcff674936163a232f2f627 Mon Sep 17 00:00:00 2001
From: Justine Ouellette <justine.ouellette@polymtl.ca>
Date: Sat, 12 Feb 2022 20:00:05 -0500
Subject: [PATCH] Backend completion

---
 migrations/Version20220213005422.php  | 32 +++++++++++++++++++++++++++
 src/Controller/CalendarController.php | 19 ++++++++++++++++
 src/Entity/User.php                   | 28 +++++++++++++++++++----
 3 files changed, 75 insertions(+), 4 deletions(-)
 create mode 100644 migrations/Version20220213005422.php

diff --git a/migrations/Version20220213005422.php b/migrations/Version20220213005422.php
new file mode 100644
index 00000000..331b3851
--- /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 0023f099..2eb4a8c2 100644
--- a/src/Controller/CalendarController.php
+++ b/src/Controller/CalendarController.php
@@ -56,4 +56,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 84635275..a6876947 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);
+    }
 }
-- 
GitLab