Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,40 @@
'url' => '/', // Relative to current
'description' => 'The current server',
],
// [
// 'url' => '"https://my-server.com:{port}/{basePath}"',
// 'description' => 'Production server',
// 'variables' => [
// 'port' => [
// 'enum' => ['80', '443'],
// 'default' => '443'
// ],
// 'basePath' => [
// 'default' => 'v2',
// 'enum' => ['v1', 'v2'],
// ]
// ]
// ]
// [
// 'url' => '"https://my-server.com:{port}/{basePath}"',
// 'description' => 'Production server',
// 'variables' => [
// 'port' => [
// 'enum' => ['80', '443'],
// 'default' => '443'
// ],
// 'basePath' => [
// 'default' => 'v2',
// 'enum' => ['v1', 'v2'],
// ]
// ]
// ]
],
// See https://spec.openapis.org/oas/v3.1.0#security-scheme-object
'security' => [
// [
// 'type' => 'http',
// 'description' => 'description',
// 'scheme' => 'Bearer',
// 'bearerFormat' => 'JWT'
// ],
// [
// 'type' => 'oauth2',
// 'flows' => [
// 'authorizationCode' => [
// 'scopes' => ['write:pets'],
// 'tokenUrl' => 'https://example.com/api/oauth/token',
// 'authorizationUrl' => 'https://example.com/api/oauth/dialog',
// 'refreshUrl' => 'https://example.com/api/oauth/refresh',
// ]
// ]
// ]
// [
// 'type' => 'http',
// 'description' => 'description',
// 'scheme' => 'Bearer',
// 'bearerFormat' => 'JWT'
// ],
// [
// 'type' => 'oauth2',
// 'flows' => [
// 'authorizationCode' => [
// 'scopes' => ['write:pets'],
// 'tokenUrl' => 'https://example.com/api/oauth/token',
// 'authorizationUrl' => 'https://example.com/api/oauth/dialog',
// 'refreshUrl' => 'https://example.com/api/oauth/refresh',
// ]
// ]
// ]
],
],
];
13 changes: 8 additions & 5 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.4.2/swagger-ui.css" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.4.2/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/vendor/rest/openapi.json',
dom_id: '#swagger-ui',
});
};
window.ui = SwaggerUIBundle({
url: '/vendor/rest/openapi.json',
dom_id: '#swagger-ui',
});
};
</script>
</body>

</html>
6 changes: 1 addition & 5 deletions src/Concerns/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function authorizeTo($ability, $model)
*/
public function authorizedTo($ability, $model)
{
if ($this->isAuthorizingEnabled()) {
return Gate::check($ability, $model);
}

return true;
return ($this->isAuthorizingEnabled()) ? Gate::check($ability, $model) : true;
}
}
21 changes: 14 additions & 7 deletions src/Concerns/PerformsRestOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lomkit\Rest\Concerns;

use Illuminate\Support\Facades\DB;
use Lomkit\Rest\Actions\Action;
use Lomkit\Rest\Contracts\QueryBuilder;
use Lomkit\Rest\Http\Requests\DestroyRequest;
use Lomkit\Rest\Http\Requests\DetailRequest;
Expand Down Expand Up @@ -66,15 +67,21 @@ public function mutate(MutateRequest $request)

DB::beginTransaction();

$operations = app()->make(QueryBuilder::class, ['resource' => $resource, 'query' => null])
->tap(function ($query) use ($request) {
self::newResource()->mutateQuery($request, $query->toBase());
})
->mutate($request->all());
try {
$operations = app()->make(QueryBuilder::class, ['resource' => $resource, 'query' => null])
->tap(function ($query) use ($request) {
self::newResource()->mutateQuery($request, $query->toBase());
})
->mutate($request->all());

DB::commit();
DB::commit();

return $operations;
return $operations;
} catch (\Throwable $th) {
DB::rollBack();

throw new \Exception($th->getMessage(), 500);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/DocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function handle()
*/
protected function getPath($name)
{
return !is_null($this->option('path')) ? $this->option('path').'/'.$name.'.json' : public_path('vendor/rest/'.$name.'.json');
return $this->hasOption('path') ? $this->option('path').'/'.$name.'.json' : public_path('vendor/rest/'.$name.'.json');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Console/stubs/action.stub
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class {{ class }} extends RestAction
]
];
}
}
}
2 changes: 1 addition & 1 deletion src/Console/stubs/instruction.stub
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class {{ class }} extends RestInstruction
]
];
}
}
}
2 changes: 1 addition & 1 deletion src/Http/Requests/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function searchRules(Resource $resource, $prefix = '', $isRootSearchRules
);
}

// @TODO: For now it's prohibited to have more than one nested depth, is this needed ?
/**
* Define the validation rules for filters within the search request.
*
Expand All @@ -68,6 +67,7 @@ public function searchRules(Resource $resource, $prefix = '', $isRootSearchRules
*
* @return array
*/
// @TODO: For now it's prohibited to have more than one nested depth, is this needed ?
public function filtersRules(Resource $resource, string $prefix, $isMaxDepth = false)
{
$rules = array_merge(
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Traits/PerformSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function applyScopes($scopes)
*/
public function instruction($name, $fields = [])
{
$this->resource->instruction(app(RestRequest::class), $name)
return $this->resource->instruction(app(RestRequest::class), $name)
->handle(
collect($fields)->mapWithKeys(function ($field) {return [$field['name'] => $field['value']]; })->toArray(),
$this->queryBuilder
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BelongsToMany extends Relation implements RelationResource
use HasMultipleResults;

/**
* Define validation rules for the BelongsToMany relation.
* Define validation rules for the MorphTo relation.
*
* @param resource $resource The resource associated with the relation.
* @param string $prefix The prefix used for validation rules.
Expand All @@ -37,7 +37,7 @@ public function rules(Resource $resource, string $prefix)
}

/**
* Perform actions after mutating the BelongsToMany relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HasMany extends Relation implements RelationResource
use HasMultipleResults;

/**
* Perform actions after mutating the HasMany relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HasManyThrough extends Relation implements RelationResource
use HasMultipleResults;

/**
* Perform actions after mutating the HasManyThrough relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand All @@ -24,7 +24,7 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
}

/**
* Define validation rules for the HasManyThrough relation.
* Define validation rules for the MorphTo relation.
*
* @param resource $resource The resource associated with the relation.
* @param string $prefix The prefix used for validation rules.
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HasOne extends Relation implements RelationResource
{
/**
* Perform actions after mutating the HasOne relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/HasOneOfMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HasOneOfMany extends Relation implements RelationResource
{
/**
* Perform actions after mutating the HasOneOfMany relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/HasOneThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HasOneThrough extends Relation implements RelationResource
{
/**
* Perform actions after mutating the HasOneThrough relation.
* Perform actions after mutating the MorphTo relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand All @@ -21,7 +21,7 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
}

/**
* Define validation rules for the HasOneThrough relation.
* Define validation rules for the MorphTo relation.
*
* @param resource $resource The resource associated with the relation.
* @param string $prefix The prefix used for validation rules.
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MorphMany extends MorphRelation implements RelationResource
use HasMultipleResults;

/**
* Handle actions after mutating a MorphMany relation.
* Handle actions after mutating a MorphOne relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/MorphedByMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MorphedByMany extends MorphRelation implements RelationResource
use HasMultipleResults;

/**
* Define validation rules for the MorphedByMany relation.
* Define validation rules for the MorphTo relation.
*
* @param resource $resource The resource associated with the relation.
* @param string $prefix The prefix used for validation rules.
Expand All @@ -37,7 +37,7 @@ public function rules(Resource $resource, string $prefix)
}

/**
* Handle actions after mutating a MorphedByMany relation.
* Handle actions after mutating a MorphOne relation.
*
* @param Model $model The Eloquent model.
* @param Relation $relation The relation being mutated.
Expand Down
12 changes: 12 additions & 0 deletions src/Rules/RequiredRelationOnCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public function resource($resource)
return $this;
}

/**
* Determine if the rule is required for the given operation.
*
* @param string $operation
*
* @return bool
*/
protected function isOperationRequired(string $operation)
{
return in_array($operation, ['create']);
}

/**
* Validate the attribute.
*
Expand Down