Skip to content

Commit 6a232e0

Browse files
authored
Readme tidy up
1 parent b43c33b commit 6a232e0

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ beats a simple REST solution.
88
Handles authenication and provides common functions for serving and parsing
99
API requests. Compared to `silverstripe-restfulserver` this module does very
1010
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)
1412

1513
## Installation
1614

@@ -32,10 +30,10 @@ Level51\JWTUtils\JWTUtils:
3230
renew_threshold_in_minutes: 60
3331
```
3432

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`.
3937

4038
_app/\_config/routes.yml_
4139

@@ -44,7 +42,6 @@ SilverStripe\Control\Director:
4442
rules:
4543
'api/v1/auth/$Action': 'FullscreenInteractive\Restful\Controllers\AuthController'
4644
'api/v1/projects//$Action': 'MyProjectsApi'
47-
'api/v1//$Action/': 'FullscreenInteractive\Restful\Controllers\ApiController'
4845
```
4946

5047
Here is an example of `MyProjectsApi` which demostrates some of the helpers
@@ -57,6 +54,7 @@ _app/src/Project.php_
5754
<?php
5855
5956
use FullscreenInteractive\Restful\Interfaces\ApiReadable;
57+
use SilverStripe\Security\Member;
6058
use SilverStripe\ORM\DataObject;
6159
6260
class Project extends DataObject implements ApiReadable
@@ -65,6 +63,18 @@ class Project extends DataObject implements ApiReadable
6563
'Title' => 'Varchar(100)',
6664
'Date' => 'DBDate'
6765
];
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+
}
6878
}
6979
```
7080

@@ -91,7 +101,8 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
91101
public function createProject()
92102
{
93103
$this->ensurePOST();
94-
$this->ensureUserLoggedIn([
104+
105+
$member = $this->ensureUserLoggedIn([
95106
'ADMIN'
96107
]);
97108
@@ -104,18 +115,20 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
104115
105116
$project = new Project();
106117
$project->Title = $title;
107-
$project->Date = $title;
118+
$project->Date = $date;
119+
$project->AuthorID = $member->ID;
108120
$project->write();
109121
110-
return $this->success([
122+
return $this->returnJSON([
111123
'project' => $project->toApi()
112124
]);
113125
}
114126
115127
public function deleteProject()
116128
{
117129
$this->ensurePOST();
118-
$this->ensureUserLoggedIn([
130+
131+
$member = $this->ensureUserLoggedIn([
119132
'ADMIN'
120133
]);
121134
@@ -132,8 +145,10 @@ class MyProjectsApi extends FullscreenInteractive\Restful\Controllers\ApiControl
132145
]);
133146
}
134147
135-
$project->delete();
136-
148+
if ($project->canDelete($member)) {
149+
$project->delete();
150+
}
151+
137152
return $this->success();
138153
}
139154
}
@@ -203,7 +218,10 @@ fetch('/api/v1/projects/createProject', {
203218
})
204219
```
205220

221+
## API Documentation
222+
223+
Todo but it's not massive. See `ApiController` for now.
224+
206225
## Licence
207226

208227
BSD-3-Clause
209-
```

0 commit comments

Comments
 (0)