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