Return a JSON view explicitly.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
CommitLineData
dec6d031
JB
1<?php
2namespace App\Controller;
3
4use App\Entity\Person;
5use FOS\RestBundle\Controller\FOSRestController;
6use FOS\RestBundle\Controller\Annotations as Rest;
5347d06b
JB
7use FOS\RestBundle\View\ViewHandler;
8use FOS\RestBundle\View\View;
dec6d031
JB
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\HttpFoundation\Response;
12use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
14class PersonController extends FOSRestController
15{
16 /**
17 * @Rest\Post(
98f85207 18 * path = "/api/person/inscription",
dec6d031
JB
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 /**
98f85207 35 * @Rest\Delete("/api/person/{id}")
dec6d031
JB
36 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
37 */
38 public function removePersonAction(Request $request)
39 {
40 $em = $this->getDoctrine()->getManager();
282545e5 41 $person = $em->getRepository('App:Person')->find($request->get('id'));
dec6d031 42
51963d7f
JB
43 if (!empty($person)) {
44 $em->remove($person);
dec6d031
JB
45 $em->flush();
46 }
98f85207 47 //TODO: remove localisation and friendship
dec6d031
JB
48 }
49
50 /**
51 * @Rest\Put(
98f85207 52 * path = "/api/person/{id}/update",
dec6d031
JB
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();
282545e5 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 /**
98f85207 77 * @Rest\Get("/api/person/{id}/friends/localisation")
dec6d031
JB
78 */
79 public function getFriendsLocalisationAction(Request $request)
80 {
81
82 }
83
84 /**
98f85207 85 * @Rest\Post("/api/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(
98f85207 104 * path = "/api/person/{id}",
dec6d031
JB
105 * name = "show_person",
106 * requirements = {"id"="\d+"}
107 * )
5347d06b 108 * @Rest\View()
dec6d031 109 */
51963d7f 110 public function showPerson(Request $request)
dec6d031 111 {
5347d06b
JB
112 $em = $this->getDoctrine()->getManager();
113 $person = $em->getRepository('App:Person')->find($request->get('id'));
51963d7f 114
5347d06b 115 if (empty($person)) {
51963d7f
JB
116 return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
117 }
118
5347d06b
JB
119 $view = View::create($person);
120 $view->setFormat('json');
121
122 return $view;
dec6d031
JB
123 }
124
125 /**
126 * @Rest\Get(
98f85207 127 * path = "/api/person/{email}",
dec6d031
JB
128 * name = "show_person_by_email",
129 * requirements = {"email"="\s+"}
130 * )
43f58db4 131 * @Rest\View()
dec6d031 132 */
51963d7f 133 public function showPersonByEmail(Request $request)
dec6d031 134 {
5347d06b
JB
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 }
51963d7f 141
5347d06b
JB
142 $view = View::create($person);
143 $view->setFormat('json');
51963d7f 144
5347d06b 145 return $view;
dec6d031
JB
146 }
147
148 /**
149 * @Rest\Get(
98f85207 150 * path = "/api/person/{id}/friends",
dec6d031
JB
151 * name = "show_person_friends",
152 * requirements = {"id"="\d+"}
153 * )
51963d7f 154 * @Rest\View()
dec6d031 155 */
51963d7f 156 public function showPersonFriends(Request $request)
dec6d031 157 {
5347d06b
JB
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 }
51963d7f 164
5347d06b
JB
165 $view = View::create($person->getFriends());
166 $view->setFormat('json');
51963d7f 167
5347d06b 168 return $view;
dec6d031
JB
169 }
170
171 /**
172 * @Rest\Get(
98f85207 173 * path = "/api/person/{email}/friends",
dec6d031
JB
174 * name = "show_person_friends_by_email",
175 * requirements = {"email"="\s+"}
176 * )
51963d7f 177 * @Rest\View()
dec6d031 178 */
51963d7f 179 public function showPersonFriendsByEmail(Request $request)
dec6d031 180 {
5347d06b
JB
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 }
51963d7f 187
5347d06b
JB
188 $view = View::create($person->getFriends());
189 $view->setFormat('json');
51963d7f 190
5347d06b 191 return $view;
dec6d031
JB
192 }
193}