Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer collective, backed by the Pireon OAuth identity server. The public site is a separate, server-rendered application consuming /api/public/v1. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
87 lines
3.5 KiB
PHP
87 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\PublicApi\V1;
|
|
|
|
use App\Http\Resources\PublicApi\PublicProjectResource;
|
|
use App\Models\Project;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
|
|
class ProjectController extends PublicController
|
|
{
|
|
/**
|
|
* GET /api/public/v1/projects
|
|
*/
|
|
public function index(Request $request): AnonymousResourceCollection
|
|
{
|
|
$locale = app()->getLocale();
|
|
|
|
$projects = Project::query()
|
|
// `coverMedia` carries the dimensions and responsive versions of
|
|
// the cover; loaded here so a listing costs one query, not one per
|
|
// project.
|
|
->with(['technologies', 'lead', 'repositories', 'coverMedia', 'lead.avatarMedia', 'client.logoMedia'])
|
|
->withCount([
|
|
'members',
|
|
// Powers the `needs_help` flag without loading every call.
|
|
'calls as open_calls_count' => fn (Builder $q) => $q->open(),
|
|
])
|
|
->unless($this->isPreview($request), fn (Builder $q) => $q->published())
|
|
->when($request->filled('status'), fn (Builder $q) => $q
|
|
->whereIn('status', (array) $request->input('status')))
|
|
->when($request->boolean('featured'), fn (Builder $q) => $q->where('is_featured', true))
|
|
->when($request->boolean('needs_help'), fn (Builder $q) => $q->needingHelp())
|
|
->when($request->filled('technology'), fn (Builder $q) => $q
|
|
->whereHas('technologies', fn (Builder $t) => $t
|
|
->whereIn('slug', (array) $request->input('technology'))))
|
|
->when($request->filled('member'), fn (Builder $q) => $q
|
|
->whereHas('members', fn (Builder $m) => $m
|
|
->where('slug', $request->string('member'))))
|
|
// Only projects that actually exist in this language, so the
|
|
// frontend never renders a listing of fallback content.
|
|
->when($request->boolean('translated'), fn (Builder $q) => $q
|
|
->whereNotNull("title->{$locale}"))
|
|
->orderByDesc('is_featured')
|
|
->orderBy('position')
|
|
->orderByDesc('published_at')
|
|
->paginate($this->perPage($request))
|
|
->withQueryString();
|
|
|
|
return PublicProjectResource::collection($projects);
|
|
}
|
|
|
|
/**
|
|
* GET /api/public/v1/projects/{slug}
|
|
*
|
|
* Resolution is by slug within the active locale — the access pattern an
|
|
* SSR frontend actually has, since it receives a URL, not an id.
|
|
*/
|
|
public function show(Request $request, string $slug): PublicProjectResource
|
|
{
|
|
$project = Project::query()
|
|
->with([
|
|
'technologies',
|
|
'coverMedia',
|
|
'images.media',
|
|
'client.logoMedia',
|
|
'services',
|
|
'testimonials' => fn ($q) => $q->public()->with('avatarMedia')->orderBy('position'),
|
|
'talks' => fn ($q) => $q->public()->with('member')->recentFirst(),
|
|
'lead',
|
|
'lead.avatarMedia',
|
|
'members.technologies',
|
|
'members.avatarMedia',
|
|
'repositories.goodFirstIssues',
|
|
'calls' => fn ($q) => $q->open()->with('technologies'),
|
|
])
|
|
->whereSlug($slug)
|
|
->unless($this->isPreview($request), fn (Builder $q) => $q->published())
|
|
->firstOrFail();
|
|
|
|
return PublicProjectResource::make($project)->detailed();
|
|
}
|
|
}
|