76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Artworks\ArtworkCreateRequest;
|
|
use App\Http\Resources\ArtworkListResource;
|
|
use App\Http\Resources\ArtworkResource;
|
|
use App\Services\ArtworkService;
|
|
use App\Services\Artworks\ArtworkDraftService;
|
|
use App\Models\Category;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ArtworkController extends Controller
|
|
{
|
|
protected ArtworkService $service;
|
|
|
|
public function __construct(ArtworkService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
/**
|
|
* POST /api/artworks
|
|
* Creates a draft artwork placeholder for the upload pipeline.
|
|
*/
|
|
public function store(ArtworkCreateRequest $request, ArtworkDraftService $drafts)
|
|
{
|
|
$user = $request->user();
|
|
$data = $request->validated();
|
|
|
|
$categoryId = isset($data['category']) && ctype_digit((string) $data['category'])
|
|
? (int) $data['category']
|
|
: null;
|
|
|
|
$result = $drafts->createDraft(
|
|
(int) $user->id,
|
|
(string) $data['title'],
|
|
isset($data['description']) ? (string) $data['description'] : null,
|
|
$categoryId
|
|
);
|
|
|
|
return response()->json([
|
|
'artwork_id' => $result->artworkId,
|
|
'status' => $result->status,
|
|
], Response::HTTP_CREATED);
|
|
}
|
|
|
|
/**
|
|
* GET /api/v1/artworks/{slug}
|
|
* Returns a single public artwork resource by slug.
|
|
*/
|
|
public function show(string $slug)
|
|
{
|
|
$artwork = $this->service->getPublicArtworkBySlug($slug);
|
|
|
|
// Return the artwork instance (service already loads lightweight relations).
|
|
// Log resolved resource for debugging failing test assertions.
|
|
// Return the resolved payload directly to avoid JsonResource wrapping inconsistencies
|
|
return response()->json((new ArtworkResource($artwork))->resolve(), 200);
|
|
}
|
|
|
|
/**
|
|
* GET /api/v1/categories/{slug}/artworks
|
|
* Uses route-model binding for Category (slug). Returns paginated list resource.
|
|
*/
|
|
public function categoryArtworks(Request $request, Category $category)
|
|
{
|
|
$perPage = (int) $request->get('per_page', 24);
|
|
|
|
$paginator = $this->service->getCategoryArtworks($category, $perPage);
|
|
|
|
return ArtworkListResource::collection($paginator);
|
|
}
|
|
}
|