Advertisement
SanderCokart

TodoController.php

Jan 23rd, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 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.  
  41.         try {
  42.             $this->entityManager->persist($todo);
  43.             $this->entityManager->flush();
  44.         } catch (Exception $exception) {
  45.             return $this->json([
  46.                 'message' => ['text' => 'Could not reach database when attempting to create a To-Do.', 'level' => 'error']
  47.             ]);
  48.  
  49.         }
  50.         return $this->json([
  51.             'todo'    => $todo->toArray(),
  52.             'message' => ['text' => 'To-Do has been created!', 'level' => 'success']
  53.         ]);
  54.     }
  55.  
  56.  
  57.     /**
  58.      * @Route("/read", name="api_todo_read", methods={"GET"})
  59.      */
  60.     public function read()
  61.     {
  62.         $todos = $this->todoRepository->findAll();
  63.  
  64.         $arrayOfTodos = [];
  65.         foreach ($todos as $todo) {
  66.             $arrayOfTodos[] = $todo->toArray();
  67.         }
  68.         return $this->json($arrayOfTodos);
  69.  
  70.     }
  71.  
  72.     /**
  73.      * @Route("/update/{id}", name="api_todo_update", methods={"PUT"})
  74.      * @param Request $request
  75.      * @param Todo $todo
  76.      * @return JsonResponse
  77.      */
  78.     public function update(Request $request, Todo $todo)
  79.     {
  80.         $content = json_decode($request->getContent());
  81.  
  82.         $todo->setName($content->name);
  83.  
  84.         try {
  85.             $this->entityManager->flush();
  86.         } catch (Exception $exception) {
  87.             return $this->json([
  88.                 'message' => ['text' => 'Could not reach database when attempting to update a To-Do.', 'level' => 'error']
  89.             ]);
  90.            
  91.         }
  92.  
  93.         return $this->json([
  94.             'message' => ['text' => 'To-Do successfully updated!', 'level' => 'success']
  95.         ]);
  96.  
  97.     }
  98.  
  99.  
  100.     /**
  101.      * @Route("/delete/{id}", name="api_todo_delete", methods={"DELETE"})
  102.      * @param Todo $todo
  103.      * @return JsonResponse
  104.      */
  105.     public function delete(Todo $todo)
  106.     {
  107.         try {
  108.             $this->entityManager->remove($todo);
  109.             $this->entityManager->flush();
  110.         } catch (Exception $exception) {
  111.             return $this->json([
  112.                 'message' => ['text' => 'Could not reach database when attempting to delete a To-Do.', 'level' => 'error']
  113.             ]);
  114.  
  115.         }
  116.  
  117.         return $this->json([
  118.             'message' => ['text' => 'To-Do has successfully been deleted!', 'level' => 'success']
  119.         ]);
  120.  
  121.     }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement