12
12
namespace App \JsonApi \V1 \Posts ;
13
13
14
14
use App \Models \Post ;
15
+ use Illuminate \Http \Request ;
16
+ use Illuminate \Validation \Rule ;
15
17
use LaravelJsonApi \Core \Schema \Attributes \Model ;
16
18
use LaravelJsonApi \Eloquent \Fields \DateTime ;
17
19
use LaravelJsonApi \Eloquent \Fields \ID ;
25
27
use LaravelJsonApi \Eloquent \Filters \Scope ;
26
28
use LaravelJsonApi \Eloquent \Filters \Where ;
27
29
use LaravelJsonApi \Eloquent \Filters \WhereIdIn ;
28
- use LaravelJsonApi \Eloquent \Pagination \MultiPagination ;
29
30
use LaravelJsonApi \Eloquent \Pagination \PagePagination ;
30
31
use LaravelJsonApi \Eloquent \Schema ;
31
32
use LaravelJsonApi \Eloquent \SoftDeletes ;
@@ -57,18 +58,21 @@ public function fields(): array
57
58
ID ::make (),
58
59
BelongsTo::make ('author ' )->type ('users ' )->readOnly (),
59
60
HasMany::make ('comments ' )->canCount ()->readOnly (),
60
- Str::make ('content ' ),
61
+ Str::make ('content ' )-> rules ( ' required ' ) ,
61
62
DateTime::make ('createdAt ' )->sortable ()->readOnly (),
62
63
SoftDelete::make ('deletedAt ' )->sortable (),
63
64
MorphToMany::make ('media ' , [
64
65
BelongsToMany::make ('images ' ),
65
66
BelongsToMany::make ('videos ' ),
66
67
])->canCount (),
67
68
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 ' ),
70
74
BelongsToMany::make ('tags ' )->canCount ()->mustValidate (),
71
- Str::make ('title ' )->sortable (),
75
+ Str::make ('title ' )->sortable ()-> rules ( ' required ' ) ,
72
76
DateTime::make ('updatedAt ' )->sortable ()->readOnly (),
73
77
];
74
78
}
@@ -79,9 +83,9 @@ public function fields(): array
79
83
public function filters (): array
80
84
{
81
85
return [
82
- WhereIdIn::make ($ this )->delimiter (', ' ),
86
+ WhereIdIn::make ($ this )->delimiter (', ' )-> onlyToMany () ,
83
87
Scope::make ('published ' , 'wherePublished ' )->asBoolean (),
84
- Where::make ('slug ' )->singular (),
88
+ Where::make ('slug ' )->singular ()-> rules ( ' string ' ) ,
85
89
OnlyTrashed::make ('trashed ' ),
86
90
];
87
91
}
@@ -99,15 +103,52 @@ public function sortables(): iterable
99
103
/**
100
104
* @inheritDoc
101
105
*/
102
- public function pagination (): MultiPagination
106
+ public function pagination (): PagePagination
103
107
{
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
+ ];
112
153
}
113
154
}
0 commit comments