d4bd7ff374204dc15b14c68f3ec21e7f6d096bd3
[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 = "/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 $this->PersonNotFound();
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 $this->PersonLocalisationsNotFound();
95 }
96
97 return $localisations;
98 }
99
100 /**
101 * @Rest\Post("/api/person/{id}/localisation")
102 * @Rest\View(StatusCode = Response::HTTP_CREATED)
103 */
104 public function updateLocalisationAction(Request $request)
105 {
106 $localisation = new Localisation();
107 $localisation->setPerson($request->get('id'));
108 $localisation->setTimestamp($request->get('timestamp'));
109 $localisation->setLatitude($request->get('latitude'));
110 $localisation->setLongitude($request->get('longitude'));
111
112 $em = $this->getDoctrine()->getManager();
113
114 $em->persist($localisation);
115 $em->flush();
116 }
117
118 /**
119 * @Rest\Get(
120 * path = "/api/person/{id}",
121 * name = "show_person",
122 * requirements = {"id"="\d+"}
123 * )
124 * @Rest\View()
125 */
126 public function showPerson(Request $request)
127 {
128 $em = $this->getDoctrine()->getManager();
129 $person = $em->getRepository('App:Person')->find($request->get('id'));
130
131 if (empty($person)) {
132 return $this->PersonNotFound();
133 }
134
135 return $person;
136 }
137
138 /**
139 * @Rest\Get(
140 * path = "/api/person/{email}",
141 * name = "show_person_by_email",
142 * requirements = {"email"="\s+"}
143 * )
144 * @Rest\View()
145 */
146 public function showPersonByEmail(Request $request)
147 {
148 $em = $this->getDoctrine()->getManager();
149 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
150
151 if (empty($person)) {
152 return $this->PersonNotFound();
153 }
154
155 return $person;
156 }
157
158 /**
159 * @Rest\Get(
160 * path = "/api/person/{id}/friends",
161 * name = "show_person_friends",
162 * requirements = {"id"="\d+"}
163 * )
164 * @Rest\View()
165 */
166 public function showPersonFriends(Request $request)
167 {
168 $em = $this->getDoctrine()->getManager();
169 $person = $em->getRepository('App:Person')->find($request->get('id'));
170
171 if (empty($person)) {
172 return $this->PersonNotFound();
173 }
174
175 return $person->getFriends();
176 }
177
178 /**
179 * @Rest\Get(
180 * path = "/api/persons",
181 * name = "show_persons"
182 * )
183 * @Rest\View()
184 */
185 public function showPersons(Request $request)
186 {
187 $em = $this->getDoctrine()->getManager();
188 $persons = $em->getRepository('App:Person')->findAll();
189
190 if (empty($persons)) {
191 return $this->PersonNotFound();
192 }
193
194 return $persons;
195 }
196
197 /**
198 * @Rest\Get(
199 * path = "/api/person/{email}/friends",
200 * name = "show_person_friends_by_email",
201 * requirements = {"email"="\s+"}
202 * )
203 * @Rest\View()
204 */
205 public function showPersonFriendsByEmail(Request $request)
206 {
207 $em = $this->getDoctrine()->getManager();
208 $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
209
210 if (empty($person)) {
211 return $this->PersonNotFound();
212 }
213
214 return $person->getFriends();
215 }
216
217 private function PersonNotFound() {
218 return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
219 }
220
221 private function PersonLocalisationsNotFound() {
222 return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
223 }
224
225 }