Commit | Line | Data |
---|---|---|
dec6d031 JB |
1 | <?php |
2 | namespace App\Controller; | |
3 | ||
4 | use App\Entity\Person; | |
5 | use FOS\RestBundle\Controller\FOSRestController; | |
6 | use FOS\RestBundle\Controller\Annotations as Rest; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
8 | use Symfony\Component\HttpFoundation\Request; | |
9 | use Symfony\Component\HttpFoundation\Response; | |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
11 | ||
12 | class PersonController extends FOSRestController | |
13 | { | |
14 | /** | |
51963d7f | 15 | * @Rest\Prefix("/api") |
dec6d031 JB |
16 | * @Rest\Post( |
17 | * path = "/person/inscription", | |
18 | * name = "create_person" | |
19 | * ) | |
20 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
21 | * @ParamConverter("person", converter="fos_rest.request_body") | |
22 | */ | |
23 | public function createPersonAction(Person $person) | |
24 | { | |
25 | $em = $this->getDoctrine()->getManager(); | |
26 | ||
27 | $em->persist($person); | |
28 | $em->flush(); | |
29 | ||
30 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
31 | } | |
32 | ||
33 | /** | |
34 | * @Rest\Delete("/person/{id}") | |
35 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) | |
36 | */ | |
37 | public function removePersonAction(Request $request) | |
38 | { | |
39 | $em = $this->getDoctrine()->getManager(); | |
51963d7f | 40 | $person = $em->getRepository('App::Person')->find($request->get('id')); |
dec6d031 | 41 | |
51963d7f JB |
42 | if (!empty($person)) { |
43 | $em->remove($person); | |
dec6d031 JB |
44 | $em->flush(); |
45 | } | |
46 | } | |
47 | ||
48 | /** | |
49 | * @Rest\Put( | |
50 | * path = "/person/{id}/update", | |
51 | * name = "update_person" | |
52 | * ) | |
53 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
54 | */ | |
55 | public function updatePersonAction(Request $request) | |
56 | { | |
57 | $em = $this->getDoctrine()->getManager(); | |
43f58db4 | 58 | $person = $em->getRepository('App::Person')->find($request->get('id')); |
dec6d031 | 59 | |
51963d7f | 60 | if (empty($person)) { |
dec6d031 JB |
61 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
62 | } | |
63 | ||
64 | $person->setFirstName($request->get('firstname')); | |
65 | $person->setLastName($request->get('lastname')); | |
66 | $person->setEmail($request->get('email')); | |
67 | ||
68 | $em->merge($person); | |
69 | $em->flush(); | |
70 | ||
71 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
72 | } | |
73 | ||
74 | /** | |
75 | * @Rest\Get("/person/{id}/friends/localisation") | |
76 | */ | |
77 | public function getFriendsLocalisationAction(Request $request) | |
78 | { | |
79 | ||
80 | } | |
81 | ||
82 | /** | |
83 | * @Rest\Post("/person/{id}/localisation") | |
43f58db4 | 84 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
85 | */ |
86 | public function updateLocalisationAction(Request $request) | |
87 | { | |
88 | $localisation = new Localisation(); | |
89 | $localisation->setPerson($request->get('id')); | |
90 | $localisation->setTimestamp($request->get('timestamp')); | |
91 | $localisation->setLatitude($request->get('latitude')); | |
92 | $localisation->setLongitude($request->get('longitude')); | |
93 | ||
94 | $em = $this->getDoctrine()->getManager(); | |
95 | ||
96 | $em->persist($localisation); | |
97 | $em->flush(); | |
98 | } | |
99 | ||
100 | /** | |
101 | * @Rest\Get( | |
102 | * path = "/person/{id}", | |
103 | * name = "show_person", | |
104 | * requirements = {"id"="\d+"} | |
105 | * ) | |
43f58db4 | 106 | * @Rest\View() |
dec6d031 | 107 | */ |
51963d7f | 108 | public function showPerson(Request $request) |
dec6d031 | 109 | { |
51963d7f JB |
110 | $em = $this->getDoctrine()->getManager(); |
111 | $person = $em->getRepository('App::Person')->find($request->get('id')); | |
112 | ||
113 | if (empty($person)) { | |
114 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
115 | } | |
116 | ||
dec6d031 JB |
117 | return $person; |
118 | } | |
119 | ||
120 | /** | |
121 | * @Rest\Get( | |
122 | * path = "/person/{email}", | |
123 | * name = "show_person_by_email", | |
124 | * requirements = {"email"="\s+"} | |
125 | * ) | |
43f58db4 | 126 | * @Rest\View() |
dec6d031 | 127 | */ |
51963d7f | 128 | public function showPersonByEmail(Request $request) |
dec6d031 | 129 | { |
51963d7f JB |
130 | $em = $this->getDoctrine()->getManager(); |
131 | $person = $em->getRepository('App::Person')->find($request->get('email')); | |
132 | ||
133 | if (empty($person)) { | |
134 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
135 | } | |
136 | ||
dec6d031 JB |
137 | return $person; |
138 | } | |
139 | ||
140 | /** | |
141 | * @Rest\Get( | |
142 | * path = "/person/{id}/friends", | |
143 | * name = "show_person_friends", | |
144 | * requirements = {"id"="\d+"} | |
145 | * ) | |
51963d7f | 146 | * @Rest\View() |
dec6d031 | 147 | */ |
51963d7f | 148 | public function showPersonFriends(Request $request) |
dec6d031 | 149 | { |
51963d7f JB |
150 | $em = $this->getDoctrine()->getManager(); |
151 | $person = $em->getRepository('App::Person')->find($request->get('id')); | |
152 | ||
153 | if (empty($person)) { | |
154 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
155 | } | |
156 | ||
dec6d031 JB |
157 | return $person->getFriends(); |
158 | } | |
159 | ||
160 | /** | |
161 | * @Rest\Get( | |
162 | * path = "/person/{email}/friends", | |
163 | * name = "show_person_friends_by_email", | |
164 | * requirements = {"email"="\s+"} | |
165 | * ) | |
51963d7f | 166 | * @Rest\View() |
dec6d031 | 167 | */ |
51963d7f | 168 | public function showPersonFriendsByEmail(Request $request) |
dec6d031 | 169 | { |
51963d7f JB |
170 | $em = $this->getDoctrine()->getManager(); |
171 | $person = $em->getRepository('App::Person')->find($request->get('email')); | |
172 | ||
173 | if (empty($person)) { | |
174 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
175 | } | |
176 | ||
dec6d031 JB |
177 | return $person->getFriends(); |
178 | } | |
179 | } |