OffreAPIController.php 5.76 KB
Newer Older
1 2 3 4 5 6
<?php

namespace APIBundle\Controller;


use APIBundle\Form\OffreAPIType;
7
use APIBundle\Form\OffreSortedAPIType;
8
use APIBundle\Form\OffreStatusAPIType;
9
use APIBundle\Form\OffreUpdateAPIType;
10 11 12 13 14 15
use PotageBundle\Entity\Offre;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class OffreAPIController extends MasterAPIController
{
16
    
17 18 19 20 21 22 23 24
    /**
     * @return string
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    private function newReference()
    {
        $now = new \DateTime();

25 26
        $today = $now->format('ymd');
        
27
        $em = $this->getDoctrine()->getManager();
28
        $num = $em->getRepository('PotageBundle:Offre')->findOneLastReference($today);
29
        
30 31 32 33 34 35 36 37 38 39
        if ($num !== null)
        {
            $last = intval(substr($num['reference'], -2));
            $last++;
            $last = sprintf("%02d", $last);
        }
        else {
            $last = '01';
        }
        return 'OF' . $today . $last;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    }
    
    
    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    function createAction(Request $request)
    {
        $reference = $this->newReference();
        
        $offre = new Offre($reference);
        $form = $this->createForm(OffreAPIType::class, $offre);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($offre);
60
            $em->flush();
61
            return $this->api($offre, 200, array('readOffres'));
62 63 64 65 66 67
        }
        return $this->api($form , Response::HTTP_BAD_REQUEST);
    }
    
    
    /**
68
     * @param $status
69 70
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
71
    public function readByStatusAction($status)
72 73
    {
        $em = $this->getDoctrine()->getManager();
Mat's avatar
Mat committed
74
        $offres = $em->getRepository('PotageBundle:Offre')->findAllByStatusForRead($status);
Mat's avatar
Mat committed
75
        return $this->api($offres, 200, array('readOffres'));
76 77
        
    }
78 79
    
    
80 81 82 83 84 85 86 87 88 89 90 91
    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function readOneAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $offre = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id);
        if ($offre === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
92
        return $this->api($offre, 200, array('readOffres'));
93 94 95
    }
    
    
96 97 98 99 100 101
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
102
    public function updateAction(Request $request, $id)
103 104
    {
        $em = $this->getDoctrine()->getManager();
Mat's avatar
Mat committed
105
        $offre = $em->getRepository('PotageBundle:Offre')->findOneForUpdate($id);
106
        
107
        $form = $this->createForm(OffreUpdateAPIType::class, $offre);
108 109 110 111 112 113 114 115 116
        $form->handleRequest($request);
        
        if ($offre === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em->flush();
117
            return $this->api($offre, 200, array('readOffres'));
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
    
    
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function updateStatusAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        $offre = $em->getRepository('PotageBundle:Offre')->findOneForUpdateStatus($id);
133 134 135 136 137 138 139 140 141 142 143
     
        $form = $this->createForm(OffreStatusAPIType::class, $offre);
        $form->handleRequest($request);
        
        if ($offre === null) {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em->flush();
144
            return $this->api($offre, 200, array('readOffres'));
145 146 147 148
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
    
149
    
150
    /**
Mat's avatar
Mat committed
151 152
     * Form update (post) pour modifier la colonne 'sorted'
     *
153 154 155 156 157
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
Mat's avatar
Mat committed
158
    public function updateSortedAction(Request $request, $id)
159 160
    {
        $em = $this->getDoctrine()->getManager();
161
        $offre = $em->getRepository('PotageBundle:Offre')->findOneForSortedWhereStatus($id);
162 163 164 165 166 167 168 169 170 171 172 173
        
        if ($offre === null)
        {
            return $this->api('Not found', Response::HTTP_NOT_FOUND);
        }
        
        $form = $this->createForm(OffreSortedAPIType::class, $offre);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $em->flush();
174
            return $this->api([]);
175 176 177 178 179
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
    
    
180 181 182 183 184 185 186 187
    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function deleteAction($id)
    {
        $em = $this->getDoctrine()->getManager();
Mat's avatar
Mat committed
188
        $offre = $em->getRepository('PotageBundle:Offre')->findOneForDelete($id);
Mat's avatar
Mat committed
189
        // TODO enlever une offre, mais aussi tous les légumes qu'elle contient !!
190 191 192 193 194 195 196 197
        
        if ($offre !== null)
        {
            $em->remove($offre);
            $em->flush();
        }
        return $this->api([]);
    }
198
}