c6b0b2c12f7321ef4be4e2f68f2ef45a4d1d4008
[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 FOS\RestBundle\View\ViewHandler;
8 use FOS\RestBundle\View\View;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\HttpFoundation\Response;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
14 class PersonController extends FOSRestController
15 {
16 /**
17 * @Rest\Post(
18 * path = "/api/person/inscription",
19 * name = "create_person"
20 * )
21 * @Rest\View(StatusCode = Response::HTTP_CREATED)
22 * @ParamConverter("person", converter="fos_rest.request_body")
23 */
24 public function createPersonAction(Person $person)
25 {
26 $em = $this->getDoctrine()->getManager();
27
28 $em->persist($person);
29 $em->flush();
30
31 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
32 }
33
34 /**
35 * @Rest\Delete("/api/person/{id}")
36 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
37 */
38 public function removePersonAction(Request $request)
39 {
40 $em = $this->getDoctrine()->getManager();
41 $person = $em->getRepository('App:Person')->find($request->get('id'));
42
43 if (!empty($person)) {
44 $em->remove($person);
45 $em->flush();
46 }
47 //TODO: remove localisation and friendship
48 }
49
50 /**
51 * @Rest\Put(
52 * path = "/api/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();
60 $person = $em->getRepository('App:Person')->find($request->get('id'));
61
62 if (empty($person)) {
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("/api/person/{id}/friends/localisation")
78 */
79 public function getFriendsLocalisationAction(Request $request)
80 {
81
82 }
83
84 /**
85 * @Rest\Post("/api/person/{id}/localisation")
86 * @Rest\View(StatusCode = Response::HTTP_CREATED)
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 = "/api/person/{id}",
105 * name = "show_person",
106 * requirements = {"id"="\d+"}
107 * )
108 * @Rest\View()
109 */
110 public function showPerson(Request $request)
111 {
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
119 $view = View::create($person);
120 $view->setFormat('json');
121
122 return $view;
123 }
124
125 /**
126 * @Rest\Get(
127 * path = "/api/person/{email}",
128 * name = "show_person_by_email",
129 * requirements = {"email"="\s+"}
130 * )
131 * @Rest\View()
132 */
133 public function showPersonByEmail(Request $request)
134 {
135 $em = $this->getDoctrine()->getManager();
136 $person = $em->getRepository('App:Person')->find($request->get('email'));
137
138 if (empty($person)) {
139 return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
140 }
141
142 $view = View::create($person);
143 $view->setFormat('json');
144
145 return $view;
146 }
147
148 /**
149 * @Rest\Get(
150 * path = "/api/person/{id}/friends",
151 * name = "show_person_friends",
152 * requirements = {"id"="\d+"}
153 * )
154 * @Rest\View()
155 */
156 public function showPersonFriends(Request $request)
157 {
158 $em = $this->getDoctrine()->getManager();
159 $person = $em->getRepository('App:Person')->find($request->get('id'));
160
161 if (empty($person)) {
162 return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
163 }
164
165 $view = View::create($person->getFriends());
166 $view->setFormat('json');
167
168 return $view;
169 }
170
171 /**
172 * @Rest\Get(
173 * path = "/api/person/{email}/friends",
174 * name = "show_person_friends_by_email",
175 * requirements = {"email"="\s+"}
176 * )
177 * @Rest\View()
178 */
179 public function showPersonFriendsByEmail(Request $request)
180 {
181 $em = $this->getDoctrine()->getManager();
182 $person = $em->getRepository('App:Person')->find($request->get('email'));
183
184 if (empty($person)) {
185 return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
186 }
187
188 $view = View::create($person->getFriends());
189 $view->setFormat('json');
190
191 return $view;
192 }
193 }