a47df7c50736291fc9ef14ab4a31540673b04b4d
[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\Prefix("/api")
16 * @Rest\Post(
17 * path = "/person/inscription",
18 * name = "create_person"
19 * )
20 * @Rest\View(StatusCode = Response::HTTP_CREATED)
21 * @ParamConverter("person", converter="fos_rest.request_body")
22 */
23 public function createPersonAction(Person $person)
24 {
25 $em = $this->getDoctrine()->getManager();
26
27 $em->persist($person);
28 $em->flush();
29
30 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
31 }
32
33 /**
34 * @Rest\Delete("/person/{id}")
35 * @Rest\View(statusCode = Response::HTTP_NO_CONTENT)
36 */
37 public function removePersonAction(Request $request)
38 {
39 $em = $this->getDoctrine()->getManager();
40 $person = $em->getRepository('App::Person')->find($request->get('id'));
41
42 if (!empty($person)) {
43 $em->remove($person);
44 $em->flush();
45 }
46 }
47
48 /**
49 * @Rest\Put(
50 * path = "/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("/person/{id}/friends/localisation")
76 */
77 public function getFriendsLocalisationAction(Request $request)
78 {
79
80 }
81
82 /**
83 * @Rest\Post("/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\Get(
102 * path = "/person/{id}",
103 * name = "show_person",
104 * requirements = {"id"="\d+"}
105 * )
106 * @Rest\View()
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 = "/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 = "/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 = "/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 }