Skip to content

Commit b769705

Browse files
committed
Merge branch '3.x' of github.com:BinarCode/laravel-restify
2 parents fbef25d + f73fa8a commit b769705

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<p align="center"><img src="https://BinarCode.github.io/laravel-restify/assets/img/logo.png"></p>
1+
<p align="center"><img src="http://restify.binarcode.com/assets/img/logo.png"></p>
22

33
<p align="center">
44
<a href="https://github.com/BinarCode/laravel-restify/actions"><img src="https://github.com/BinarCode/laravel-restify/workflows/tests/badge.svg" alt="Build Status"></a>
@@ -21,8 +21,8 @@ composer require binaryk/laravel-restify
2121
## Key havings
2222

2323
- "CRUD" over resources with 0 (zero) extra custom code
24-
- Passport authentication (use `php artisan restify:check-passport` for it's setup)
25-
- Auth module (register, verify, login, reset + forgot password)
24+
- Passport checker (`php artisan restify:check-passport`)
25+
- Auth module with [Laravel Sanctum](https://laravel.com/docs/7.x/sanctum#introduction) (register, verify, login, reset + forgot password)
2626
- Beautiful response maker
2727
- Powerful and configurable searching/filtering over entities
2828
- API friendly Exception Handler
@@ -38,7 +38,7 @@ php artisan restify:setup
3838
Generate repository:
3939

4040
```bash
41-
php artisan restify:repository Post
41+
php artisan restify:repository PostRepository --all
4242
```
4343

4444
## Usage

src/Repositories/Repository.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,14 @@ public function index(RestifyRequest $request)
532532
return static::resolveWith($value);
533533
})->filter(function (self $repository) use ($request) {
534534
return $repository->authorizedToShow($request);
535-
})->values()->map(fn (self $repository) => $repository->serializeForIndex($request));
535+
})->values();
536536

537537
return $this->response([
538-
'meta' => $this->resolveIndexMainMeta($request, $items) ?? RepositoryCollection::meta($paginator->toArray()),
538+
'meta' => $this->resolveIndexMainMeta(
539+
$request, $items->map(fn (self $repository) => $repository->resource),
540+
) ?? RepositoryCollection::meta($paginator->toArray()),
539541
'links' => RepositoryCollection::paginationLinks($paginator->toArray()),
540-
'data' => $items,
542+
'data' => $items->map(fn (self $repository) => $repository->serializeForIndex($request)),
541543
]);
542544
}
543545

src/Services/Search/RepositorySearchService.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
7070

7171
public function prepareOrders(RestifyRequest $request, $query, $extra = [])
7272
{
73-
$sort = $request->get('sort', '');
73+
$orderings = explode(',', $request->get('sort', ''));
7474

7575
if (isset($extra['sort'])) {
76-
$sort = $extra['sort'];
76+
$orderings = $extra['sort'];
7777
}
7878

79-
$params = explode(',', $sort);
79+
$params = array_filter($orderings);
8080

8181
if (is_array($params) === true && empty($params) === false) {
8282
foreach ($params as $param) {
@@ -85,7 +85,7 @@ public function prepareOrders(RestifyRequest $request, $query, $extra = [])
8585
}
8686

8787
if (empty($params) === true) {
88-
$this->setOrder($query, '+id');
88+
$this->setOrder($query, '+'.$this->repository->newModel()->getKeyName());
8989
}
9090

9191
return $query;
@@ -107,6 +107,11 @@ public function prepareRelations(RestifyRequest $request, $query, $extra = [])
107107
public function prepareSearchFields(RestifyRequest $request, $query, $extra = [])
108108
{
109109
$search = $request->get('search', data_get($extra, 'search', ''));
110+
111+
if (empty($search)) {
112+
return $query;
113+
}
114+
110115
$model = $query->getModel();
111116

112117
$query->where(function ($query) use ($search, $model) {
@@ -154,6 +159,8 @@ public function setOrder($query, $param)
154159
$field = $param;
155160
}
156161

162+
$field = $field ?? $this->repository->newModel()->getKeyName();
163+
157164
if (isset($field)) {
158165
if (in_array($field, $this->repository->getOrderByFields()) === true) {
159166
if ($order === '-') {

tests/Controllers/RepositoryIndexControllerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ public function test_index_mergeable_repository_containes_model_attributes_and_l
148148
]],
149149
]);
150150
}
151+
152+
public function test_can_add_custom_index_main_meta_attributes()
153+
{
154+
factory(Post::class)->create([
155+
'title' => 'Post Title',
156+
]);
157+
158+
$response = $this->get('/restify-api/posts')
159+
->assertStatus(200)
160+
->assertJsonStructure([
161+
'meta' => [
162+
'postKey',
163+
],
164+
]);
165+
166+
$this->assertEquals('Custom Meta Value', $response->json('meta.postKey'));
167+
$this->assertEquals('Post Title', $response->json('meta.first_title'));
168+
}
151169
}

tests/Fixtures/Post/PostRepository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binaryk\LaravelRestify\Fields\Field;
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
77
use Binaryk\LaravelRestify\Repositories\Repository;
8+
use Illuminate\Support\Collection;
89

910
class PostRepository extends Repository
1011
{
@@ -58,4 +59,12 @@ public function filters(RestifyRequest $request)
5859
CreatedAfterDateFilter::new(),
5960
];
6061
}
62+
63+
public function resolveIndexMainMeta(RestifyRequest $request, Collection $items)
64+
{
65+
return [
66+
'postKey' => 'Custom Meta Value',
67+
'first_title' => optional($items->first())->title,
68+
];
69+
}
6170
}

0 commit comments

Comments
 (0)