Only expose relevant attributes.
[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\Annotations as Rest;
7 use FOS\RestBundle\View\ViewHandler;
8 use FOS\RestBundle\View\View;
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
14 class PersonController extends Controller
15 {
16 /**
17 * @Rest\Post(
18 * path = "/api/person/inscription",
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 /**
35 * @Rest\Delete("/api/person/{id}")
36 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
37 */
38 public function removePersonAction(Request $request)
39 {
40 $em = $this->getDoctrine()->getManager();
41 $person = $em->getRepository('App:Person')->find($request->get('id'));
42
43 if (!empty($person)) {
44 $em->remove($person);
45 $em->flush();
46 }
47 //TODO: remove localisation and friendship
48 }
49
50 /**
51 * @Rest\Put(
52 * path = "/api/person/{id}/update",
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();
60 $person = $em->getRepository('App:Person')->find($request->get('id'));
61
62 if (empty($person)) {
63 return $this->PersonNotFound();
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 /**
77 * @Rest\Get("/api/person/{id}/localisations")
78 * @Rest\View()
79 */
80 public function getLocalisationsAction(Request $request)
81 {
82 $em = $this->getDoctrine()->getManager();
83 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
84
85 if (empty($localisations)) {
86 return $this->PersonLocalisationsNotFound();
87 }
88
89 return $localisations;
90 }
91
92 /**
93 * @Rest\Get("/api/person/{id}/localisation")
94 * @Rest\View()
95 */
96 public function getLocalisationAction(Request $request)
97 {
98 $em = $this->getDoctrine()->getManager();
99 $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
100
101 if (empty($localisations)) {
102 return $this->PersonLocalisationNotFound();
103 }
104
105 return $localisations;
106 }
107
108 /**
109 * @Rest\Post("/api/person/{id}/localisation")
110 * @Rest\View(StatusCode = Response::HTTP_CREATED)
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(
128 * path = "/api/person/{id}",
129 * name = "show_person",
130 * requirements = {"id"="\d+"}
131 * )
132 * @Rest\View()
133 */
134 public function showPerson(Request $request)
135 {
136 $em = $this->getDoctrine()->getManager();
137 $person = $em->getRepository('App:Person')->find($request->get('id'));
138
139 if (empty($person)) {
140 return $this->PersonNotFound();
141 }
142
143 return $person;
144 }
145
146 /**
147 * @Rest\Get(
148 * path = "/api/person/{email}",
149 * name = "show_person_by_email",
150 * requirements = {"email"="\s+"}
151 * )
152 * @Rest\View()
153 */
154 public function showPersonByEmail(Request $request)
155 {
156 $em = $this->getDoctrine()->getManager();
157 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
158
159 if (empty($person)) {
160 return $this->PersonNotFound();
161 }
162
163 return $person;
164 }
165
166 /**
167 * @Rest\Get(
168 * path = "/api/person/{id}/friends",
169 * name = "show_person_friends",
170 * requirements = {"id"="\d+"}
171 * )
172 * @Rest\View()
173 */
174 public function showPersonFriends(Request $request)
175 {
176 $em = $this->getDoctrine()->getManager();
177 $person = $em->getRepository('App:Person')->find($request->get('id'));
178
179 if (empty($person)) {
180 return $this->PersonNotFound();
181 }
182
183 return $person->getFriends();
184 }
185
186 /**
187 * @Rest\Get(
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
198 if (empty($persons)) {
199 return $this->PersonsNotFound();
200 }
201
202 return $persons;
203 }
204
205 /**
206 * @Rest\Get(
207 * path = "/api/person/{email}/friends",
208 * name = "show_person_friends_by_email",
209 * requirements = {"email"="\s+"}
210 * )
211 * @Rest\View()
212 */
213 public function showPersonFriendsByEmail(Request $request)
214 {
215 $em = $this->getDoctrine()->getManager();
216 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
217
218 if (empty($person)) {
219 return $this->PersonNotFound();
220 }
221
222 return $person->getFriends();
223 }
224
225 private function PersonNotFound() {
226 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
227 }
228
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
237 private function PersonLocalisationsNotFound() {
238 return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
239 }
240
241 }