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 | ||
df299514 JB |
12 | /** |
13 | * @Rest\Prefix("/api") | |
14 | */ | |
dec6d031 JB |
15 | class PersonController extends FOSRestController |
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("/person/{id}") | |
37 | * @Rest\View(statusCode = Response::HTTP_NO_CONTENT) | |
38 | */ | |
39 | public function removePersonAction(Request $request) | |
40 | { | |
41 | $em = $this->getDoctrine()->getManager(); | |
51963d7f | 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 | } | |
48 | } | |
49 | ||
50 | /** | |
51 | * @Rest\Put( | |
52 | * path = "/person/{id}/update", | |
53 | * name = "update_person" | |
54 | * ) | |
55 | * @Rest\View(StatusCode = Response::HTTP_CREATED) | |
56 | */ | |
57 | public function updatePersonAction(Request $request) | |
58 | { | |
59 | $em = $this->getDoctrine()->getManager(); | |
43f58db4 | 60 | $person = $em->getRepository('App::Person')->find($request->get('id')); |
dec6d031 | 61 | |
51963d7f | 62 | if (empty($person)) { |
dec6d031 JB |
63 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); |
64 | } | |
65 | ||
66 | $person->setFirstName($request->get('firstname')); | |
67 | $person->setLastName($request->get('lastname')); | |
68 | $person->setEmail($request->get('email')); | |
69 | ||
70 | $em->merge($person); | |
71 | $em->flush(); | |
72 | ||
73 | return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]); | |
74 | } | |
75 | ||
76 | /** | |
77 | * @Rest\Get("/person/{id}/friends/localisation") | |
78 | */ | |
79 | public function getFriendsLocalisationAction(Request $request) | |
80 | { | |
81 | ||
82 | } | |
83 | ||
84 | /** | |
85 | * @Rest\Post("/person/{id}/localisation") | |
43f58db4 | 86 | * @Rest\View(StatusCode = Response::HTTP_CREATED) |
dec6d031 JB |
87 | */ |
88 | public function updateLocalisationAction(Request $request) | |
89 | { | |
90 | $localisation = new Localisation(); | |
91 | $localisation->setPerson($request->get('id')); | |
92 | $localisation->setTimestamp($request->get('timestamp')); | |
93 | $localisation->setLatitude($request->get('latitude')); | |
94 | $localisation->setLongitude($request->get('longitude')); | |
95 | ||
96 | $em = $this->getDoctrine()->getManager(); | |
97 | ||
98 | $em->persist($localisation); | |
99 | $em->flush(); | |
100 | } | |
101 | ||
102 | /** | |
103 | * @Rest\Get( | |
104 | * path = "/person/{id}", | |
105 | * name = "show_person", | |
106 | * requirements = {"id"="\d+"} | |
107 | * ) | |
43f58db4 | 108 | * @Rest\View() |
dec6d031 | 109 | */ |
51963d7f | 110 | public function showPerson(Request $request) |
dec6d031 | 111 | { |
51963d7f JB |
112 | $em = $this->getDoctrine()->getManager(); |
113 | $person = $em->getRepository('App::Person')->find($request->get('id')); | |
114 | ||
115 | if (empty($person)) { | |
116 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
117 | } | |
118 | ||
dec6d031 JB |
119 | return $person; |
120 | } | |
121 | ||
122 | /** | |
123 | * @Rest\Get( | |
124 | * path = "/person/{email}", | |
125 | * name = "show_person_by_email", | |
126 | * requirements = {"email"="\s+"} | |
127 | * ) | |
43f58db4 | 128 | * @Rest\View() |
dec6d031 | 129 | */ |
51963d7f | 130 | public function showPersonByEmail(Request $request) |
dec6d031 | 131 | { |
51963d7f JB |
132 | $em = $this->getDoctrine()->getManager(); |
133 | $person = $em->getRepository('App::Person')->find($request->get('email')); | |
134 | ||
135 | if (empty($person)) { | |
136 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
137 | } | |
138 | ||
dec6d031 JB |
139 | return $person; |
140 | } | |
141 | ||
142 | /** | |
143 | * @Rest\Get( | |
144 | * path = "/person/{id}/friends", | |
145 | * name = "show_person_friends", | |
146 | * requirements = {"id"="\d+"} | |
147 | * ) | |
51963d7f | 148 | * @Rest\View() |
dec6d031 | 149 | */ |
51963d7f | 150 | public function showPersonFriends(Request $request) |
dec6d031 | 151 | { |
51963d7f JB |
152 | $em = $this->getDoctrine()->getManager(); |
153 | $person = $em->getRepository('App::Person')->find($request->get('id')); | |
154 | ||
155 | if (empty($person)) { | |
156 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
157 | } | |
158 | ||
dec6d031 JB |
159 | return $person->getFriends(); |
160 | } | |
161 | ||
162 | /** | |
163 | * @Rest\Get( | |
164 | * path = "/person/{email}/friends", | |
165 | * name = "show_person_friends_by_email", | |
166 | * requirements = {"email"="\s+"} | |
167 | * ) | |
51963d7f | 168 | * @Rest\View() |
dec6d031 | 169 | */ |
51963d7f | 170 | public function showPersonFriendsByEmail(Request $request) |
dec6d031 | 171 | { |
51963d7f JB |
172 | $em = $this->getDoctrine()->getManager(); |
173 | $person = $em->getRepository('App::Person')->find($request->get('email')); | |
174 | ||
175 | if (empty($person)) { | |
176 | return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); | |
177 | } | |
178 | ||
dec6d031 JB |
179 | return $person->getFriends(); |
180 | } | |
181 | } |