Skip to content
Merged
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
23 changes: 11 additions & 12 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
<?php

use Binaryk\LaravelRestify\Http\Controllers\GlobalSearchController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryAttachController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryDetachController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryFilterController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryIndexController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryAttachController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryShowController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController;
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController;
use Illuminate\Support\Facades\Route;

// Global Search...
Route::get('/search', '\\' . GlobalSearchController::class);
Route::get('/search', '\\'.GlobalSearchController::class);

// Filters
Route::get('/{repository}/filters', '\\' . RepositoryFilterController::class);
Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class);

// API CRUD
Route::get('/{repository}', '\\' . RepositoryIndexController::class);
Route::post('/{repository}', '\\' . RepositoryStoreController::class);
Route::get('/{repository}/{repositoryId}', '\\' . RepositoryShowController::class);
Route::patch('/{repository}/{repositoryId}', '\\' . RepositoryUpdateController::class);
Route::put('/{repository}/{repositoryId}', '\\' . RepositoryUpdateController::class);
Route::delete('/{repository}/{repositoryId}', '\\' . RepositoryDestroyController::class);

Route::get('/{repository}', '\\'.RepositoryIndexController::class);
Route::post('/{repository}', '\\'.RepositoryStoreController::class);
Route::get('/{repository}/{repositoryId}', '\\'.RepositoryShowController::class);
Route::patch('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
Route::put('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
Route::delete('/{repository}/{repositoryId}', '\\'.RepositoryDestroyController::class);

// Attach related repository id
Route::post('/{repository}/{repositoryId}/attach/{relatedRepository}', '\\' . RepositoryAttachController::class);
Route::post('/{repository}/{repositoryId}/detach/{relatedRepository}', '\\' . RepositoryDetachController::class);
Route::post('/{repository}/{repositoryId}/attach/{relatedRepository}', '\\'.RepositoryAttachController::class);
Route::post('/{repository}/{repositoryId}/detach/{relatedRepository}', '\\'.RepositoryDetachController::class);
3 changes: 1 addition & 2 deletions src/Http/Controllers/RepositoryAttachController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __invoke(RepositoryAttachRequest $request)
return $repository->attach(
$request, $request->repositoryId,
collect(Arr::wrap($request->input($request->relatedRepository)))
->map(fn($relatedRepositoryId) => $this->initializePivot(
->map(fn ($relatedRepositoryId) => $this->initializePivot(
$request, $model->{$request->viaRelationship ?? $request->relatedRepository}(), $relatedRepositoryId
))
);
Expand Down Expand Up @@ -61,5 +61,4 @@ protected function initializePivot(RestifyRequest $request, $relationship, $rela

return $pivot;
}

}
3 changes: 1 addition & 2 deletions src/Http/Controllers/RepositoryDetachController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __invoke(RepositoryDetachRequest $request)
return $repository->detach(
$request, $request->repositoryId,
collect(Arr::wrap($request->input($request->relatedRepository)))
->map(fn($relatedRepositoryId) => $this->initializePivot(
->map(fn ($relatedRepositoryId) => $this->initializePivot(
$request, $model->{$request->viaRelationship ?? $request->relatedRepository}(), $relatedRepositoryId
))
);
Expand Down Expand Up @@ -61,5 +61,4 @@ protected function initializePivot(RestifyRequest $request, $relationship, $rela

return $pivot;
}

}
10 changes: 3 additions & 7 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function repository($key = null): ?Repository
]), 404);
}

if (!$repository::authorizedToUseRepository($this)) {
if (! $repository::authorizedToUseRepository($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name. See "allowRestify" policy.', [
'name' => $repository,
]), 403);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function newRepositoryWith($model, $uriKey = null)
*/
public function newQueryWithoutScopes($uriKey = null)
{
if (!$this->isViaRepository()) {
if (! $this->isViaRepository()) {
return $this->model($uriKey)->newQueryWithoutScopes();
}

Expand Down Expand Up @@ -170,15 +170,13 @@ public function findRelatedModelOrFail()
});
}


public function findRelatedQuery($relatedRepository = null, $relatedRepositoryId = null)
{
return $this->repository($relatedRepository ?? request('relatedRepository'))::newModel()
->newQueryWithoutScopes()
->whereKey($relatedRepositoryId ?? request('relatedRepositoryId'));
}


protected function findPivot(RestifyRequest $request, $model)
{
$pivot = $model->{$request->relatedRepository}()->getPivotAccessor();
Expand All @@ -193,7 +191,7 @@ public function viaParentModel()
{
$parent = $this->repository($this->viaRepository);

return once(fn() => $parent::newModel()->newQueryWithoutScopes()->whereKey($this->viaRepositoryId)->firstOrFail());
return once(fn () => $parent::newModel()->newQueryWithoutScopes()->whereKey($this->viaRepositoryId)->firstOrFail());
}

public function viaQuery()
Expand All @@ -217,6 +215,4 @@ public function relatedRepository()
{
return Restify::repositoryForKey($this->relatedRepository);
}


}
1 change: 0 additions & 1 deletion src/Http/Requests/RepositoryAttachRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class RepositoryAttachRequest extends RestifyRequest
{

}
1 change: 0 additions & 1 deletion src/Http/Requests/RepositoryDetachRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class RepositoryDetachRequest extends RestifyRequest
{

}
51 changes: 25 additions & 26 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Fields\FieldCollection;
use Binaryk\LaravelRestify\Filter;
use Binaryk\LaravelRestify\Http\Requests\RepositoryAttachRequest;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Restify;
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
Expand Down Expand Up @@ -182,7 +181,7 @@ public static function newModel(): Model

public static function query(RestifyRequest $request)
{
if (!$request->isViaRepository()) {
if (! $request->isViaRepository()) {
return static::newModel()->query();
}

Expand Down Expand Up @@ -239,8 +238,8 @@ public function collectFields(RestifyRequest $request)

if ($this instanceof Mergeable) {
$fillable = collect($this->resource->getFillable())
->filter(fn($attribute) => $fields->contains('attribute', $attribute) === false)
->map(fn($attribute) => Field::new($attribute));
->filter(fn ($attribute) => $fields->contains('attribute', $attribute) === false)
->map(fn ($attribute) => Field::new($attribute));

$fields = $fields->merge($fillable);
}
Expand All @@ -251,14 +250,14 @@ public function collectFields(RestifyRequest $request)
private function indexFields(RestifyRequest $request): Collection
{
return $this->collectFields($request)
->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, $this))
->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, $this))
->values();
}

private function showFields(RestifyRequest $request): Collection
{
return $this->collectFields($request)
->filter(fn(Field $field) => !$field->isHiddenOnShow($request, $this))
->filter(fn (Field $field) => ! $field->isHiddenOnShow($request, $this))
->values();
}

Expand Down Expand Up @@ -358,10 +357,10 @@ public static function routes(Router $router, $attributes, $wrap = false)
public function resolveShowAttributes(RestifyRequest $request)
{
$fields = $this->showFields($request)
->filter(fn(Field $field) => $field->authorize($request))
->each(fn(Field $field) => $field->resolveForShow($this))
->map(fn(Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn($value) => $value)
->filter(fn (Field $field) => $field->authorize($request))
->each(fn (Field $field) => $field->resolveForShow($this))
->map(fn (Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn ($value) => $value)
->all();

if ($this instanceof Mergeable) {
Expand All @@ -379,7 +378,7 @@ public function resolveShowAttributes(RestifyRequest $request)
return false;
}

if (!$field->authorize($request)) {
if (! $field->authorize($request)) {
return false;
}

Expand All @@ -400,10 +399,10 @@ public function resolveIndexAttributes($request)
{
// Resolve the show method, and attach the value to the array
$fields = $this->indexFields($request)
->filter(fn(Field $field) => $field->authorize($request))
->each(fn(Field $field) => $field->resolveForIndex($this))
->map(fn(Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn($value) => $value)
->filter(fn (Field $field) => $field->authorize($request))
->each(fn (Field $field) => $field->resolveForIndex($this))
->map(fn (Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn ($value) => $value)
->all();

if ($this instanceof Mergeable) {
Expand All @@ -421,7 +420,7 @@ public function resolveIndexAttributes($request)
return false;
}

if (!$field->authorize($request)) {
if (! $field->authorize($request)) {
return false;
}

Expand Down Expand Up @@ -467,7 +466,7 @@ public function resolveRelationships($request): array
/** * @var AbstractPaginator $paginator */
$paginator = $this->resource->{$relation}()->paginate($request->get('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE));

$withs[$relation] = $paginator->getCollection()->map(fn(Model $item) => [
$withs[$relation] = $paginator->getCollection()->map(fn (Model $item) => [
'attributes' => $item->toArray(),
]);
}
Expand Down Expand Up @@ -519,7 +518,7 @@ public function index(RestifyRequest $request)
return static::resolveWith($value);
})->filter(function (self $repository) use ($request) {
return $repository->authorizedToShow($request);
})->values()->map(fn(self $repository) => $repository->serializeForIndex($request));
})->values()->map(fn (self $repository) => $repository->serializeForIndex($request));

return $this->response([
'meta' => RepositoryCollection::meta($paginator->toArray()),
Expand Down Expand Up @@ -547,7 +546,7 @@ public function store(RestifyRequest $request)
$this->resource->save();
}

$this->storeFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
$this->storeFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));
});

static::stored($this->resource, $request);
Expand Down Expand Up @@ -578,8 +577,8 @@ public function update(RestifyRequest $request, $repositoryId)
public function attach(RestifyRequest $request, $repositoryId, Collection $pivots)
{
DB::transaction(function () use ($request, $pivots) {
return $pivots->map(fn($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
->map(fn($pivot) => $pivot->save());
return $pivots->map(fn ($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
->map(fn ($pivot) => $pivot->save());
});

return $this->response()
Expand All @@ -590,7 +589,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot
public function detach(RestifyRequest $request, $repositoryId, Collection $pivots)
{
$deleted = DB::transaction(function () use ($request, $pivots) {
return $pivots->map(fn($pivot) => $pivot->delete());
return $pivots->map(fn ($pivot) => $pivot->delete());
});

return $this->response()
Expand Down Expand Up @@ -668,7 +667,7 @@ public function response($content = '', $status = 200, array $headers = []): Res
public function serializeForShow(RestifyRequest $request): array
{
return $this->filter([
'id' => $this->when($this->resource->id, fn() => $this->getShowId($request)),
'id' => $this->when($this->resource->id, fn () => $this->getShowId($request)),
'type' => $this->when($type = $this->getType($request), $type),
'attributes' => $request->isShowRequest() ? $this->resolveShowAttributes($request) : $this->resolveIndexAttributes($request),
'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related),
Expand All @@ -681,7 +680,7 @@ public function serializeForIndex(RestifyRequest $request): array
return $this->filter([
'id' => $this->when($id = $this->getShowId($request), $id),
'type' => $this->when($type = $this->getType($request), $type),
'attributes' => $this->when((bool)$attrs = $this->resolveIndexAttributes($request), $attrs),
'attributes' => $this->when((bool) $attrs = $this->resolveIndexAttributes($request), $attrs),
'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related),
'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta),
]);
Expand Down Expand Up @@ -724,12 +723,12 @@ protected static function fillFields(RestifyRequest $request, Model $model, Coll

public static function uriTo(Model $model)
{
return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey();
return Restify::path().'/'.static::uriKey().'/'.$model->getKey();
}

public function availableFilters(RestifyRequest $request)
{
return collect($this->filter($this->filters($request)))->each(fn(Filter $filter) => $filter->authorizedToSee($request))
return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request))
->values();
}
}
6 changes: 3 additions & 3 deletions tests/Controllers/RepositoryAttachControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function test_attach_a_user_to_a_company()
$user = $this->mockUsers(2)->first();
$company = factory(Company::class)->create();

$response = $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [
$response = $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [
'users' => $user->id,
'is_admin' => true,
])
Expand All @@ -32,7 +32,7 @@ public function test_attach_multiple_users_to_a_company()
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(0, $usersFromCompany->json('data'));

$response = $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [
$response = $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [
'users' => [1, 2],
'is_admin' => true,
])
Expand All @@ -56,7 +56,7 @@ public function test_after_attach_a_user_to_company_number_of_users_increased()
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(0, $usersFromCompany->json('data'));

$this->postJson('restify-api/companies/' . $company->id . '/attach/users', [
$this->postJson('restify-api/companies/'.$company->id.'/attach/users', [
'users' => $user->id,
]);

Expand Down
7 changes: 3 additions & 4 deletions tests/Controllers/RepositoryDetachControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function test_detach_a_user_from_a_company()
$company->users()->attach($user->id);
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(1, $usersFromCompany->json('data'));
$this->postJson('restify-api/companies/' . $company->id . '/detach/users', [
$this->postJson('restify-api/companies/'.$company->id.'/detach/users', [
'users' => $user->id,
])
->assertStatus(204);
Expand All @@ -31,13 +31,12 @@ public function test_detach_multiple_users_from_a_company()
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(3, $usersFromCompany->json('data'));

$this->postJson('restify-api/companies/' . $company->id . '/detach/users', [
'users' => [1, 2]
$this->postJson('restify-api/companies/'.$company->id.'/detach/users', [
'users' => [1, 2],
])
->assertStatus(204);

$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(1, $usersFromCompany->json('data'));
}

}
2 changes: 1 addition & 1 deletion tests/Fixtures/Company/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function users()
{
return $this->belongsToMany(User::class, 'company_user', 'company_id', 'user_id')
->withPivot([
'is_admin'
'is_admin',
])
->withTimestamps();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function posts()
public function companies()
{
return $this->belongsToMany(Company::class, 'company_user', 'user_id', 'company_id')->withPivot([
'is_admin'
'is_admin',
])->withTimestamps();
}

Expand Down