src/Controller/Api/ObjectivesController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Controller\Response;
  4. use App\Entity\Objectives;
  5. use App\Services\Objectives\ObjectiveSetter;
  6. use App\Services\Objectives\Validator;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * @Route("/api/objective", name="api_objective_")
  13.  */
  14. class ObjectivesController extends AbstractController
  15. {
  16.     use Response;
  17.     use \App\Controller\Request;
  18.     /**
  19.      * @Route("/set", name="set", methods={"POST"})
  20.      */
  21.     public function setterAction(Request $requestValidator $validatorObjectiveSetter $objectiveSetter)
  22.     {
  23.         try {
  24.             $user $this->getUser(); // TOKEN
  25.             # get Request json
  26.             $content $request->getContent();
  27.             # parse JSON to Array
  28.             $data \json_decode($contenttrue);
  29.             # validate data
  30.             $validator->validate($data);
  31.             if ($validator->isValid()) {
  32.                 if ($data['weight'] < $data['objective_weight']) {
  33.                     throw new \Exception('Weight must be greater than objective weight');
  34.                 }
  35.                 $data['user_id'] = $user->getId(); // TOKEN
  36.                 # set objective
  37.                 $objectiveSetter->setData($data)->saveObjective();
  38.             }
  39.             return $this->jsonResponse();
  40.         } catch (\Exception $e) {
  41.             return $this->errorJsonResponse(['message' => $e->getMessage()]);
  42.         }
  43.     }
  44.     /**
  45.      * @Route("/get/{userid}", name="get", methods={"GET"})
  46.      */
  47.     public function getterAction($useridEntityManagerInterface $em)
  48.     {
  49.         try {
  50.             $user $this->getUser(); // TOKEN
  51.             $objective $em->getRepository(Objectives::class)->findOneByUser($user->getId()); // TOKEN
  52.             //$objective = $em->getRepository(Objectives::class)->findOneByUser($userid);
  53.             if ($objective) {
  54.                 $addWeeks $objective['weight'] - $objective['objectiveWeight'];
  55.                 $endObjective = (clone $objective['createdAt'])->add(new \DateInterval('P' $addWeeks 'W'));
  56.                 $days $endObjective->diff(new \DateTime())->format('%a');
  57.                 $objective['remainingDays'] = intval($days);
  58.                 $objective['remainingWeeks'] = ceil($days 7);
  59.             }
  60.             return $this->jsonResponse(
  61.                 [
  62.                     'data' => $objective,
  63.                 ]
  64.             );
  65.         } catch (\Exception $e) {
  66.             return $this->errorJsonResponse(['message' => $e->getMessage()]);
  67.         }
  68.     }
  69.     /**
  70.      * @Route("/cancel/{userid}", name="cancel", methods={"POST"})
  71.      */
  72.     public function cancelAction($useridEntityManagerInterface $em)
  73.     {
  74.         try {
  75.             $user $this->getUser();   // TOKEN
  76.             $objective $em->getRepository(Objectives::class)->findOneBy([
  77.                 'userId' => $user->getId(),   // TOKEN
  78.                 //'userId' => $userid,
  79.             ]);
  80.             if ($objective) {
  81.                 $objective->setCancelledAt(new \DateTime());
  82.                 $em->persist($objective);
  83.                 $em->flush();
  84.             }
  85.             return $this->jsonResponse();
  86.         } catch (\Exception $e) {
  87.             return $this->errorJsonResponse(['message' => $e->getMessage()]);
  88.         }
  89.     }
  90. }