From 2bc674770cd0bc05d3589620c4a7a15e5ff3004f Mon Sep 17 00:00:00 2001
From: Moreau Elise <moreau.elise13@gmail.com>
Date: Sat, 31 Oct 2020 11:18:50 +0100
Subject: [PATCH] add controler to get near contaminated users

---
 .../Actions/User/NearContaminatedUsers.php    | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 src/Application/Actions/User/NearContaminatedUsers.php

diff --git a/src/Application/Actions/User/NearContaminatedUsers.php b/src/Application/Actions/User/NearContaminatedUsers.php
new file mode 100644
index 0000000..1780bab
--- /dev/null
+++ b/src/Application/Actions/User/NearContaminatedUsers.php
@@ -0,0 +1,68 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Application\Actions\User;
+use Psr\Http\Message\ResponseInterface as Response;
+use Psr\Http\Message\ServerRequestInterface as Request;
+use App\Domain\User\UserNotFoundException;
+
+use Doctrine\ORM\EntityManager;
+use Psr\Log\LoggerInterface;
+use Slim\Views\Twig;
+use Slim\Flash\Messages;
+
+/**
+ * Class ListUsersAction
+ *
+ * @author : moreau96u
+ */
+class NearContaminatedUsers extends UserAction
+{
+    public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash){
+
+        parent::__construct($logger, $em, $twig, $flash);
+        $this->em = $em;
+        $this->userRepository = $em->getRepository('App\Domain\User\User');
+        $this->groupRepository = $em->getRepository('App\Domain\Group\Group');
+        $this->locationRepository = $em->getRepository('App\Domain\Location\Location');
+    }
+
+    protected function action(): Response
+    {
+        if(!isset($_SESSION['userId'])){
+            $this->flash->addMessage('login', 'Please log in or sign up.');
+
+            return $this->response
+            ->withHeader('Location', '/login')
+            ->withStatus(302);
+        }
+
+        $currentUser = $this->userRepository->find($_SESSION['userId']);
+
+        $location = $this->locationRepository->find($currentUser->getLocationId());
+        $userLatitude = $location->getLatitude();
+        $userLongitude = $location->getLongitude();
+
+        $latitude_min = $userLatitude - 1 ;
+        $latitude_max = $userLatitude + 1 ;
+        $longitude_min = $userLongitude - 1 ;
+        $longitude_max = $userLongitude + 1 ;
+
+        $qb = $this->em->createQueryBuilder()
+        ->select('u')
+        ->from('App\Domain\User\User', 'u')
+        ->where('u.contaminated = 1')
+        ->andWhere('u.location.latitude  >= :latitude_min')
+        ->andWhere('u.location.latitude  <= :latitude_max')
+        ->andWhere('u.location.longitude  >= :longitude_min')
+        ->andWhere('u.location.longitude  <= :longitude_max')
+        ->setParameter('latitude_min', $latitude_min)
+        ->setParameter('latitude_max', $latitude_max)
+        ->setParameter('longitude_min', $longitude_min)
+        ->setParameter('longitude_max', $longitude_max)
+        ->getQuery()
+        ->getResult();
+
+        return $this->respondWithData($qb);
+    }
+}
-- 
GitLab