src/Controller/ArticleController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Article;
  4. use App\Repository\ArticleRepository;
  5. use App\Repository\CategoryRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. class ArticleController extends AbstractController
  12. {
  13.     private $articleRepository;
  14.     private $categoryRepository;
  15.     public  function __construct(ArticleRepository $articleRepositoryCategoryRepository $categoryRepository)
  16.     {
  17.         $this->categoryRepository $categoryRepository;
  18.         $this->articleRepository $articleRepository;
  19.     }
  20.     /**
  21.      * 
  22.      * @Route("/", name="app_home")
  23.      * 
  24.      */
  25.     public function index(): Response
  26.     {
  27.         $articles $this->articleRepository->findAll();
  28.         $categories $this->categoryRepository->findAll();
  29.         return $this->render('article/index.html.twig', [
  30.             'articles' => $articles,
  31.             'categories' => $categories
  32.         ]);
  33.     }
  34.     /**
  35.      * 
  36.      * @Route("/list", name="app_list")
  37.      * 
  38.      */
  39.     public function list(): Response
  40.     {
  41.         $articles $this->articleRepository->listachat();
  42.         return $this->render('article/list.html.twig', [
  43.             'articles' => $articles
  44.         ]);
  45.     }
  46.     /**
  47.      * 
  48.      * @Route("/{id}", name="app_detail")
  49.      * 
  50.      */
  51.     public function detail($id): Response
  52.     {
  53.         $article $this->articleRepository->findOneBy(['id' => $id]);
  54.         $evolution $this->articleRepository->evolution($id);
  55.         return $this->render('article/detail.html.twig', [
  56.             'article' => $article,
  57.             'evolution' => $evolution
  58.         ]);
  59.     }
  60.     /**
  61.      * A function to add an article
  62.      * 
  63.      * @Route("/article/{id}/minus", name="article_minus");
  64.      *
  65.      * @param Article $article
  66.      * @param EntityManagerInterface $manager
  67.      * @param ArticleRepository $repo
  68.      * @return Response
  69.      */
  70.     public function minus(Article $articleEntityManagerInterface $managerArticleRepository $repo)
  71.     {
  72.         //$article = $this->getArticle();
  73.         if (!$article) {
  74.             return $this->json([
  75.                 'code' => 403,
  76.                 'message' => "Article doesn't exist"
  77.             ], 403);
  78.         } else {
  79.             $quantity $article->getQuantity() - 1;
  80.             $article->setQuantity($quantity);
  81.             $manager->persist($article);
  82.             $manager->flush();
  83.             return $this->json([
  84.                 'code' => 200,
  85.                 'message' => "Article founded",
  86.                 'id' => $article->getId(),
  87.                 'name' => $article->getName(),
  88.                 'quantity' => $article->getQuantity()
  89.             ], 200);
  90.         }
  91.     }
  92.     /**
  93.      * A function to add an article
  94.      * 
  95.      * @Route("/article/{id}/add", name="article_add");
  96.      *
  97.      * @param Article $article
  98.      * @param EntityManagerInterface $manager
  99.      * @param ArticleRepository $repo
  100.      * @return Response
  101.      */
  102.     public function add(Article $articleEntityManagerInterface $managerArticleRepository $repo)
  103.     {
  104.         //$article = $this->getArticle();
  105.         if (!$article) {
  106.             return $this->json([
  107.                 'code' => 403,
  108.                 'message' => "Article doesn't exist"
  109.             ], 403);
  110.         } else {
  111.             $quantity $article->getQuantity() + 1;
  112.             $article->setQuantity($quantity);
  113.             $manager->persist($article);
  114.             $manager->flush();
  115.             return $this->json([
  116.                 'code' => 200,
  117.                 'message' => "Article found",
  118.                 'id' => $article->getId(),
  119.                 'name' => $article->getName(),
  120.                 'quantity' => $article->getQuantity()
  121.             ], 200);
  122.         }
  123.     }
  124. }