Pass models to events by reference and document Show endpoint

This commit is contained in:
Toby Zerner 2021-01-15 08:35:59 +10:30
parent 9846de5267
commit 497cd364a6
9 changed files with 29 additions and 13 deletions

View File

@ -39,6 +39,7 @@ module.exports = {
collapsable: false, collapsable: false,
children: [ children: [
'list', 'list',
'show',
'create', 'create',
'update', 'update',
'delete', 'delete',

View File

@ -29,7 +29,7 @@ $type->newModel(function (Context $context) {
Run before the model is saved. Run before the model is saved.
```php ```php
$type->onCreating(function ($model, Context $context) { $type->onCreating(function (&$model, Context $context) {
// do something // do something
}); });
``` ```
@ -39,7 +39,7 @@ $type->onCreating(function ($model, Context $context) {
Run after the model is saved. Run after the model is saved.
```php ```php
$type->onCreated(function ($model, Context $context) { $type->onCreated(function (&$model, Context $context) {
$context->meta('foo', 'bar'); $context->meta('foo', 'bar');
}); });
``` ```

View File

@ -19,7 +19,7 @@ $type->deletable(function (Context $context) {
Run before the model is deleted. Run before the model is deleted.
```php ```php
$type->onDeleting(function ($model, Context $context) { $type->onDeleting(function (&$model, Context $context) {
// do something // do something
}); });
``` ```
@ -29,7 +29,7 @@ $type->onDeleting(function ($model, Context $context) {
Run after the model is deleted. Run after the model is deleted.
```php ```php
$type->onDeleted(function ($model, Context $context) { $type->onDeleted(function (&$model, Context $context) {
// do something // do something
}); });
``` ```

15
docs/show.md Normal file
View File

@ -0,0 +1,15 @@
# Showing Resources
For each resource type, a `GET /{type}/{id}` endpoint is exposed to show an individual resource.
## Events
### `onShow`
Run after models and relationships have been retrieved, but before they are serialized into a JSON:API document.
```php
$type->onShow(function (&$model, Context $context) {
// do something
});
```

View File

@ -19,7 +19,7 @@ $type->updatable(function (Context $context) {
Run before the model is saved. Run before the model is saved.
```php ```php
$type->onUpdating(function ($model, Context $context) { $type->onUpdating(function (&$model, Context $context) {
// do something // do something
}); });
``` ```
@ -29,7 +29,7 @@ $type->onUpdating(function ($model, Context $context) {
Run after the model is saved. Run after the model is saved.
```php ```php
$type->onUpdated(function ($model, Context $context) { $type->onUpdated(function (&$model, Context $context) {
// do something // do something
}); });
``` ```

View File

@ -54,11 +54,11 @@ class Create
$this->assertDataValid($data, $model, $context, true); $this->assertDataValid($data, $model, $context, true);
$this->setValues($data, $model, $context); $this->setValues($data, $model, $context);
run_callbacks($schema->getListeners('creating'), [$model, $context]); run_callbacks($schema->getListeners('creating'), [&$model, $context]);
$this->save($data, $model, $context); $this->save($data, $model, $context);
run_callbacks($schema->getListeners('created'), [$model, $context]); run_callbacks($schema->getListeners('created'), [&$model, $context]);
return (new Show($this->api, $this->resource, $model)) return (new Show($this->api, $this->resource, $model))
->handle($context) ->handle($context)

View File

@ -44,7 +44,7 @@ class Delete
throw new ForbiddenException; throw new ForbiddenException;
} }
run_callbacks($schema->getListeners('deleting'), [$this->model, $context]); run_callbacks($schema->getListeners('deleting'), [&$this->model, $context]);
if ($deleteCallback = $schema->getDeleteCallback()) { if ($deleteCallback = $schema->getDeleteCallback()) {
$deleteCallback($this->model, $context); $deleteCallback($this->model, $context);
@ -52,7 +52,7 @@ class Delete
$this->resource->getAdapter()->delete($this->model); $this->resource->getAdapter()->delete($this->model);
} }
run_callbacks($schema->getListeners('deleted'), [$this->model, $context]); run_callbacks($schema->getListeners('deleted'), [&$this->model, $context]);
return new Response(204); return new Response(204);
} }

View File

@ -42,7 +42,7 @@ class Show
$this->loadRelationships([$this->model], $include, $context); $this->loadRelationships([$this->model], $include, $context);
run_callbacks($this->resource->getSchema()->getListeners('show'), [$this->model, $context]); run_callbacks($this->resource->getSchema()->getListeners('show'), [&$this->model, $context]);
$serializer = new Serializer($this->api, $context); $serializer = new Serializer($this->api, $context);
$serializer->add($this->resource, $this->model, $include); $serializer->add($this->resource, $this->model, $include);

View File

@ -52,11 +52,11 @@ class Update
$this->assertDataValid($data, $this->model, $context, false); $this->assertDataValid($data, $this->model, $context, false);
$this->setValues($data, $this->model, $context); $this->setValues($data, $this->model, $context);
run_callbacks($schema->getListeners('updating'), [$this->model, $context]); run_callbacks($schema->getListeners('updating'), [&$this->model, $context]);
$this->save($data, $this->model, $context); $this->save($data, $this->model, $context);
run_callbacks($schema->getListeners('updated'), [$this->model, $context]); run_callbacks($schema->getListeners('updated'), [&$this->model, $context]);
return (new Show($this->api, $this->resource, $this->model)) return (new Show($this->api, $this->resource, $this->model))
->handle($context); ->handle($context);