Enable @Rest\View annotations.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
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 /**
15 * @Rest\Post(
16 * path = "/api/person/inscription",
17 * name = "create_person"
18 * )
19 * @Rest\View(StatusCode = Response::HTTP_CREATED)
20 * @ParamConverter("person", converter="fos_rest.request_body")
21 */
22 public function createPersonAction(Person $person)
23 {
24 $em = $this->getDoctrine()->getManager();
25
26 $em->persist($person);
27 $em->flush();
28
29 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
30 }
31
32 /**
33 * @Rest\Delete("/api/person/{id}")
34 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
35 */
36 public function removePersonAction(Request $request)
37 {
38 $em = $this->getDoctrine()->getManager();
39 $person = $em->getRepository('App::Person')->find($request->get('id'));
40
41 if (!empty($person)) {
42 $em->remove($person);
43 $em->flush();
44 }
45 //TODO: remove localisation and friendship
46 }
47
48 /**
49 * @Rest\Put(
50 * path = "/api/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();
58 $person = $em->getRepository('App::Person')->find($request->get('id'));
59
60 if (empty($person)) {
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("/api/person/{id}/friends/localisation")
76 */
77 public function getFriendsLocalisationAction(Request $request)
78 {
79
80 }
81
82 /**
83 * @Rest\Post("/api/person/{id}/localisation")
84 * @Rest\View(StatusCode = Response::HTTP_CREATED)
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\View()
102 * @Rest\Get(
103 * path = "/api/person/{id}",
104 * name = "show_person",
105 * requirements = {"id"="\d+"}
106 * )
107 */
108 public function showPerson(Request $request)
109 {
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
117 return $person;
118 }
119
120 /**
121 * @Rest\Get(
122 * path = "/api/person/{email}",
123 * name = "show_person_by_email",
124 * requirements = {"email"="\s+"}
125 * )
126 * @Rest\View()
127 */
128 public function showPersonByEmail(Request $request)
129 {
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
137 return $person;
138 }
139
140 /**
141 * @Rest\Get(
142 * path = "/api/person/{id}/friends",
143 * name = "show_person_friends",
144 * requirements = {"id"="\d+"}
145 * )
146 * @Rest\View()
147 */
148 public function showPersonFriends(Request $request)
149 {
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
157 return $person->getFriends();
158 }
159
160 /**
161 * @Rest\Get(
162 * path = "/api/person/{email}/friends",
163 * name = "show_person_friends_by_email",
164 * requirements = {"email"="\s+"}
165 * )
166 * @Rest\View()
167 */
168 public function showPersonFriendsByEmail(Request $request)
169 {
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
177 return $person->getFriends();
178 }
179 }