Commit | Line | Data |
---|---|---|
dec6d031 JB |
1 | <?php |
2 | namespace App\Controller; | |
3 | ||
4 | use App\Entity\Person; | |
1d9d8d51 | 5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
dec6d031 | 6 | use FOS\RestBundle\Controller\Annotations as Rest; |
5347d06b JB |
7 | use FOS\RestBundle\View\ViewHandler; |
8 | use FOS\RestBundle\View\View; | |
dec6d031 JB |
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
10 | use Symfony\Component\HttpFoundation\Request; | |
11 | use Symfony\Component\HttpFoundation\Response; | |
12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
13 | ||
1d9d8d51 | 14 | class PersonController extends Controller |
dec6d031 JB |
15 | { |
16 | /** | |
17 | * @Rest\Post( | |
84fd6c7f | 18 | * path = "/api/person/inscription", |
dec6d031 JB |
19 | * name = "create_person" |
20 | * ) | |
21 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
22 | * @ParamConverter("person", converter="fos_rest.request_body") | |
23 | */ | |
24 | public function createPersonAction(Person $person) | |
25 | { | |
26 | $em = $this->getDoctrine()->getManager(); | |
27 | ||
28 | $em->persist($person); | |
29 | $em->flush(); | |
30 | ||
31 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
32 | } | |
33 | ||
34 | /** | |
98f85207 | 35 | * @Rest\Delete("/api/person/{id}") |
dec6d031 JB |
36 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) |
37 | */ | |
38 | public function removePersonAction(Request $request) | |
39 | { | |
40 | $em = $this->getDoctrine()->getManager(); | |
282545e5 | 41 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
dec6d031 | 42 | |
51963d7f JB |
43 | if (!empty($person)) { |
44 | $em->remove($person); | |
dec6d031 JB |
45 | $em->flush(); |
46 | } | |
98f85207 | 47 | //TODO: remove localisation and friendship |
dec6d031 JB |
48 | } |
49 | ||
50 | /** | |
51 | * @Rest\Put( | |
98f85207 | 52 | * path = "/api/person/{id}/update", |
dec6d031 JB |
53 | * name = "update_person" |
54 | * ) | |
55 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
56 | */ | |
57 | public function updatePersonAction(Request $request) | |
58 | { | |
59 | $em = $this->getDoctrine()->getManager(); | |
282545e5 | 60 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
dec6d031 | 61 | |
51963d7f | 62 | if (empty($person)) { |
1faa29dc | 63 | return $this->PersonNotFound(); |
dec6d031 JB |
64 | } |
65 | ||
66 | $person->setFirstName($request->get('firstname')); | |
67 | $person->setLastName($request->get('lastname')); | |
68 | $person->setEmail($request->get('email')); | |
69 | ||
70 | $em->merge($person); | |
71 | $em->flush(); | |
72 | ||
73 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
74 | } | |
75 | ||
76 | /** | |
84fd6c7f JB |
77 | * @Rest\Get("/api/person/{id}/localisations") |
78 | * @Rest\View() | |
dec6d031 | 79 | */ |
84fd6c7f | 80 | public function getLocalisationsAction(Request $request) |
dec6d031 | 81 | { |
84fd6c7f JB |
82 | $em = $this->getDoctrine()->getManager(); |
83 | $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); | |
dec6d031 | 84 | |
84fd6c7f JB |
85 | if (empty($localisations)) { |
86 | return $this->PersonLocalisationsNotFound(); | |
87 | } | |
88 | ||
89 | return $localisations; | |
dec6d031 JB |
90 | } |
91 | ||
4c4feb3e | 92 | /** |
84fd6c7f JB |
93 | * @Rest\Get("/api/person/{id}/localisation") |
94 | * @Rest\View() | |
4c4feb3e | 95 | */ |
84fd6c7f | 96 | public function getLocalisationAction(Request $request) |
4c4feb3e JB |
97 | { |
98 | $em = $this->getDoctrine()->getManager(); | |
84fd6c7f | 99 | $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]); |
4c4feb3e JB |
100 | |
101 | if (empty($localisations)) { | |
84fd6c7f | 102 | return $this->PersonLocalisationNotFound(); |
4c4feb3e JB |
103 | } |
104 | ||
1faa29dc | 105 | return $localisations; |
4c4feb3e JB |
106 | } |
107 | ||
dec6d031 | 108 | /** |
98f85207 | 109 | * @Rest\Post("/api/person/{id}/localisation") |
43f58db4 | 110 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
111 | */ |
112 | public function updateLocalisationAction(Request $request) | |
113 | { | |
114 | $localisation = new Localisation(); | |
115 | $localisation->setPerson($request->get('id')); | |
116 | $localisation->setTimestamp($request->get('timestamp')); | |
117 | $localisation->setLatitude($request->get('latitude')); | |
118 | $localisation->setLongitude($request->get('longitude')); | |
119 | ||
120 | $em = $this->getDoctrine()->getManager(); | |
121 | ||
122 | $em->persist($localisation); | |
123 | $em->flush(); | |
124 | } | |
125 | ||
126 | /** | |
127 | * @Rest\Get( | |
98f85207 | 128 | * path = "/api/person/{id}", |
dec6d031 JB |
129 | * name = "show_person", |
130 | * requirements = {"id"="\d+"} | |
131 | * ) | |
5347d06b | 132 | * @Rest\View() |
dec6d031 | 133 | */ |
51963d7f | 134 | public function showPerson(Request $request) |
dec6d031 | 135 | { |
5347d06b JB |
136 | $em = $this->getDoctrine()->getManager(); |
137 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
51963d7f | 138 | |
5347d06b | 139 | if (empty($person)) { |
1faa29dc | 140 | return $this->PersonNotFound(); |
51963d7f JB |
141 | } |
142 | ||
1faa29dc | 143 | return $person; |
dec6d031 JB |
144 | } |
145 | ||
146 | /** | |
147 | * @Rest\Get( | |
98f85207 | 148 | * path = "/api/person/{email}", |
dec6d031 JB |
149 | * name = "show_person_by_email", |
150 | * requirements = {"email"="\s+"} | |
151 | * ) | |
43f58db4 | 152 | * @Rest\View() |
dec6d031 | 153 | */ |
51963d7f | 154 | public function showPersonByEmail(Request $request) |
dec6d031 | 155 | { |
5347d06b | 156 | $em = $this->getDoctrine()->getManager(); |
b6077893 JB |
157 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
158 | ||
5347d06b | 159 | if (empty($person)) { |
1faa29dc | 160 | return $this->PersonNotFound(); |
5347d06b | 161 | } |
51963d7f | 162 | |
1faa29dc | 163 | return $person; |
dec6d031 JB |
164 | } |
165 | ||
166 | /** | |
167 | * @Rest\Get( | |
98f85207 | 168 | * path = "/api/person/{id}/friends", |
dec6d031 JB |
169 | * name = "show_person_friends", |
170 | * requirements = {"id"="\d+"} | |
171 | * ) | |
51963d7f | 172 | * @Rest\View() |
dec6d031 | 173 | */ |
51963d7f | 174 | public function showPersonFriends(Request $request) |
dec6d031 | 175 | { |
5347d06b JB |
176 | $em = $this->getDoctrine()->getManager(); |
177 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
178 | ||
179 | if (empty($person)) { | |
1faa29dc | 180 | return $this->PersonNotFound(); |
5347d06b | 181 | } |
51963d7f | 182 | |
1faa29dc | 183 | return $person->getFriends(); |
dec6d031 JB |
184 | } |
185 | ||
186 | /** | |
187 | * @Rest\Get( | |
0a86529e JB |
188 | * path = "/api/persons", |
189 | * name = "show_persons" | |
190 | * ) | |
191 | * @Rest\View() | |
192 | */ | |
193 | public function showPersons(Request $request) | |
194 | { | |
195 | $em = $this->getDoctrine()->getManager(); | |
196 | $persons = $em->getRepository('App:Person')->findAll(); | |
197 | ||
0a86529e | 198 | if (empty($persons)) { |
84fd6c7f | 199 | return $this->PersonsNotFound(); |
0a86529e JB |
200 | } |
201 | ||
0a86529e JB |
202 | return $persons; |
203 | } | |
204 | ||
205 | /** | |
206 | * @Rest\Get( | |
98f85207 | 207 | * path = "/api/person/{email}/friends", |
dec6d031 JB |
208 | * name = "show_person_friends_by_email", |
209 | * requirements = {"email"="\s+"} | |
210 | * ) | |
51963d7f | 211 | * @Rest\View() |
dec6d031 | 212 | */ |
51963d7f | 213 | public function showPersonFriendsByEmail(Request $request) |
dec6d031 | 214 | { |
5347d06b | 215 | $em = $this->getDoctrine()->getManager(); |
b6077893 JB |
216 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
217 | ||
5347d06b | 218 | if (empty($person)) { |
1faa29dc | 219 | return $this->PersonNotFound(); |
5347d06b | 220 | } |
51963d7f | 221 | |
1faa29dc | 222 | return $person->getFriends(); |
dec6d031 | 223 | } |
b6077893 | 224 | |
1faa29dc JB |
225 | private function PersonNotFound() { |
226 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
b6077893 JB |
227 | } |
228 | ||
84fd6c7f JB |
229 | private function PersonsNotFound() { |
230 | return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND); | |
231 | } | |
232 | ||
233 | private function PersonLocalisationNotFound() { | |
234 | return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND); | |
235 | } | |
236 | ||
1faa29dc JB |
237 | private function PersonLocalisationsNotFound() { |
238 | return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); | |
b6077893 JB |
239 | } |
240 | ||
dec6d031 | 241 | } |