6
6
7
7
use App \Common \DoctrineListRepresentationFactory ;
8
8
use App \Entity \Location ;
9
+ use App \Repository \LocationRepository ;
9
10
use FOS \RestBundle \Controller \Annotations \RouteResource ;
10
11
use FOS \RestBundle \Routing \ClassResourceInterface ;
11
12
use FOS \RestBundle \View \ViewHandlerInterface ;
12
13
use Sulu \Component \Rest \AbstractRestController ;
14
+ use Symfony \Component \HttpFoundation \Request ;
13
15
use Symfony \Component \HttpFoundation \Response ;
16
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
14
17
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
15
18
16
19
/**
@@ -23,12 +26,19 @@ class LocationController extends AbstractRestController implements ClassResource
23
26
*/
24
27
private $ doctrineListRepresentationFactory ;
25
28
29
+ /**
30
+ * @var LocationRepository
31
+ */
32
+ private $ repository ;
33
+
26
34
public function __construct (
27
35
DoctrineListRepresentationFactory $ doctrineListRepresentationFactory ,
36
+ LocationRepository $ repository ,
28
37
ViewHandlerInterface $ viewHandler ,
29
38
?TokenStorageInterface $ tokenStorage = null
30
39
) {
31
40
$ this ->doctrineListRepresentationFactory = $ doctrineListRepresentationFactory ;
41
+ $ this ->repository = $ repository ;
32
42
33
43
parent ::__construct ($ viewHandler , $ tokenStorage );
34
44
}
@@ -41,4 +51,79 @@ public function cgetAction(): Response
41
51
42
52
return $ this ->handleView ($ this ->view ($ listRepresentation ));
43
53
}
54
+
55
+ public function getAction (int $ id , Request $ request ): Response
56
+ {
57
+ $ entity = $ this ->load ($ id );
58
+ if (!$ entity ) {
59
+ throw new NotFoundHttpException ();
60
+ }
61
+
62
+ return $ this ->handleView ($ this ->view ($ entity ));
63
+ }
64
+
65
+ public function postAction (Request $ request ): Response
66
+ {
67
+ $ entity = $ this ->create ();
68
+
69
+ $ this ->mapDataToEntity ($ request ->request ->all (), $ entity );
70
+
71
+ $ this ->save ($ entity );
72
+
73
+ return $ this ->handleView ($ this ->view ($ entity ));
74
+ }
75
+
76
+ public function putAction (int $ id , Request $ request ): Response
77
+ {
78
+ $ entity = $ this ->load ($ id );
79
+ if (!$ entity ) {
80
+ throw new NotFoundHttpException ();
81
+ }
82
+
83
+ $ this ->mapDataToEntity ($ request ->request ->all (), $ entity );
84
+
85
+ $ this ->save ($ entity );
86
+
87
+ return $ this ->handleView ($ this ->view ($ entity ));
88
+ }
89
+
90
+ public function deleteAction (int $ id ): Response
91
+ {
92
+ $ this ->remove ($ id );
93
+
94
+ return $ this ->handleView ($ this ->view ());
95
+ }
96
+
97
+ /**
98
+ * @param string[] $data
99
+ */
100
+ protected function mapDataToEntity (array $ data , Location $ entity ): void
101
+ {
102
+ $ entity ->setName ($ data ['name ' ]);
103
+ $ entity ->setStreet ($ data ['street ' ] ?? '' );
104
+ $ entity ->setNumber ($ data ['number ' ] ?? '' );
105
+ $ entity ->setCity ($ data ['city ' ] ?? '' );
106
+ $ entity ->setPostalCode ($ data ['postalCode ' ] ?? '' );
107
+ $ entity ->setCountryCode ($ data ['countryCode ' ] ?? '' );
108
+ }
109
+
110
+ protected function load (int $ id ): ?Location
111
+ {
112
+ return $ this ->repository ->findById ($ id );
113
+ }
114
+
115
+ protected function create (): Location
116
+ {
117
+ return $ this ->repository ->create ();
118
+ }
119
+
120
+ protected function save (Location $ entity ): void
121
+ {
122
+ $ this ->repository ->save ($ entity );
123
+ }
124
+
125
+ protected function remove (int $ id ): void
126
+ {
127
+ $ this ->repository ->remove ($ id );
128
+ }
44
129
}
0 commit comments