Enable @Rest\View annotations.
[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;
7use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\Response;
10use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
12class PersonController extends FOSRestController
13{
14 /**
15 * @Rest\Post(
98f85207 16 * path = "/api/person/inscription",
dec6d031
JB
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 /**
98f85207 33 * @Rest\Delete("/api/person/{id}")
dec6d031
JB
34 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
35 */
36 public function removePersonAction(Request $request)
37 {
38 $em = $this->getDoctrine()->getManager();
51963d7f 39 $person = $em->getRepository('App::Person')->find($request->get('id'));
dec6d031 40
51963d7f
JB
41 if (!empty($person)) {
42 $em->remove($person);
dec6d031
JB
43 $em->flush();
44 }
98f85207 45 //TODO: remove localisation and friendship
dec6d031
JB
46 }
47
48 /**
49 * @Rest\Put(
98f85207 50 * path = "/api/person/{id}/update",
dec6d031
JB
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 /**
98f85207 75 * @Rest\Get("/api/person/{id}/friends/localisation")
dec6d031
JB
76 */
77 public function getFriendsLocalisationAction(Request $request)
78 {
79
80 }
81
82 /**
98f85207 83 * @Rest\Post("/api/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 /**
9e71bdc5 101 * @Rest\View()
dec6d031 102 * @Rest\Get(
98f85207 103 * path = "/api/person/{id}",
dec6d031
JB
104 * name = "show_person",
105 * requirements = {"id"="\d+"}
106 * )
dec6d031 107 */
51963d7f 108 public function showPerson(Request $request)
dec6d031 109 {
51963d7f 110 $em = $this->getDoctrine()->getManager();
9e71bdc5 111 $person = $em->getRepository('App:Person')->find($request->get('id'));
51963d7f
JB
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(
98f85207 122 * path = "/api/person/{email}",
dec6d031
JB
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(
98f85207 142 * path = "/api/person/{id}/friends",
dec6d031
JB
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 150 $em = $this->getDoctrine()->getManager();
9e71bdc5 151 $person = $em->getRepository('App:Person')->find($request->get('id'));
51963d7f
JB
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(
98f85207 162 * path = "/api/person/{email}/friends",
dec6d031
JB
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}