feat: increase gallery grid from 4 to 5 columns per row on desktopfeat: increase gallery grid from 4 to 5 columns per row on desktop
This commit is contained in:
121
app/Http/Controllers/Api/ArtworkAwardController.php
Normal file
121
app/Http/Controllers/Api/ArtworkAwardController.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkAward;
|
||||
use App\Services\ArtworkAwardService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
final class ArtworkAwardController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ArtworkAwardService $service
|
||||
) {}
|
||||
|
||||
/**
|
||||
* POST /api/artworks/{id}/award
|
||||
* Award the artwork with a medal.
|
||||
*/
|
||||
public function store(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$artwork = Artwork::findOrFail($id);
|
||||
|
||||
$this->authorize('award', [ArtworkAward::class, $artwork]);
|
||||
|
||||
$data = $request->validate([
|
||||
'medal' => ['required', 'string', 'in:gold,silver,bronze'],
|
||||
]);
|
||||
|
||||
$award = $this->service->award($artwork, $user, $data['medal']);
|
||||
|
||||
return response()->json(
|
||||
$this->buildPayload($artwork->id, $user->id),
|
||||
201
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT /api/artworks/{id}/award
|
||||
* Change an existing award medal.
|
||||
*/
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$artwork = Artwork::findOrFail($id);
|
||||
|
||||
$existingAward = ArtworkAward::where('artwork_id', $artwork->id)
|
||||
->where('user_id', $user->id)
|
||||
->firstOrFail();
|
||||
|
||||
$this->authorize('change', $existingAward);
|
||||
|
||||
$data = $request->validate([
|
||||
'medal' => ['required', 'string', 'in:gold,silver,bronze'],
|
||||
]);
|
||||
|
||||
$award = $this->service->changeAward($artwork, $user, $data['medal']);
|
||||
|
||||
return response()->json($this->buildPayload($artwork->id, $user->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/artworks/{id}/award
|
||||
* Remove the user's award for this artwork.
|
||||
*/
|
||||
public function destroy(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$artwork = Artwork::findOrFail($id);
|
||||
|
||||
$existingAward = ArtworkAward::where('artwork_id', $artwork->id)
|
||||
->where('user_id', $user->id)
|
||||
->firstOrFail();
|
||||
|
||||
$this->authorize('remove', $existingAward);
|
||||
|
||||
$this->service->removeAward($artwork, $user);
|
||||
|
||||
return response()->json($this->buildPayload($artwork->id, $user->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/artworks/{id}/awards
|
||||
* Return award stats + viewer's current award.
|
||||
*/
|
||||
public function show(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$artwork = Artwork::findOrFail($id);
|
||||
|
||||
return response()->json($this->buildPayload($artwork->id, $request->user()?->id));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// All authorization is delegated to ArtworkAwardPolicy via $this->authorize().
|
||||
|
||||
private function buildPayload(int $artworkId, ?int $userId): array
|
||||
{
|
||||
$stat = \App\Models\ArtworkAwardStat::find($artworkId);
|
||||
|
||||
$userAward = $userId
|
||||
? ArtworkAward::where('artwork_id', $artworkId)
|
||||
->where('user_id', $userId)
|
||||
->value('medal')
|
||||
: null;
|
||||
|
||||
return [
|
||||
'awards' => [
|
||||
'gold' => $stat?->gold_count ?? 0,
|
||||
'silver' => $stat?->silver_count ?? 0,
|
||||
'bronze' => $stat?->bronze_count ?? 0,
|
||||
'score' => $stat?->score_total ?? 0,
|
||||
],
|
||||
'viewer_award' => $userAward,
|
||||
];
|
||||
}
|
||||
}
|
||||
71
app/Http/Controllers/Api/ArtworkNavigationController.php
Normal file
71
app/Http/Controllers/Api/ArtworkNavigationController.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ArtworkResource;
|
||||
use App\Models\Artwork;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ArtworkNavigationController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/artworks/navigation/{id}
|
||||
*
|
||||
* Returns prev/next published artworks by the same author.
|
||||
*/
|
||||
public function neighbors(int $id): JsonResponse
|
||||
{
|
||||
$artwork = Artwork::published()
|
||||
->select(['id', 'user_id', 'title', 'slug'])
|
||||
->find($id);
|
||||
|
||||
if (! $artwork) {
|
||||
return response()->json([
|
||||
'prev_id' => null, 'next_id' => null,
|
||||
'prev_url' => null, 'next_url' => null,
|
||||
'prev_slug' => null, 'next_slug' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
$scope = Artwork::published()
|
||||
->select(['id', 'title', 'slug'])
|
||||
->where('user_id', $artwork->user_id);
|
||||
|
||||
$prev = (clone $scope)->where('id', '<', $id)->orderByDesc('id')->first();
|
||||
$next = (clone $scope)->where('id', '>', $id)->orderBy('id')->first();
|
||||
|
||||
$prevSlug = $prev ? (Str::slug($prev->slug ?: $prev->title) ?: (string) $prev->id) : null;
|
||||
$nextSlug = $next ? (Str::slug($next->slug ?: $next->title) ?: (string) $next->id) : null;
|
||||
|
||||
return response()->json([
|
||||
'prev_id' => $prev?->id,
|
||||
'next_id' => $next?->id,
|
||||
'prev_url' => $prev ? url('/art/' . $prev->id . '/' . $prevSlug) : null,
|
||||
'next_url' => $next ? url('/art/' . $next->id . '/' . $nextSlug) : null,
|
||||
'prev_slug' => $prevSlug,
|
||||
'next_slug' => $nextSlug,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/artworks/{id}/page
|
||||
*
|
||||
* Returns full artwork resource by numeric ID for client-side (no-reload) navigation.
|
||||
*/
|
||||
public function pageData(int $id): JsonResponse
|
||||
{
|
||||
$artwork = Artwork::with(['user.profile', 'categories.contentType', 'tags', 'stats'])
|
||||
->published()
|
||||
->find($id);
|
||||
|
||||
if (! $artwork) {
|
||||
return response()->json(['error' => 'Not found'], 404);
|
||||
}
|
||||
|
||||
$resource = (new ArtworkResource($artwork))->toArray(request());
|
||||
|
||||
return response()->json($resource);
|
||||
}
|
||||
}
|
||||
109
app/Http/Controllers/Api/Search/ArtworkSearchController.php
Normal file
109
app/Http/Controllers/Api/Search/ArtworkSearchController.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Search;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ArtworkListResource;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\Tag;
|
||||
use App\Services\ArtworkSearchService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Artwork search endpoints powered by Meilisearch.
|
||||
*
|
||||
* GET /api/search/artworks?q=&tags[]=&category=&orientation=&sort=
|
||||
* GET /api/search/artworks/tag/{slug}
|
||||
* GET /api/search/artworks/category/{cat}
|
||||
* GET /api/search/artworks/related/{id}
|
||||
*/
|
||||
class ArtworkSearchController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ArtworkSearchService $search
|
||||
) {}
|
||||
|
||||
/**
|
||||
* GET /api/search/artworks
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'q' => ['nullable', 'string', 'max:200'],
|
||||
'tags' => ['nullable', 'array', 'max:10'],
|
||||
'tags.*' => ['string', 'max:80'],
|
||||
'category' => ['nullable', 'string', 'max:80'],
|
||||
'orientation' => ['nullable', 'in:landscape,portrait,square'],
|
||||
'resolution' => ['nullable', 'string', 'max:20'],
|
||||
'author_id' => ['nullable', 'integer', 'min:1'],
|
||||
'sort' => ['nullable', 'string', 'regex:/^(created_at|downloads|likes|views):(asc|desc)$/'],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
]);
|
||||
|
||||
$results = $this->search->search(
|
||||
q: (string) ($validated['q'] ?? ''),
|
||||
filters: array_filter([
|
||||
'tags' => $validated['tags'] ?? [],
|
||||
'category' => $validated['category'] ?? null,
|
||||
'orientation' => $validated['orientation'] ?? null,
|
||||
'resolution' => $validated['resolution'] ?? null,
|
||||
'author_id' => $validated['author_id'] ?? null,
|
||||
'sort' => $validated['sort'] ?? null,
|
||||
]),
|
||||
perPage: (int) ($validated['per_page'] ?? 24),
|
||||
);
|
||||
|
||||
// Eager-load relations needed by ArtworkListResource
|
||||
$results->getCollection()->loadMissing(['user', 'categories.contentType']);
|
||||
|
||||
return ArtworkListResource::collection($results)->response();
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/search/artworks/tag/{slug}
|
||||
*/
|
||||
public function byTag(Request $request, string $slug): JsonResponse
|
||||
{
|
||||
$tag = Tag::where('slug', $slug)->first();
|
||||
|
||||
if (! $tag) {
|
||||
return response()->json(['message' => 'Tag not found.'], 404);
|
||||
}
|
||||
|
||||
$results = $this->search->byTag($slug, (int) $request->query('per_page', 24));
|
||||
|
||||
return response()->json([
|
||||
'tag' => ['id' => $tag->id, 'name' => $tag->name, 'slug' => $tag->slug],
|
||||
'results' => $results,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/search/artworks/category/{cat}
|
||||
*/
|
||||
public function byCategory(Request $request, string $cat): JsonResponse
|
||||
{
|
||||
$results = $this->search->byCategory($cat, (int) $request->query('per_page', 24));
|
||||
|
||||
return response()->json($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/search/artworks/related/{id}
|
||||
*/
|
||||
public function related(int $id): JsonResponse
|
||||
{
|
||||
$artwork = Artwork::with(['tags'])->find($id);
|
||||
|
||||
if (! $artwork) {
|
||||
return response()->json(['message' => 'Artwork not found.'], 404);
|
||||
}
|
||||
|
||||
$results = $this->search->related($artwork, 12);
|
||||
|
||||
return response()->json($results);
|
||||
}
|
||||
}
|
||||
@@ -9,44 +9,49 @@ use App\Http\Requests\Tags\PopularTagsRequest;
|
||||
use App\Http\Requests\Tags\TagSearchRequest;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
final class TagController extends Controller
|
||||
{
|
||||
public function search(TagSearchRequest $request): JsonResponse
|
||||
{
|
||||
$q = (string) ($request->validated()['q'] ?? '');
|
||||
$q = trim($q);
|
||||
$q = trim((string) ($request->validated()['q'] ?? ''));
|
||||
|
||||
$query = Tag::query()->where('is_active', true);
|
||||
if ($q !== '') {
|
||||
$query->where(function ($sub) use ($q): void {
|
||||
$sub->where('name', 'like', $q . '%')
|
||||
->orWhere('slug', 'like', $q . '%');
|
||||
});
|
||||
}
|
||||
// Short results cached for 2 min; empty-query (popular suggestions) for 5 min.
|
||||
$ttl = $q === '' ? 300 : 120;
|
||||
$cacheKey = 'tags.search.' . ($q === '' ? '__empty__' : md5($q));
|
||||
|
||||
$tags = $query
|
||||
->orderByDesc('usage_count')
|
||||
->limit(20)
|
||||
->get(['id', 'name', 'slug', 'usage_count']);
|
||||
$data = Cache::remember($cacheKey, $ttl, function () use ($q): mixed {
|
||||
$query = Tag::query()->where('is_active', true);
|
||||
if ($q !== '') {
|
||||
$query->where(function ($sub) use ($q): void {
|
||||
$sub->where('name', 'like', $q . '%')
|
||||
->orWhere('slug', 'like', $q . '%');
|
||||
});
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $tags,
|
||||
]);
|
||||
return $query
|
||||
->orderByDesc('usage_count')
|
||||
->limit(20)
|
||||
->get(['id', 'name', 'slug', 'usage_count']);
|
||||
});
|
||||
|
||||
return response()->json(['data' => $data]);
|
||||
}
|
||||
|
||||
public function popular(PopularTagsRequest $request): JsonResponse
|
||||
{
|
||||
$limit = (int) ($request->validated()['limit'] ?? 20);
|
||||
$limit = (int) ($request->validated()['limit'] ?? 20);
|
||||
$cacheKey = 'tags.popular.' . $limit;
|
||||
|
||||
$tags = Tag::query()
|
||||
->where('is_active', true)
|
||||
->orderByDesc('usage_count')
|
||||
->limit($limit)
|
||||
->get(['id', 'name', 'slug', 'usage_count']);
|
||||
$data = Cache::remember($cacheKey, 300, function () use ($limit): mixed {
|
||||
return Tag::query()
|
||||
->where('is_active', true)
|
||||
->orderByDesc('usage_count')
|
||||
->limit($limit)
|
||||
->get(['id', 'name', 'slug', 'usage_count']);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => $tags,
|
||||
]);
|
||||
return response()->json(['data' => $data]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user