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