60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Exceptions\MissingRewardedAdUnit;
|
|
use App\Exceptions\RewardedAdUnavailable;
|
|
use App\Http\Requests\RewardedAdSessionRequest;
|
|
use App\Models\RewardedAdSession;
|
|
use App\Services\GenerationCreditService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class GenerationCreditController extends Controller
|
|
{
|
|
public function show(Request $request, GenerationCreditService $credits): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'data' => $credits->summary($request->user()),
|
|
]);
|
|
}
|
|
|
|
public function storeRewardedAdSession(
|
|
RewardedAdSessionRequest $request,
|
|
GenerationCreditService $credits,
|
|
): JsonResponse {
|
|
try {
|
|
$session = $credits->createRewardedAdSession(
|
|
$request->user(),
|
|
$request->validated('platform'),
|
|
);
|
|
} catch (RewardedAdUnavailable $exception) {
|
|
return response()->json([
|
|
'message' => $exception->getMessage(),
|
|
], 422);
|
|
} catch (MissingRewardedAdUnit $exception) {
|
|
return response()->json([
|
|
'message' => $exception->getMessage(),
|
|
], 503);
|
|
}
|
|
|
|
return response()->json([
|
|
'data' => $this->rewardedAdSessionPayload($session),
|
|
], $session->wasRecentlyCreated ? 201 : 200);
|
|
}
|
|
|
|
/**
|
|
* @return array{id: string, adUnitId: ?string, customData: string, userId: string, expiresAt: string}
|
|
*/
|
|
private function rewardedAdSessionPayload(RewardedAdSession $session): array
|
|
{
|
|
return [
|
|
'id' => $session->id,
|
|
'adUnitId' => $session->ad_unit_id,
|
|
'customData' => $session->custom_data,
|
|
'userId' => $session->user_id,
|
|
'expiresAt' => $session->expires_at->toIso8601String(),
|
|
];
|
|
}
|
|
}
|