Skip to content

Commit

Permalink
feat: implement draft filtering for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
saade committed Jan 7, 2025
1 parent 99a807d commit e840691
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
7 changes: 2 additions & 5 deletions app/Actions/GetPluginsListData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ function () use ($plugins): array {
->get()
->pluck('count', 'starrable_id');

return Plugin::query()
return Plugin::with(['author'])
->draft(false)
->when(
$plugins,
fn (EloquentBuilder $query) => $query->whereKey($plugins),
)
->where(
fn (EloquentBuilder $query) => $query->whereNull('is_draft')->orWhere('is_draft', false)
)
->orderByDesc('publish_date')
->with(['author'])
->get()
->map(fn (Plugin $plugin): array => [
...$plugin->getDataArray(),
Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/Api/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

use App\Http\Controllers\Controller;
use App\Models\Plugin;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class PluginController extends Controller
{
public function index()
public function index(Request $request)
{
return Plugin::paginate();
return Plugin::query()
->when(
$request->boolean('draft'),
fn (Builder $query, bool $condition) => $query->draft($condition)
)
->paginate();
}

public function show(Plugin $plugin)
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function stars(): MorphMany
return $this->morphMany(Star::class, 'starrable');
}

public function scopeDraft(Builder $query, bool $condition = true): Builder
{
if (! $condition) {
return $query->whereNull('is_draft')->orWhere('is_draft', false);
}

return $query->where('is_draft', true);
}

public function getDocUrl(string $version = null): ?string
{
if (filled($this->docs_url)) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/plugins/view-plugin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ class="absolute inset-0 -z-10 h-full w-full -translate-x-1.5 translate-y-1.5 rou
</div>
</div>

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

0 comments on commit e840691

Please sign in to comment.