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