optimizations
This commit is contained in:
55
app/Http/Controllers/Api/SimilarAiArtworksController.php
Normal file
55
app/Http/Controllers/Api/SimilarAiArtworksController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Artwork;
|
||||
use App\Services\Vision\VectorService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use RuntimeException;
|
||||
|
||||
final class SimilarAiArtworksController extends Controller
|
||||
{
|
||||
public function __construct(private readonly VectorService $vectors)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$artwork = Artwork::query()->public()->published()->find($id);
|
||||
if ($artwork === null) {
|
||||
return response()->json(['error' => 'Artwork not found'], 404);
|
||||
}
|
||||
|
||||
if (! $this->vectors->isConfigured()) {
|
||||
return response()->json([
|
||||
'data' => [],
|
||||
'reason' => 'vector_gateway_not_configured',
|
||||
], 503);
|
||||
}
|
||||
|
||||
$limit = max(1, min(24, (int) $request->query('limit', 12)));
|
||||
|
||||
try {
|
||||
$items = $this->vectors->similarToArtwork($artwork, $limit);
|
||||
} catch (RuntimeException $e) {
|
||||
return response()->json([
|
||||
'data' => [],
|
||||
'reason' => 'vector_gateway_error',
|
||||
'message' => $e->getMessage(),
|
||||
], 502);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $items,
|
||||
'meta' => [
|
||||
'source' => 'vector_gateway',
|
||||
'artwork_id' => $artwork->id,
|
||||
'limit' => $limit,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user