src/Controller/FAQController.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Administrateur;
  4. use App\Entity\Categorie;
  5. use App\Entity\FAQ;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class FAQController extends AbstractController
  12. {
  13.     // /**
  14.     //  * @Route("/faq/{id}", name="deleteFAQByID", methods={"DELETE"})
  15.     //  */
  16.     // public function deleteFAQByID(FAQ $faq)
  17.     // {
  18.     //     $entityManager = $this->getDoctrine()->getManager();
  19.     //     $entityManager->remove($faq);
  20.     //     $entityManager->flush();
  21.     //     return new Response($faq->getId() . " has been successfully deleted");
  22.     // }
  23.     // /**
  24.     //  * @Route("/faq", name="postFAQ", methods={"POST"})
  25.     //  */
  26.     // public function postFAQ(Request $request): Response
  27.     // {
  28.     //     $data = json_decode($request->getContent(), true);
  29.     //     $faq = new FAQ();
  30.     //     $admin = $this->getDoctrine()
  31.     //         ->getRepository(Administrateur::class)
  32.     //         ->findOneBy(['id' => $data["admin_id"]]);
  33.     //     if ($admin == null) {
  34.     //         return new Response(json_encode([
  35.     //             'message' => 'Error, no admin found at this id',
  36.     //             'path' => 'src/Controller/FAQController.php',
  37.     //         ]));
  38.     //     }
  39.     //     $category = $this->getDoctrine()
  40.     //         ->getRepository(Categorie::class)
  41.     //         ->findOneBy(['id' => $data["category_id"]]);
  42.     //     if ($category == null) {
  43.     //         return new Response(json_encode([
  44.     //             'message' => 'Error, no category found at this id',
  45.     //             'path' => 'src/Controller/FAQController.php',
  46.     //         ]));
  47.     //     }
  48.     //     $faq->setIdAdmin($admin);
  49.     //     $faq->setIdCategorie($category);
  50.     //     $faq->setQuestion($data["question"]);
  51.     //     $faq->setAnswer($data["answer"]);
  52.     //     $faq->setIsPro($data["is_pro"]);
  53.     //     $faq->setDate(new \DateTime());
  54.     //     $entityManager = $this->getDoctrine()->getManager();
  55.     //     $entityManager->persist($faq);
  56.     //     $entityManager->flush();
  57.     //     return new Response(json_encode([
  58.     //         "question" => $faq->getQuestion(),
  59.     //         "answer" => $faq->getAnswer(),
  60.     //         "admin_id" => $faq->getIdAdmin()->getId(),
  61.     //         "category_id" => $faq->getIdCategorie()->getId(),
  62.     //         "is_pro" => $faq->getIsPro(),
  63.     //     ]));
  64.     // }
  65.     /**
  66.      * @Route("/faqs", name="getFAQs", methods={"GET","HEAD"})
  67.      */
  68.     public function getFAQs()
  69.     {
  70.         $faqs $this->getDoctrine()
  71.             ->getRepository(FAQ::class)
  72.             ->findBy(["isPro" => false]);
  73.         $result = new ArrayCollection();
  74.         foreach ($faqs as $faq) {
  75.             $result->add([
  76.                 "id" => $faq->getId(),
  77.                 "question" => $faq->getQuestion(),
  78.                 "answer" => $faq->getAnswer(),
  79.                 "admin_id" => $faq->getIdAdmin()->getId(),
  80.                 "category_id" => $faq->getIdCategorie()->getId(),
  81.                 "is_pro" => $faq->getIsPro(),
  82.             ]);
  83.         }
  84.         return new Response(json_encode([
  85.             "faqs" => $result->toArray()
  86.         ]));
  87.     }
  88.     /**
  89.      * @Route("/faqs/pro", name="getFAQsPro", methods={"GET","HEAD"})
  90.      */
  91.     public function getFAQsPro()
  92.     {
  93.         $faqs $this->getDoctrine()
  94.             ->getRepository(FAQ::class)
  95.             ->findBy(["isPro" => true]);
  96.         $result = new ArrayCollection();
  97.         foreach ($faqs as $faq) {
  98.             $result->add([
  99.                 "id" => $faq->getId(),
  100.                 "question" => $faq->getQuestion(),
  101.                 "answer" => $faq->getAnswer(),
  102.                 "admin_id" => $faq->getIdAdmin()->getId(),
  103.                 "category_id" => $faq->getIdCategorie()->getId(),
  104.                 "is_pro" => $faq->getIsPro(),
  105.             ]);
  106.         }
  107.         return new Response(json_encode([
  108.             "faqs" => $result->toArray()
  109.         ]));
  110.     }
  111.     /**
  112.      * @Route("/faq/{id}", name="getFAQ", methods={"GET","HEAD"})
  113.      */
  114.     public function getFAQ(FAQ $faq): Response
  115.     {
  116.         return new Response(json_encode([
  117.             "question" => $faq->getQuestion(),
  118.             "answer" => $faq->getAnswer(),
  119.             "admin_id" => $faq->getIdAdmin()->getId(),
  120.             "category_id" => $faq->getIdCategorie()->getId(),
  121.             "is_pro" => $faq->getIsPro(),
  122.         ]));
  123.     }
  124.     // /**
  125.     //  * @Route("/faq/{id}", name="editFAQByID", methods={"PUT"})
  126.     //  */
  127.     // public function editFAQByID(FAQ $faq, Request $request)
  128.     // {
  129.     //     $entityManager = $this->getDoctrine()->getManager();
  130.     //     $data = json_decode($request->getContent(), true);
  131.     //     if (isset($data["admin_id"])) {
  132.     //         $admin = $this->getDoctrine()
  133.     //             ->getRepository(Administrateur::class)
  134.     //             ->findOneBy(['id' => $data["admin_id"]]);
  135.     //         if ($admin == null) {
  136.     //             return new Response(json_encode([
  137.     //                 'message' => 'Error, no Admin found at this id',
  138.     //                 'path' => 'src/Controller/FAQController.php',
  139.     //             ]));
  140.     //         }
  141.     //         $faq->setIdAdmin($admin);
  142.     //     }
  143.     //     if (isset($data["categorie_id"])) {
  144.     //         $categorie = $this->getDoctrine()
  145.     //             ->getRepository(Categorie::class)
  146.     //             ->findOneBy(['id' => $data["categorie_id"]]);
  147.     //         if ($categorie == null) {
  148.     //             return new Response(json_encode([
  149.     //                 'message' => 'Error, no Category found at this id',
  150.     //                 'path' => 'src/Controller/FAQController.php',
  151.     //             ]));
  152.     //         }
  153.     //         $faq->setIdCategorie($categorie);
  154.     //     }
  155.     //     if (isset($data["question"]))
  156.     //         $faq->setQuestion($data["question"]);
  157.     //     if (isset($data["answer"]))
  158.     //         $faq->setAnswer($data["answer"]);
  159.     //     if (isset($data["is_pro"]))
  160.     //         $faq->setIsPro($data["is_pro"]);
  161.     //     $entityManager->flush();
  162.     //     return new Response(json_encode([
  163.     //         "question" => $faq->getQuestion(),
  164.     //         "answer" => $faq->getAnswer(),
  165.     //         "admin_id" => $faq->getIdAdmin()->getId(),
  166.     //         "category_id" => $faq->getIdCategorie()->getId(),
  167.     //         "is_pro" => $faq->getIsPro(),
  168.     //     ]));
  169.     // }
  170. }