@@ -8,9 +8,7 @@ beats a simple REST solution.
8
8
Handles authenication and provides common functions for serving and parsing
9
9
API requests. Compared to ` silverstripe-restfulserver ` this module does very
10
10
little scaffolding of models and fields out of the box but instead relies on
11
- developers to design the API layout (although scaffolding helpers are available
12
-
13
- - see below)
11
+ developers to design the API layout (although scaffolding helpers are available)
14
12
15
13
## Installation
16
14
@@ -32,10 +30,10 @@ Level51\JWTUtils\JWTUtils:
32
30
renew_threshold_in_minutes: 60
33
31
```
34
32
35
- Setup the routing for the API. You can modify the name of the routes as
36
- required for the project or use your own classes . At the very least you would
37
- have a project-specific end point which would subclass the ` ApiController ` for
38
- example, ` MyProjectsApi ` .
33
+ Next step is to setup the routing for the API. You can modify the name of the
34
+ routes as required for the project. At the very least you would have a
35
+ project-specific end point which would subclass the ` ApiController ` for example,
36
+ ` MyProjectsApi ` .
39
37
40
38
_ app/\_ config/routes.yml_
41
39
@@ -44,7 +42,6 @@ SilverStripe\Control\Director:
44
42
rules:
45
43
'api/v1/auth/$Action': 'FullscreenInteractive\Restful\Controllers\AuthController'
46
44
'api/v1/projects//$Action': 'MyProjectsApi'
47
- 'api/v1//$Action/': 'FullscreenInteractive\Restful\Controllers\ApiController'
48
45
```
49
46
50
47
Here is an example of ` MyProjectsApi ` which demostrates some of the helpers
@@ -57,6 +54,7 @@ _app/src/Project.php_
57
54
<?php
58
55
59
56
use FullscreenInteractive\Restful\Interfaces\ApiReadable;
57
+ use SilverStripe\Security\Member;
60
58
use SilverStripe\ORM\DataObject;
61
59
62
60
class Project extends DataObject implements ApiReadable
@@ -65,6 +63,18 @@ class Project extends DataObject implements ApiReadable
65
63
'Title' => 'Varchar(100)',
66
64
'Date' => 'DBDate'
67
65
];
66
+
67
+ private static $has_one = [
68
+ 'Author' => Member::class
69
+ ];
70
+
71
+ public function toApi(): array
72
+ {
73
+ return [
74
+ 'title' => $this->Title,
75
+ 'date' => $this->dbObject('Date')->getTimestamp()
76
+ ];
77
+ }
68
78
}
69
79
```
70
80
@@ -91,7 +101,8 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
91
101
public function createProject()
92
102
{
93
103
$this->ensurePOST();
94
- $this->ensureUserLoggedIn([
104
+
105
+ $member = $this->ensureUserLoggedIn([
95
106
'ADMIN'
96
107
]);
97
108
@@ -104,18 +115,20 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
104
115
105
116
$project = new Project();
106
117
$project->Title = $title;
107
- $project->Date = $title;
118
+ $project->Date = $date;
119
+ $project->AuthorID = $member->ID;
108
120
$project->write();
109
121
110
- return $this->success ([
122
+ return $this->returnJSON ([
111
123
'project' => $project->toApi()
112
124
]);
113
125
}
114
126
115
127
public function deleteProject()
116
128
{
117
129
$this->ensurePOST();
118
- $this->ensureUserLoggedIn([
130
+
131
+ $member = $this->ensureUserLoggedIn([
119
132
'ADMIN'
120
133
]);
121
134
@@ -132,8 +145,10 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
132
145
]);
133
146
}
134
147
135
- $project->delete();
136
-
148
+ if ($project->canDelete($member)) {
149
+ $project->delete();
150
+ }
151
+
137
152
return $this->success();
138
153
}
139
154
}
@@ -203,7 +218,10 @@ fetch('/api/v1/projects/createProject', {
203
218
})
204
219
```
205
220
221
+ ## API Documentation
222
+
223
+ Todo but it's not massive. See ` ApiController ` for now.
224
+
206
225
## Licence
207
226
208
227
BSD-3-Clause
209
- ```
0 commit comments