6
6
7
7
use App \Common \DoctrineListRepresentationFactory ;
8
8
use App \Entity \Location ;
9
+ use App \Repository \LocationRepository ;
9
10
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
10
11
use Symfony \Component \HttpFoundation \Request ;
11
12
use Symfony \Component \HttpFoundation \Response ;
13
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
12
14
use Symfony \Component \Routing \Annotation \Route ;
13
15
14
16
class LocationController extends AbstractController
15
17
{
18
+ /**
19
+ * @var LocationRepository
20
+ */
21
+ private $ locationRepository ;
22
+
16
23
/**
17
24
* @var DoctrineListRepresentationFactory
18
25
*/
19
26
private $ doctrineListRepresentationFactory ;
20
27
21
28
public function __construct (
29
+ LocationRepository $ repository ,
22
30
DoctrineListRepresentationFactory $ doctrineListRepresentationFactory
23
31
) {
32
+ $ this ->locationRepository = $ repository ;
24
33
$ this ->doctrineListRepresentationFactory = $ doctrineListRepresentationFactory ;
25
34
}
26
35
36
+ /**
37
+ * @Route("/admin/api/locations/{id}", methods={"GET"}, name="app.get_location")
38
+ */
39
+ public function getAction (int $ id , Request $ request ): Response
40
+ {
41
+ $ location = $ this ->load ($ id );
42
+ if (!$ location ) {
43
+ throw new NotFoundHttpException ();
44
+ }
45
+
46
+ return $ this ->json ($ this ->getDataForEntity ($ location ));
47
+ }
48
+
49
+ /**
50
+ * @Route("/admin/api/locations/{id}", methods={"PUT"}, name="app.put_location")
51
+ */
52
+ public function putAction (int $ id , Request $ request ): Response
53
+ {
54
+ $ location = $ this ->load ($ id );
55
+ if (!$ location ) {
56
+ throw new NotFoundHttpException ();
57
+ }
58
+
59
+ $ this ->mapDataToEntity ($ request ->toArray (), $ location );
60
+ $ this ->save ($ location );
61
+
62
+ return $ this ->json ($ this ->getDataForEntity ($ location ));
63
+ }
64
+
65
+ /**
66
+ * @Route("/admin/api/locations", methods={"POST"}, name="app.post_location")
67
+ */
68
+ public function postAction (Request $ request ): Response
69
+ {
70
+ $ location = $ this ->create ();
71
+
72
+ $ this ->mapDataToEntity ($ request ->toArray (), $ location );
73
+ $ this ->save ($ location );
74
+
75
+ return $ this ->json ($ this ->getDataForEntity ($ location ), 201 );
76
+ }
77
+
78
+ /**
79
+ * @Route("/admin/api/locations/{id}", methods={"DELETE"}, name="app.delete_location")
80
+ */
81
+ public function deleteAction (int $ id ): Response
82
+ {
83
+ $ this ->remove ($ id );
84
+
85
+ return $ this ->json (null , 204 );
86
+ }
87
+
27
88
/**
28
89
* @Route("/admin/api/locations", methods={"GET"}, name="app.get_location_list")
29
90
*/
@@ -35,4 +96,53 @@ public function getListAction(Request $request): Response
35
96
36
97
return $ this ->json ($ listRepresentation ->toArray ());
37
98
}
99
+
100
+ /**
101
+ * @return array<string, mixed>
102
+ */
103
+ protected function getDataForEntity (Location $ entity ): array
104
+ {
105
+ return [
106
+ 'id ' => $ entity ->getId (),
107
+ 'name ' => $ entity ->getName (),
108
+ 'street ' => $ entity ->getStreet (),
109
+ 'number ' => $ entity ->getNumber (),
110
+ 'postalCode ' => $ entity ->getPostalCode (),
111
+ 'city ' => $ entity ->getCity (),
112
+ 'countryCode ' => $ entity ->getCountryCode (),
113
+ ];
114
+ }
115
+
116
+ /**
117
+ * @param array<string, mixed> $data
118
+ */
119
+ protected function mapDataToEntity (array $ data , Location $ entity ): void
120
+ {
121
+ $ entity ->setName ($ data ['name ' ]);
122
+ $ entity ->setStreet ($ data ['street ' ] ?? '' );
123
+ $ entity ->setNumber ($ data ['number ' ] ?? '' );
124
+ $ entity ->setPostalCode ($ data ['postalCode ' ] ?? '' );
125
+ $ entity ->setCity ($ data ['city ' ] ?? '' );
126
+ $ entity ->setCountryCode ($ data ['countryCode ' ] ?? '' );
127
+ }
128
+
129
+ protected function load (int $ id ): ?Location
130
+ {
131
+ return $ this ->locationRepository ->findById ($ id );
132
+ }
133
+
134
+ protected function create (): Location
135
+ {
136
+ return $ this ->locationRepository ->create ();
137
+ }
138
+
139
+ protected function save (Location $ entity ): void
140
+ {
141
+ $ this ->locationRepository ->save ($ entity );
142
+ }
143
+
144
+ protected function remove (int $ id ): void
145
+ {
146
+ $ this ->locationRepository ->remove ($ id );
147
+ }
38
148
}
0 commit comments