Fix the localisations resssource.
[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 Controller
16 {
17 /**
18 * @Rest\Post(
19 * path = "/api/person/inscription",
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 /**
36 * @Rest\Delete("/api/person/{id}")
37 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
38 */
39 public function removePersonAction(Request $request)
40 {
41 $em = $this->getDoctrine()->getManager();
42 $person = $em->getRepository('App:Person')->find($request->get('id'));
43
44 if (!empty($person)) {
45 $em->remove($person);
46 $em->flush();
47 }
48 //TODO: remove localisation and friendship
49 }
50
51 /**
52 * @Rest\Put(
53 * path = "/api/person/{id}/update",
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();
61 $person = $em->getRepository('App:Person')->find($request->get('id'));
62
63 if (empty($person)) {
64 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
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 /**
78 * @Rest\Get("/api/person/{id}/friends/localisation")
79 */
80 public function getFriendsLocalisationAction(Request $request)
81 {
82
83 }
84
85 /**
86 * @Rest\Get("/api/person/{id}/localisations")
87 */
88 public function getLocalisationsAction(Request $request)
89 {
90 $em = $this->getDoctrine()->getManager();
91 $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
92
93 if (empty($localisations)) {
94 return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
95 }
96
97 $view = View::create($localisations);
98 $view->setFormat('json');
99
100 $viewHandler = $this->get('fos_rest.view_handler');
101 return $viewHandler->handle($view);
102
103 }
104
105 /**
106 * @Rest\Post("/api/person/{id}/localisation")
107 * @Rest\View(StatusCode = Response::HTTP_CREATED)
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(
125 * path = "/api/person/{id}",
126 * name = "show_person",
127 * requirements = {"id"="\d+"}
128 * )
129 * @Rest\View()
130 */
131 public function showPerson(Request $request)
132 {
133 $em = $this->getDoctrine()->getManager();
134 $person = $em->getRepository('App:Person')->find($request->get('id'));
135
136 if (empty($person)) {
137 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
138 }
139
140 $view = View::create($person);
141 $view->setFormat('json');
142
143 $viewHandler = $this->get('fos_rest.view_handler');
144 return $viewHandler->handle($view);
145 }
146
147 /**
148 * @Rest\Get(
149 * path = "/api/person/{email}",
150 * name = "show_person_by_email",
151 * requirements = {"email"="\s+"}
152 * )
153 * @Rest\View()
154 */
155 public function showPersonByEmail(Request $request)
156 {
157 $em = $this->getDoctrine()->getManager();
158 $person = $em->getRepository('App:Person')->find($request->get('email'));
159
160 if (empty($person)) {
161 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
162 }
163
164 $view = View::create($person);
165 $view->setFormat('json');
166
167 $viewHandler = $this->get('fos_rest.view_handler');
168 return $viewHandler->handle($view);
169 }
170
171 /**
172 * @Rest\Get(
173 * path = "/api/person/{id}/friends",
174 * name = "show_person_friends",
175 * requirements = {"id"="\d+"}
176 * )
177 * @Rest\View()
178 */
179 public function showPersonFriends(Request $request)
180 {
181 $em = $this->getDoctrine()->getManager();
182 $person = $em->getRepository('App:Person')->find($request->get('id'));
183
184 if (empty($person)) {
185 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
186 }
187
188 $view = View::create($person->getFriends());
189 $view->setFormat('json');
190
191 $viewHandler = $this->get('fos_rest.view_handler');
192 return $viewHandler->handle($view);
193 }
194
195 /**
196 * @Rest\Get(
197 * path = "/api/person/{email}/friends",
198 * name = "show_person_friends_by_email",
199 * requirements = {"email"="\s+"}
200 * )
201 * @Rest\View()
202 */
203 public function showPersonFriendsByEmail(Request $request)
204 {
205 $em = $this->getDoctrine()->getManager();
206 $person = $em->getRepository('App:Person')->find($request->get('email'));
207
208 if (empty($person)) {
209 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
210 }
211
212 $view = View::create($person->getFriends());
213 $view->setFormat('json');
214
215 $viewHandler = $this->get('fos_rest.view_handler');
216 return $viewHandler->handle($view);
217 }
218 }