1a6821335bad4f2715b2a16dd788982940ada456
[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 = "/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("/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 ($person) {
42 $em->remove($place);
43 $em->flush();
44 }
45 }
46
47 /**
48 * @Rest\Put(
49 * path = "/person/{id}/update",
50 * name = "update_person"
51 * )
52 * @Rest\View(StatusCode = Response::HTTP_CREATED)
53 */
54 public function updatePersonAction(Request $request)
55 {
56 $em = $this->getDoctrine()->getManager();
57 $person = $em->getRepository('App:Person')->find($request->get('id'));
58
59 if (empty($place)) {
60 return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
61 }
62
63 $person->setFirstName($request->get('firstname'));
64 $person->setLastName($request->get('lastname'));
65 $person->setEmail($request->get('email'));
66
67 $em->merge($person);
68 $em->flush();
69
70 return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
71 }
72
73 /**
74 * @Rest\Get("/person/{id}/friends/localisation")
75 */
76 public function getFriendsLocalisationAction(Request $request)
77 {
78
79 }
80
81 /**
82 * @Rest\Post("/person/{id}/localisation")
83 */
84 public function updateLocalisationAction(Request $request)
85 {
86 $localisation = new Localisation();
87 $localisation->setPerson($request->get('id'));
88 $localisation->setTimestamp($request->get('timestamp'));
89 $localisation->setLatitude($request->get('latitude'));
90 $localisation->setLongitude($request->get('longitude'));
91
92 $em = $this->getDoctrine()->getManager();
93
94 $em->persist($localisation);
95 $em->flush();
96 }
97
98 /**
99 * @Rest\Get(
100 * path = "/person/{id}",
101 * name = "show_person",
102 * requirements = {"id"="\d+"}
103 * )
104 * @Rest\View
105 */
106 public function showPerson(Person $person)
107 {
108 return $person;
109 }
110
111 /**
112 * @Rest\Get(
113 * path = "/person/{email}",
114 * name = "show_person_by_email",
115 * requirements = {"email"="\s+"}
116 * )
117 * @Rest\View
118 */
119 public function showPersonByEmail(Person $person)
120 {
121 return $person;
122 }
123
124 /**
125 * @Rest\Get(
126 * path = "/person/{id}/friends",
127 * name = "show_person_friends",
128 * requirements = {"id"="\d+"}
129 * )
130 * @Rest\View
131 */
132 public function showPersonFriends(Person $person)
133 {
134 return $person->getFriends();
135 }
136
137 /**
138 * @Rest\Get(
139 * path = "/person/{email}/friends",
140 * name = "show_person_friends_by_email",
141 * requirements = {"email"="\s+"}
142 * )
143 * @Rest\View
144 */
145 public function showPersonFriendsByEmail(Person $person)
146 {
147 return $person->getFriends();
148 }
149 }