Commit | Line | Data |
---|---|---|
dec6d031 JB |
1 | <?php |
2 | namespace App\Controller; | |
3 | ||
4 | use App\Entity\Person; | |
1d9d8d51 JB |
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
6 | #use FOS\RestBundle\Controller\FOSRestController; | |
dec6d031 | 7 | use FOS\RestBundle\Controller\Annotations as Rest; |
5347d06b JB |
8 | use FOS\RestBundle\View\ViewHandler; |
9 | use FOS\RestBundle\View\View; | |
dec6d031 JB |
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 | ||
1d9d8d51 | 15 | class PersonController extends Controller |
dec6d031 JB |
16 | { |
17 | /** | |
18 | * @Rest\Post( | |
1faa29dc | 19 | * path = "/person/inscription", |
dec6d031 JB |
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 | /** | |
98f85207 | 36 | * @Rest\Delete("/api/person/{id}") |
dec6d031 JB |
37 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) |
38 | */ | |
39 | public function removePersonAction(Request $request) | |
40 | { | |
41 | $em = $this->getDoctrine()->getManager(); | |
282545e5 | 42 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
dec6d031 | 43 | |
51963d7f JB |
44 | if (!empty($person)) { |
45 | $em->remove($person); | |
dec6d031 JB |
46 | $em->flush(); |
47 | } | |
98f85207 | 48 | //TODO: remove localisation and friendship |
dec6d031 JB |
49 | } |
50 | ||
51 | /** | |
52 | * @Rest\Put( | |
98f85207 | 53 | * path = "/api/person/{id}/update", |
dec6d031 JB |
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(); | |
282545e5 | 61 | $person = $em->getRepository('App:Person')->find($request->get('id')); |
dec6d031 | 62 | |
51963d7f | 63 | if (empty($person)) { |
1faa29dc | 64 | return $this->PersonNotFound(); |
dec6d031 JB |
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 | /** | |
98f85207 | 78 | * @Rest\Get("/api/person/{id}/friends/localisation") |
dec6d031 JB |
79 | */ |
80 | public function getFriendsLocalisationAction(Request $request) | |
81 | { | |
82 | ||
83 | } | |
84 | ||
4c4feb3e JB |
85 | /** |
86 | * @Rest\Get("/api/person/{id}/localisations") | |
87 | */ | |
1de9ba73 | 88 | public function getLocalisationsAction(Request $request) |
4c4feb3e JB |
89 | { |
90 | $em = $this->getDoctrine()->getManager(); | |
1de9ba73 | 91 | $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); |
4c4feb3e JB |
92 | |
93 | if (empty($localisations)) { | |
1faa29dc | 94 | return $this->PersonLocalisationsNotFound(); |
4c4feb3e JB |
95 | } |
96 | ||
1faa29dc | 97 | return $localisations; |
4c4feb3e JB |
98 | } |
99 | ||
dec6d031 | 100 | /** |
98f85207 | 101 | * @Rest\Post("/api/person/{id}/localisation") |
43f58db4 | 102 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
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( | |
98f85207 | 120 | * path = "/api/person/{id}", |
dec6d031 JB |
121 | * name = "show_person", |
122 | * requirements = {"id"="\d+"} | |
123 | * ) | |
5347d06b | 124 | * @Rest\View() |
dec6d031 | 125 | */ |
51963d7f | 126 | public function showPerson(Request $request) |
dec6d031 | 127 | { |
5347d06b JB |
128 | $em = $this->getDoctrine()->getManager(); |
129 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
51963d7f | 130 | |
5347d06b | 131 | if (empty($person)) { |
1faa29dc | 132 | return $this->PersonNotFound(); |
51963d7f JB |
133 | } |
134 | ||
1faa29dc | 135 | return $person; |
dec6d031 JB |
136 | } |
137 | ||
138 | /** | |
139 | * @Rest\Get( | |
98f85207 | 140 | * path = "/api/person/{email}", |
dec6d031 JB |
141 | * name = "show_person_by_email", |
142 | * requirements = {"email"="\s+"} | |
143 | * ) | |
43f58db4 | 144 | * @Rest\View() |
dec6d031 | 145 | */ |
51963d7f | 146 | public function showPersonByEmail(Request $request) |
dec6d031 | 147 | { |
5347d06b | 148 | $em = $this->getDoctrine()->getManager(); |
b6077893 JB |
149 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
150 | ||
5347d06b | 151 | if (empty($person)) { |
1faa29dc | 152 | return $this->PersonNotFound(); |
5347d06b | 153 | } |
51963d7f | 154 | |
1faa29dc | 155 | return $person; |
dec6d031 JB |
156 | } |
157 | ||
158 | /** | |
159 | * @Rest\Get( | |
98f85207 | 160 | * path = "/api/person/{id}/friends", |
dec6d031 JB |
161 | * name = "show_person_friends", |
162 | * requirements = {"id"="\d+"} | |
163 | * ) | |
51963d7f | 164 | * @Rest\View() |
dec6d031 | 165 | */ |
51963d7f | 166 | public function showPersonFriends(Request $request) |
dec6d031 | 167 | { |
5347d06b JB |
168 | $em = $this->getDoctrine()->getManager(); |
169 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
170 | ||
171 | if (empty($person)) { | |
1faa29dc | 172 | return $this->PersonNotFound(); |
5347d06b | 173 | } |
51963d7f | 174 | |
1faa29dc | 175 | return $person->getFriends(); |
dec6d031 JB |
176 | } |
177 | ||
178 | /** | |
179 | * @Rest\Get( | |
0a86529e JB |
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 | ||
0a86529e | 190 | if (empty($persons)) { |
1faa29dc | 191 | return $this->PersonNotFound(); |
0a86529e JB |
192 | } |
193 | ||
0a86529e JB |
194 | return $persons; |
195 | } | |
196 | ||
197 | /** | |
198 | * @Rest\Get( | |
98f85207 | 199 | * path = "/api/person/{email}/friends", |
dec6d031 JB |
200 | * name = "show_person_friends_by_email", |
201 | * requirements = {"email"="\s+"} | |
202 | * ) | |
51963d7f | 203 | * @Rest\View() |
dec6d031 | 204 | */ |
51963d7f | 205 | public function showPersonFriendsByEmail(Request $request) |
dec6d031 | 206 | { |
5347d06b | 207 | $em = $this->getDoctrine()->getManager(); |
b6077893 JB |
208 | $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]); |
209 | ||
5347d06b | 210 | if (empty($person)) { |
1faa29dc | 211 | return $this->PersonNotFound(); |
5347d06b | 212 | } |
51963d7f | 213 | |
1faa29dc | 214 | return $person->getFriends(); |
dec6d031 | 215 | } |
b6077893 | 216 | |
1faa29dc JB |
217 | private function PersonNotFound() { |
218 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
b6077893 JB |
219 | } |
220 | ||
1faa29dc JB |
221 | private function PersonLocalisationsNotFound() { |
222 | return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); | |
b6077893 JB |
223 | } |
224 | ||
dec6d031 | 225 | } |