Initialer Laravel Commit für BetiX
This commit is contained in:
51
app/Http/Controllers/PromoController.php
Normal file
51
app/Http/Controllers/PromoController.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Concerns\ProxiesBackend;
|
||||
use App\Services\BackendHttpClient;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class PromoController extends Controller
|
||||
{
|
||||
use ProxiesBackend;
|
||||
|
||||
public function __construct(private readonly BackendHttpClient $client)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a promo code for the authenticated user via external API.
|
||||
* Request: { code: string }
|
||||
*/
|
||||
public function apply(Request $request)
|
||||
{
|
||||
$data = Validator::make($request->all(), [
|
||||
'code' => ['required','string','max:64'],
|
||||
])->validate();
|
||||
|
||||
$code = strtoupper(trim($data['code']));
|
||||
try {
|
||||
$res = $this->client->post($request, '/promos/apply', [ 'code' => $code ]);
|
||||
if ($res->successful()) {
|
||||
$body = $res->json() ?: [];
|
||||
// Backward compatibility: ensure a message key exists
|
||||
if (!isset($body['message'])) {
|
||||
$body['message'] = 'Promo applied successfully.';
|
||||
}
|
||||
// PromoControllerTest expects { success: true, message: '...' }
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => $body['message']
|
||||
], 200);
|
||||
}
|
||||
if ($res->clientError()) return $this->mapClientError($res);
|
||||
if ($res->serverError()) return $this->mapServiceUnavailable($res);
|
||||
return $this->mapBadGateway();
|
||||
} catch (\Throwable $e) {
|
||||
return $this->mapBadGateway('API server not reachable');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user