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( | |
98f85207 | 19 | * path = "/api/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)) { |
d2685734 | 64 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
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 | ||
85 | /** | |
98f85207 | 86 | * @Rest\Post("/api/person/{id}/localisation") |
43f58db4 | 87 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
88 | */ |
89 | public function updateLocalisationAction(Request $request) | |
90 | { | |
91 | $localisation = new Localisation(); | |
92 | $localisation->setPerson($request->get('id')); | |
93 | $localisation->setTimestamp($request->get('timestamp')); | |
94 | $localisation->setLatitude($request->get('latitude')); | |
95 | $localisation->setLongitude($request->get('longitude')); | |
96 | ||
97 | $em = $this->getDoctrine()->getManager(); | |
98 | ||
99 | $em->persist($localisation); | |
100 | $em->flush(); | |
101 | } | |
102 | ||
103 | /** | |
104 | * @Rest\Get( | |
98f85207 | 105 | * path = "/api/person/{id}", |
dec6d031 JB |
106 | * name = "show_person", |
107 | * requirements = {"id"="\d+"} | |
108 | * ) | |
5347d06b | 109 | * @Rest\View() |
dec6d031 | 110 | */ |
51963d7f | 111 | public function showPerson(Request $request) |
dec6d031 | 112 | { |
5347d06b JB |
113 | $em = $this->getDoctrine()->getManager(); |
114 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
51963d7f | 115 | |
5347d06b | 116 | if (empty($person)) { |
d2685734 | 117 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
51963d7f JB |
118 | } |
119 | ||
5347d06b JB |
120 | $view = View::create($person); |
121 | $view->setFormat('json'); | |
122 | ||
d68c2073 JB |
123 | $viewHandler = $this->get('fos_rest.view_handler'); |
124 | return $viewHandler->handle($view); | |
dec6d031 JB |
125 | } |
126 | ||
127 | /** | |
128 | * @Rest\Get( | |
98f85207 | 129 | * path = "/api/person/{email}", |
dec6d031 JB |
130 | * name = "show_person_by_email", |
131 | * requirements = {"email"="\s+"} | |
132 | * ) | |
43f58db4 | 133 | * @Rest\View() |
dec6d031 | 134 | */ |
51963d7f | 135 | public function showPersonByEmail(Request $request) |
dec6d031 | 136 | { |
5347d06b JB |
137 | $em = $this->getDoctrine()->getManager(); |
138 | $person = $em->getRepository('App:Person')->find($request->get('email')); | |
139 | ||
140 | if (empty($person)) { | |
d2685734 | 141 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
5347d06b | 142 | } |
51963d7f | 143 | |
5347d06b JB |
144 | $view = View::create($person); |
145 | $view->setFormat('json'); | |
51963d7f | 146 | |
d68c2073 JB |
147 | $viewHandler = $this->get('fos_rest.view_handler'); |
148 | return $viewHandler->handle($view); | |
dec6d031 JB |
149 | } |
150 | ||
151 | /** | |
152 | * @Rest\Get( | |
98f85207 | 153 | * path = "/api/person/{id}/friends", |
dec6d031 JB |
154 | * name = "show_person_friends", |
155 | * requirements = {"id"="\d+"} | |
156 | * ) | |
51963d7f | 157 | * @Rest\View() |
dec6d031 | 158 | */ |
51963d7f | 159 | public function showPersonFriends(Request $request) |
dec6d031 | 160 | { |
5347d06b JB |
161 | $em = $this->getDoctrine()->getManager(); |
162 | $person = $em->getRepository('App:Person')->find($request->get('id')); | |
163 | ||
164 | if (empty($person)) { | |
d2685734 | 165 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
5347d06b | 166 | } |
51963d7f | 167 | |
5347d06b JB |
168 | $view = View::create($person->getFriends()); |
169 | $view->setFormat('json'); | |
51963d7f | 170 | |
d68c2073 JB |
171 | $viewHandler = $this->get('fos_rest.view_handler'); |
172 | return $viewHandler->handle($view); | |
dec6d031 JB |
173 | } |
174 | ||
175 | /** | |
176 | * @Rest\Get( | |
98f85207 | 177 | * path = "/api/person/{email}/friends", |
dec6d031 JB |
178 | * name = "show_person_friends_by_email", |
179 | * requirements = {"email"="\s+"} | |
180 | * ) | |
51963d7f | 181 | * @Rest\View() |
dec6d031 | 182 | */ |
51963d7f | 183 | public function showPersonFriendsByEmail(Request $request) |
dec6d031 | 184 | { |
5347d06b JB |
185 | $em = $this->getDoctrine()->getManager(); |
186 | $person = $em->getRepository('App:Person')->find($request->get('email')); | |
187 | ||
188 | if (empty($person)) { | |
d2685734 | 189 | return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
5347d06b | 190 | } |
51963d7f | 191 | |
5347d06b JB |
192 | $view = View::create($person->getFriends()); |
193 | $view->setFormat('json'); | |
51963d7f | 194 | |
d68c2073 JB |
195 | $viewHandler = $this->get('fos_rest.view_handler'); |
196 | return $viewHandler->handle($view); | |
dec6d031 JB |
197 | } |
198 | } |