Fix the user creation ressource.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
1 <?php
2 namespace App\Controller;
3
4 use App\Entity\Person;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use FOS\RestBundle\Controller\FOSRestController;
7 use FOS\RestBundle\Controller\Annotations as Rest;
8 use FOS\RestBundle\View\ViewHandler;
9 use FOS\RestBundle\View\View;
10 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpFoundation\Response;
13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15 class PersonController extends FOSRestController
16 {
17 /**
18 * @Rest\Post(
19 * path = "/api/person/register",
20 * name = "create_person"
21 * )
22 * @Rest\View(StatusCode = Response::HTTP_CREATED)
23 */
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
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 /**
41 * @Rest\Delete("/api/person/{id}")
42 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
43 */
44 public function removePersonAction(Request $request)
45 {
46 $em = $this->getDoctrine()->getManager();
47 $person = $em->getRepository('App:Person')->find($request->get('id'));
48
49 if (!empty($person)) {
50 $em->remove($person);
51 $em->flush();
52 }
53 //TODO: remove localisation and friendship
54 }
55
56 /**
57 * @Rest\Put(
58 * path = "/api/person/{id}/update",
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();
66 $person = $em->getRepository('App:Person')->find($request->get('id'));
67
68 if (empty($person)) {
69 return $this->PersonNotFound();
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 /**
83 * @Rest\Get("/api/person/{id}/localisations")
84 * @Rest\View()
85 */
86 public function getLocalisationsAction(Request $request)
87 {
88 //TODO: Check that the authenticated user is allowed to see the localisation
89 $em = $this->getDoctrine()->getManager();
90 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
91
92 if (empty($localisations)) {
93 return $this->PersonLocalisationsNotFound();
94 }
95
96 return $localisations;
97 }
98
99 /**
100 * @Rest\Get("/api/person/{id}/localisation")
101 * @Rest\View()
102 */
103 public function getLocalisationAction(Request $request)
104 {
105 //TODO: Check that the authenticated user is allowed to see the localisation
106 $em = $this->getDoctrine()->getManager();
107 $localisation = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
108
109 if (empty($localisation)) {
110 return $this->PersonLocalisationNotFound();
111 }
112
113 return $localisation;
114 }
115
116 /**
117 * @Rest\Post("/api/person/{id}/localisation")
118 * @Rest\View(StatusCode = Response::HTTP_CREATED)
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 /**
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 {
144 $em = $this->getDoctrine()->getManager();
145 $person = $em->getRepository('App:Person')->find($request->get('id'));
146
147 if (empty($person)) {
148 return $this->PersonNotFound();
149 }
150
151 return $person;
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 {
164 $em = $this->getDoctrine()->getManager();
165 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
166
167 if (empty($person)) {
168 return $this->PersonNotFound();
169 }
170
171 return $person;
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 {
184 $em = $this->getDoctrine()->getManager();
185 $person = $em->getRepository('App:Person')->find($request->get('id'));
186
187 if (empty($person)) {
188 return $this->PersonNotFound();
189 }
190
191 return $person->getFriends();
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)) {
207 return $this->PersonsNotFound();
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 {
223 $em = $this->getDoctrine()->getManager();
224 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
225
226 if (empty($person)) {
227 return $this->PersonNotFound();
228 }
229
230 return $person->getFriends();
231 }
232
233 private function PersonNotFound() {
234 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
235 }
236
237 private function PersonsNotFound() {
238 return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND);
239 }
240
241 private function PersonLocalisationNotFound() {
242 return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND);
243 }
244
245 private function PersonLocalisationsNotFound() {
246 return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
247 }
248
249 }