Fix the localisations resssource.
[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
32 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
33 }
34
35 /**
98f85207 36 * @Rest\Delete("/api/person/{id}")
dec6d031
JB
37 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
38 */
39 public function removePersonAction(Request $request)
40 {
41 $em = $this->getDoctrine()->getManager();
282545e5 42 $person = $em->getRepository('App:Person')->find($request->get('id'));
dec6d031 43
51963d7f
JB
44 if (!empty($person)) {
45 $em->remove($person);
dec6d031
JB
46 $em->flush();
47 }
98f85207 48 //TODO: remove localisation and friendship
dec6d031
JB
49 }
50
51 /**
52 * @Rest\Put(
98f85207 53 * path = "/api/person/{id}/update",
dec6d031
JB
54 * name = "update_person"
55 * )
56 * @Rest\View(StatusCode = Response::HTTP_CREATED)
57 */
58 public function updatePersonAction(Request $request)
59 {
60 $em = $this->getDoctrine()->getManager();
282545e5 61 $person = $em->getRepository('App:Person')->find($request->get('id'));
dec6d031 62
51963d7f 63 if (empty($person)) {
d2685734 64 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
dec6d031
JB
65 }
66
67 $person->setFirstName($request->get('firstname'));
68 $person->setLastName($request->get('lastname'));
69 $person->setEmail($request->get('email'));
70
71 $em->merge($person);
72 $em->flush();
73
74 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
75 }
76
77 /**
98f85207 78 * @Rest\Get("/api/person/{id}/friends/localisation")
dec6d031
JB
79 */
80 public function getFriendsLocalisationAction(Request $request)
81 {
82
83 }
84
4c4feb3e
JB
85 /**
86 * @Rest\Get("/api/person/{id}/localisations")
87 */
1de9ba73 88 public function getLocalisationsAction(Request $request)
4c4feb3e
JB
89 {
90 $em = $this->getDoctrine()->getManager();
1de9ba73 91 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
4c4feb3e
JB
92
93 if (empty($localisations)) {
1de9ba73 94 return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
4c4feb3e
JB
95 }
96
1de9ba73 97 $view = View::create($localisations);
4c4feb3e
JB
98 $view->setFormat('json');
99
100 $viewHandler = $this->get('fos_rest.view_handler');
101 return $viewHandler->handle($view);
102
103 }
104
dec6d031 105 /**
98f85207 106 * @Rest\Post("/api/person/{id}/localisation")
43f58db4 107 * @Rest\View(StatusCode = Response::HTTP_CREATED)
dec6d031
JB
108 */
109 public function updateLocalisationAction(Request $request)
110 {
111 $localisation = new Localisation();
112 $localisation->setPerson($request->get('id'));
113 $localisation->setTimestamp($request->get('timestamp'));
114 $localisation->setLatitude($request->get('latitude'));
115 $localisation->setLongitude($request->get('longitude'));
116
117 $em = $this->getDoctrine()->getManager();
118
119 $em->persist($localisation);
120 $em->flush();
121 }
122
123 /**
124 * @Rest\Get(
98f85207 125 * path = "/api/person/{id}",
dec6d031
JB
126 * name = "show_person",
127 * requirements = {"id"="\d+"}
128 * )
5347d06b 129 * @Rest\View()
dec6d031 130 */
51963d7f 131 public function showPerson(Request $request)
dec6d031 132 {
5347d06b
JB
133 $em = $this->getDoctrine()->getManager();
134 $person = $em->getRepository('App:Person')->find($request->get('id'));
51963d7f 135
5347d06b 136 if (empty($person)) {
d2685734 137 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
51963d7f
JB
138 }
139
5347d06b
JB
140 $view = View::create($person);
141 $view->setFormat('json');
142
d68c2073
JB
143 $viewHandler = $this->get('fos_rest.view_handler');
144 return $viewHandler->handle($view);
dec6d031
JB
145 }
146
147 /**
148 * @Rest\Get(
98f85207 149 * path = "/api/person/{email}",
dec6d031
JB
150 * name = "show_person_by_email",
151 * requirements = {"email"="\s+"}
152 * )
43f58db4 153 * @Rest\View()
dec6d031 154 */
51963d7f 155 public function showPersonByEmail(Request $request)
dec6d031 156 {
5347d06b
JB
157 $em = $this->getDoctrine()->getManager();
158 $person = $em->getRepository('App:Person')->find($request->get('email'));
159
160 if (empty($person)) {
d2685734 161 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
5347d06b 162 }
51963d7f 163
5347d06b
JB
164 $view = View::create($person);
165 $view->setFormat('json');
51963d7f 166
d68c2073
JB
167 $viewHandler = $this->get('fos_rest.view_handler');
168 return $viewHandler->handle($view);
dec6d031
JB
169 }
170
171 /**
172 * @Rest\Get(
98f85207 173 * path = "/api/person/{id}/friends",
dec6d031
JB
174 * name = "show_person_friends",
175 * requirements = {"id"="\d+"}
176 * )
51963d7f 177 * @Rest\View()
dec6d031 178 */
51963d7f 179 public function showPersonFriends(Request $request)
dec6d031 180 {
5347d06b
JB
181 $em = $this->getDoctrine()->getManager();
182 $person = $em->getRepository('App:Person')->find($request->get('id'));
183
184 if (empty($person)) {
d2685734 185 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
5347d06b 186 }
51963d7f 187
5347d06b
JB
188 $view = View::create($person->getFriends());
189 $view->setFormat('json');
51963d7f 190
d68c2073
JB
191 $viewHandler = $this->get('fos_rest.view_handler');
192 return $viewHandler->handle($view);
dec6d031
JB
193 }
194
195 /**
196 * @Rest\Get(
98f85207 197 * path = "/api/person/{email}/friends",
dec6d031
JB
198 * name = "show_person_friends_by_email",
199 * requirements = {"email"="\s+"}
200 * )
51963d7f 201 * @Rest\View()
dec6d031 202 */
51963d7f 203 public function showPersonFriendsByEmail(Request $request)
dec6d031 204 {
5347d06b
JB
205 $em = $this->getDoctrine()->getManager();
206 $person = $em->getRepository('App:Person')->find($request->get('email'));
207
208 if (empty($person)) {
d2685734 209 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
5347d06b 210 }
51963d7f 211
5347d06b
JB
212 $view = View::create($person->getFriends());
213 $view->setFormat('json');
51963d7f 214
d68c2073
JB
215 $viewHandler = $this->get('fos_rest.view_handler');
216 return $viewHandler->handle($view);
dec6d031
JB
217 }
218}