InfoAPIController.php 4.22 KB
Newer Older
1 2 3 4
<?php

namespace APIBundle\Controller;

5
use APIBundle\Form\InfoAPIType;
6
use APIBundle\Form\InfoInsertAPIType;
7
use PotageBundle\Entity\Info;
8
use PotageBundle\Entity\Lettre;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11

12 13
class InfoAPIController extends MasterAPIController
{
14 15 16 17 18
    
    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
19 20
    public function createAction(Request $request)
    {
21 22 23 24 25 26 27 28 29 30 31 32 33 34
        $info = new Info();
        
        $form = $this->createForm(InfoAPIType::class, $info);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($info);
            $em->flush();
            
            return $this->api($info);
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
35 36 37 38
    }
    
    
    /**
39
     * @param $type
40 41
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
42
    public function readByTypeAction($type)
43
    {
44
        $isPost = $type === 'actu' ? 1 : 0;
45
        $em = $this->getDoctrine()->getManager();
46
        $infos = $em->getRepository('PotageBundle:Info')->findAllByTypeForAPIRead($isPost);
47 48
        return $this->api($infos);
    }
49
    
50
    
Mat's avatar
Mat committed
51 52 53 54 55
    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
56 57
    public function readOneAction($id)
    {
Mat's avatar
Mat committed
58 59 60 61 62 63
        $em = $this->getDoctrine()->getManager();
        $info = $em->getRepository('PotageBundle:Info')->findOneForAPIRead($id);
        if ($info === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        return $this->api($info);
64
    }
Mat's avatar
Mat committed
65 66 67 68 69 70 71 72
    
    
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
73 74
    public function updateAction(Request $request, $id)
    {
Mat's avatar
Mat committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        $em = $this->getDoctrine()->getManager();
        $info = $em->getRepository('PotageBundle:Info')->findOneForAPIUpdate($id);
        
        if ($info === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        
        $form = $this->createForm(InfoAPIType::class, $info);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em->flush();
            return $this->api($info);
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
91
    }
Mat's avatar
Mat committed
92 93 94 95 96 97 98
    
    
    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
99 100
    public function deleteAction($id)
    {
Mat's avatar
Mat committed
101 102 103 104 105 106 107 108 109
        $em = $this->getDoctrine()->getManager();
        $info = $em->getRepository('PotageBundle:Info')->findOneForAPIDelete($id);
        
        if ($info !== null)
        {
            $em->remove($info);
            $em->flush();
        }
        return $this->api([]);
110
    }
111 112 113 114 115 116 117 118 119 120 121 122
    
    
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function insertAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        $info = $em->getRepository('PotageBundle:Info')->findOneForAPIInsert($id);
123
        
124 125 126 127 128 129
        if ($info === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        
        $form = $this->createForm(InfoInsertAPIType::class, $info);
        $form->handleRequest($request);
130
    
131 132 133
        
        if($form->isSubmitted() && $form->isValid())
        {
134 135 136 137 138 139 140 141 142
            
            // TODO ceci est temporaire, avant de comprendre pq il ne passe pas dans la méthode addLettre() de l'entité Info
            foreach ($info->getLettres() as $lettre){
                /**
                 * @var $lettre Lettre
                 */
                $lettre->addInfo($info);
            }
            
143 144 145 146 147
            $em->flush();
            return $this->api($info);
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
148
}