Skip to content

Commit 056db78

Browse files
committed
wip: more app changes
1 parent 00ef984 commit 056db78

File tree

9 files changed

+60
-337
lines changed

9 files changed

+60
-337
lines changed

tests/dummy/app/JsonApi/V1/Posts/PostCollectionQuery.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Posts/PostQuery.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Posts/PostRequest.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace App\JsonApi\V1\Posts;
1313

1414
use App\Models\Post;
15+
use Illuminate\Http\Request;
16+
use Illuminate\Validation\Rule;
1517
use LaravelJsonApi\Core\Schema\Attributes\Model;
1618
use LaravelJsonApi\Eloquent\Fields\DateTime;
1719
use LaravelJsonApi\Eloquent\Fields\ID;
@@ -25,7 +27,6 @@
2527
use LaravelJsonApi\Eloquent\Filters\Scope;
2628
use LaravelJsonApi\Eloquent\Filters\Where;
2729
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
28-
use LaravelJsonApi\Eloquent\Pagination\MultiPagination;
2930
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
3031
use LaravelJsonApi\Eloquent\Schema;
3132
use LaravelJsonApi\Eloquent\SoftDeletes;
@@ -57,18 +58,21 @@ public function fields(): array
5758
ID::make(),
5859
BelongsTo::make('author')->type('users')->readOnly(),
5960
HasMany::make('comments')->canCount()->readOnly(),
60-
Str::make('content'),
61+
Str::make('content')->rules('required'),
6162
DateTime::make('createdAt')->sortable()->readOnly(),
6263
SoftDelete::make('deletedAt')->sortable(),
6364
MorphToMany::make('media', [
6465
BelongsToMany::make('images'),
6566
BelongsToMany::make('videos'),
6667
])->canCount(),
6768
DateTime::make('publishedAt')->sortable(),
68-
Str::make('slug'),
69-
Str::make('synopsis'),
69+
Str::make('slug')
70+
->rules('required')
71+
->creationRules(Rule::unique('posts'))
72+
->updateRules(fn($r, Post $model) => Rule::unique('posts')->ignore($model)),
73+
Str::make('synopsis')->rules('required'),
7074
BelongsToMany::make('tags')->canCount()->mustValidate(),
71-
Str::make('title')->sortable(),
75+
Str::make('title')->sortable()->rules('required'),
7276
DateTime::make('updatedAt')->sortable()->readOnly(),
7377
];
7478
}
@@ -79,9 +83,9 @@ public function fields(): array
7983
public function filters(): array
8084
{
8185
return [
82-
WhereIdIn::make($this)->delimiter(','),
86+
WhereIdIn::make($this)->delimiter(',')->onlyToMany(),
8387
Scope::make('published', 'wherePublished')->asBoolean(),
84-
Where::make('slug')->singular(),
88+
Where::make('slug')->singular()->rules('string'),
8589
OnlyTrashed::make('trashed'),
8690
];
8791
}
@@ -99,15 +103,52 @@ public function sortables(): iterable
99103
/**
100104
* @inheritDoc
101105
*/
102-
public function pagination(): MultiPagination
106+
public function pagination(): PagePagination
103107
{
104-
return new MultiPagination(
105-
PagePagination::make()->withoutNestedMeta(),
106-
PagePagination::make()
107-
->withoutNestedMeta()
108-
->withSimplePagination()
109-
->withPageKey('current-page')
110-
->withPerPageKey('per-page')
111-
);
108+
// TODO add validation to the multi-paginator.
109+
// return new MultiPagination(
110+
// PagePagination::make()->withoutNestedMeta(),
111+
// PagePagination::make()
112+
// ->withoutNestedMeta()
113+
// ->withSimplePagination()
114+
// ->withPageKey('current-page')
115+
// ->withPerPageKey('per-page')
116+
// );
117+
118+
return PagePagination::make()
119+
->withoutNestedMeta()
120+
->withMaxPerPage(200);
121+
}
122+
123+
/**
124+
* @return array
125+
*/
126+
public function deletionRules(): array
127+
{
128+
return [
129+
'meta.no_comments' => 'accepted',
130+
];
131+
}
132+
133+
/**
134+
* @return array
135+
*/
136+
public function deletionMessages(): array
137+
{
138+
return [
139+
'meta.no_comments.accepted' => 'Cannot delete a post with comments.',
140+
];
141+
}
142+
143+
/**
144+
* @param Request|null $request
145+
* @param Post $post
146+
* @return array
147+
*/
148+
public function metaForDeletion(?Request $request, Post $post): array
149+
{
150+
return [
151+
'no_comments' => $post->comments()->doesntExist(),
152+
];
112153
}
113154
}

tests/dummy/app/JsonApi/V1/Users/UserQuery.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)