Skip to content

Commit e840691

Browse files
committed
feat: implement draft filtering for plugins
1 parent 99a807d commit e840691

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

app/Actions/GetPluginsListData.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ function () use ($plugins): array {
2828
->get()
2929
->pluck('count', 'starrable_id');
3030

31-
return Plugin::query()
31+
return Plugin::with(['author'])
32+
->draft(false)
3233
->when(
3334
$plugins,
3435
fn (EloquentBuilder $query) => $query->whereKey($plugins),
3536
)
36-
->where(
37-
fn (EloquentBuilder $query) => $query->whereNull('is_draft')->orWhere('is_draft', false)
38-
)
3937
->orderByDesc('publish_date')
40-
->with(['author'])
4138
->get()
4239
->map(fn (Plugin $plugin): array => [
4340
...$plugin->getDataArray(),

app/Http/Controllers/Api/PluginController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Models\Plugin;
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Http\Request;
79

810
class PluginController extends Controller
911
{
10-
public function index()
12+
public function index(Request $request)
1113
{
12-
return Plugin::paginate();
14+
return Plugin::query()
15+
->when(
16+
$request->boolean('draft'),
17+
fn (Builder $query, bool $condition) => $query->draft($condition)
18+
)
19+
->paginate();
1320
}
1421

1522
public function show(Plugin $plugin)

app/Models/Plugin.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public function stars(): MorphMany
6969
return $this->morphMany(Star::class, 'starrable');
7070
}
7171

72+
public function scopeDraft(Builder $query, bool $condition = true): Builder
73+
{
74+
if (! $condition) {
75+
return $query->whereNull('is_draft')->orWhere('is_draft', false);
76+
}
77+
78+
return $query->where('is_draft', true);
79+
}
80+
7281
public function getDocUrl(string $version = null): ?string
7382
{
7483
if (filled($this->docs_url)) {

resources/views/plugins/view-plugin.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ class="absolute inset-0 -z-10 h-full w-full -translate-x-1.5 translate-y-1.5 rou
545545
</div>
546546
</div>
547547

548-
@if (count($otherPlugins = $plugin->author->plugins()->where('slug', '!=', $plugin->slug)->inRandomOrder()->limit(3)->get()))
548+
@if (count($otherPlugins = $plugin->author->plugins()->draft(false)->where('slug', '!=', $plugin->slug)->inRandomOrder()->limit(3)->get()))
549549
{{-- More From This Author --}}
550550
<div>
551551
<div class="text-lg font-extrabold">

0 commit comments

Comments
 (0)