Current state
This commit is contained in:
64
app/Http/Controllers/ArtworkController.php
Normal file
64
app/Http/Controllers/ArtworkController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ArtworkIndexRequest;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ArtworkController extends Controller
|
||||
{
|
||||
/**
|
||||
* Browse artworks with optional category filtering.
|
||||
* Uses cursor pagination (no offset pagination) and only returns public, approved, not-deleted items.
|
||||
*/
|
||||
public function index(ArtworkIndexRequest $request, ?Category $category = null): View
|
||||
{
|
||||
$perPage = (int) ($request->get('per_page', 24));
|
||||
|
||||
$query = Artwork::public()->published();
|
||||
|
||||
if ($category) {
|
||||
$query->whereHas('categories', function ($q) use ($category) {
|
||||
$q->where('categories.id', $category->id);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('q')) {
|
||||
$q = $request->get('q');
|
||||
$query->where(function ($sub) use ($q) {
|
||||
$sub->where('title', 'like', '%' . $q . '%')
|
||||
->orWhere('description', 'like', '%' . $q . '%');
|
||||
});
|
||||
}
|
||||
|
||||
$sort = $request->get('sort', 'latest');
|
||||
if ($sort === 'oldest') {
|
||||
$query->orderBy('published_at', 'asc');
|
||||
} else {
|
||||
$query->orderByDesc('published_at');
|
||||
}
|
||||
|
||||
// Important: do NOT eager-load artwork_stats in listings
|
||||
$artworks = $query->cursorPaginate($perPage);
|
||||
|
||||
return view('artworks.index', [
|
||||
'artworks' => $artworks,
|
||||
'category' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a single artwork by slug. Ensure it's public, approved and not deleted.
|
||||
*/
|
||||
public function show(Artwork $artwork): View
|
||||
{
|
||||
if (! $artwork->is_public || ! $artwork->is_approved || $artwork->trashed()) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return view('artworks.show', ['artwork' => $artwork]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user