Advertisement
SanderCokart

TodoController.php

Feb 2nd, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.81 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\Todo;
  6. use App\Repository\TodoRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13.  
  14. /**
  15.  * @Route("/api/todo", name="api_todo")
  16.  */
  17. class TodoController extends AbstractController
  18. {
  19.     private $entityManager;
  20.     private $todoRepository;
  21.  
  22.     public function __construct(EntityManagerInterface $entityManager, TodoRepository $todoRepository)
  23.     {
  24.         $this->entityManager = $entityManager;
  25.         $this->todoRepository = $todoRepository;
  26.     }
  27.  
  28.     /**
  29.      * @Route("/create", name="api_todo_create", methods={"POST"})
  30.      * @param Request $request
  31.      * @return JsonResponse
  32.      */
  33.     public function create(Request $request)
  34.     {
  35.         $content = json_decode($request->getContent());
  36.  
  37.         $todo = new Todo();
  38.  
  39.         $todo->setName($content->name);
  40.         $todo->setDescription($content->description);
  41.  
  42.         try {
  43.             $this->entityManager->persist($todo);
  44.             $this->entityManager->flush();
  45.         } catch (Exception $exception) {
  46.             return $this->json([
  47.                 'message' => ['text' => 'Could not reach database when attempting to create a To-Do.', 'level' => 'error']
  48.             ]);
  49.  
  50.         }
  51.         return $this->json([
  52.             'todo'    => $todo->toArray(),
  53.             'message' => ['text' => 'To-Do has been created!', 'level' => 'success']
  54.         ]);
  55.     }
  56.  
  57.  
  58.     /**
  59.      * @Route("/read", name="api_todo_read", methods={"GET"})
  60.      */
  61.     public function read()
  62.     {
  63.         $todos = $this->todoRepository->findAll();
  64.  
  65.         $arrayOfTodos = [];
  66.         foreach ($todos as $todo) {
  67.             $arrayOfTodos[] = $todo->toArray();
  68.         }
  69.         return $this->json($arrayOfTodos);
  70.  
  71.     }
  72.  
  73.     /**
  74.      * @Route("/update/{id}", name="api_todo_update", methods={"PUT"})
  75.      * @param Request $request
  76.      * @param Todo $todo
  77.      * @return JsonResponse
  78.      */
  79.     public function update(Request $request, Todo $todo)
  80.     {
  81.         $content = json_decode($request->getContent());
  82.  
  83.  
  84.         if ($todo->getName() === $content->name && $todo->getDescription() === $content->description) {
  85.             return $this->json([
  86.                 'message' => ['text' => 'There was no change to the To-Do. Neither the name or the description was changed.', 'level' => 'error']
  87.             ]);
  88.         }
  89.  
  90.         $todo->setName($content->name);
  91.         $todo->setDescription($content->description);
  92.  
  93.         try {
  94.             $this->entityManager->flush();
  95.         } catch (Exception $exception) {
  96.             return $this->json([
  97.                 'message' => ['text' => 'Could not reach database when attempting to update a To-Do.', 'level' => 'error']
  98.             ]);
  99.         }
  100.  
  101.         return $this->json([
  102.             'todo'    => $todo->toArray(),
  103.             'message' => ['text' => 'To-Do successfully updated!', 'level' => 'success']
  104.         ]);
  105.  
  106.     }
  107.  
  108.  
  109.     /**
  110.      * @Route("/delete/{id}", name="api_todo_delete", methods={"DELETE"})
  111.      * @param Todo $todo
  112.      * @return JsonResponse
  113.      */
  114.     public function delete(Todo $todo)
  115.     {
  116.         try {
  117.             $this->entityManager->remove($todo);
  118.             $this->entityManager->flush();
  119.         } catch (Exception $exception) {
  120.             return $this->json([
  121.                 'message' => ['text' => 'Could not reach database when attempting to delete a To-Do.', 'level' => 'error']
  122.             ]);
  123.  
  124.         }
  125.  
  126.         return $this->json([
  127.             'message' => ['text' => 'To-Do has successfully been deleted!', 'level' => 'success']
  128.         ]);
  129.  
  130.     }
  131.  
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement