LegumeAPIController.php 6.2 KB
Newer Older
Mat's avatar
Mat committed
1
2
3
4
5
<?php

namespace APIBundle\Controller;

use APIBundle\Form\LegumeAPIType;
6
use APIBundle\Form\OffreLegumesInsertAPIType;
Mat's avatar
Mat committed
7
use PotageBundle\Entity\Legume;
8
use PotageBundle\Entity\OffreLegumes;
Mat's avatar
Mat committed
9
10
11
12
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


13
class LegumeAPIController extends MasterAPIController
Mat's avatar
Mat committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{

    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function createAction(Request $request)
    {
        $legume = new Legume();
        $form = $this->createForm(LegumeAPIType::class, $legume);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($legume);
            $em->flush();
            return $this->api($legume);
        }
        return $this->api($form , Response::HTTP_BAD_REQUEST);
    }
34
35


Mat's avatar
Mat committed
36
    /**
37
     * @param Request $request
Mat's avatar
Mat committed
38
39
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
40
    public function readAction(Request $request)
Mat's avatar
Mat committed
41
    {
42

43
44
45
46
47
        // defaults
        $paginate = ($request->query->get('step') !== null) ? $request->query->get('step') : 10;
        $start = ($request->query->get('page') !== null) ? $request->query->get('page') : 1;
        $column = ($request->query->get('column') !== null) ? $request->query->get('column') : 'id';
        $order = ($request->query->get('order') !== null) ? $request->query->get('order') : 'ASC';
48

Mat's avatar
Mat committed
49
        $legumes = $this->getDoctrine()->getManager()
50
51
            ->getRepository('PotageBundle:Legume')
            ->findAllForAPIReadPaginate($paginate, $start, $column, $order);
52

Mat's avatar
Mat committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        return $this->api($legumes);
    }



    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function readOneAction($id)
    {
        $legume = $this->getDoctrine()->getManager()
            ->getRepository('PotageBundle:Legume')->findOneForAPIRead($id);
        if ($legume === null) {
68
            return $this->api($legume, Response::HTTP_NOT_FOUND);
Mat's avatar
Mat committed
69
70
71
        }
        return $this->api($legume);
    }
72
73
    
    
Mat's avatar
Mat committed
74
75
76
77
78
79
80
81
82
83
84
85
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')
            ->findOneForAPIUpdate($id);
        if ($legume === null) {
86
            return $this->api($legume, Response::HTTP_NOT_FOUND);
Mat's avatar
Mat committed
87
88
89
90
91
92
93
94
95
96
97
        }
        $form = $this->createForm(LegumeAPIType::class, $legume);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid())
        {
            $em->persist($legume);
            $em->flush();
            return $this->api($legume);
        }
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
98
99
100



Mat's avatar
Mat committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function deleteAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')
            ->findOneForAPIdelete($id);
        if ($legume !== null)
        {
            $em->remove($legume);
            $em->flush();
        }
        return $this->api([]);
    }

119

120
121
122
123
124
125

    /**
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    public function duplicateAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')
            ->findOneForAPIDuplicate($id);

        if ($legume !== null)
        {
            $newRow = clone $legume;
            $em->detach($newRow);
            $em->persist($newRow);
            $em->flush();
            return $this->api($newRow);
        }
140
        return $this->api([], Response::HTTP_BAD_REQUEST);
141
    }
Mat's avatar
Mat committed
142
    
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    
    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function searchAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $requestString = $request->get('q');
        
        $legumes = $em->getRepository('PotageBundle:Legume')
            ->findRowsByString($requestString);
        
        if ($legumes === null)
        {
            return $this->api($legumes, Response::HTTP_NOT_FOUND);
        }
        else
        {
            return $this->api($legumes);
        }
    }
165
    
166
    
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    /**
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public  function insertAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        
        $legume = $em->getRepository('PotageBundle:Legume')
            ->findOneForAPIRead($id);  // TODO temporaire, réutilise un repository déjà existant
        
        if ($legume === null ) {
            return $this->api($legume, Response::HTTP_NOT_FOUND);
        }
    
        $offrelegumes = new OffreLegumes();
        $offrelegumes
            ->setNom($legume)
            ->setDescription($legume)
            ->setOrigine($legume)
            ->setUnite($legume)
            ->setQuantiteMin($legume)
            ->setQuantiteMax($legume)
            ->setQuantiteStep($legume)
            ->setPrixUnitaire($legume)
            ->setFruit($legume)
            ->setRevente($legume)
            ->setImage($legume);
        
198
       
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        $form = $this->createForm(OffreLegumesInsertAPIType::class, $offrelegumes);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            
            $em->persist($offrelegumes);
            $em->flush();
            return $this->api($offrelegumes);
        }
        
        return $this->api($form, Response::HTTP_BAD_REQUEST);
    }
    
Mat's avatar
Mat committed
213
}
214
215
216
/*
* TODO submit du formulaire, persist et flush
*/