Quite some work done here.
This commit is contained in:
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Game\Controller;
|
||||
|
||||
use App\Game\Service\GameResponseService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -12,6 +13,12 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
#[Route('/game/api', name: 'game_api_')]
|
||||
final class GameApiController extends AbstractController
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
protected GameResponseService $gameResponseService) {
|
||||
|
||||
}
|
||||
|
||||
#[Route('/ping', name: 'ping', methods: ['GET'])]
|
||||
public function ping(): JsonResponse
|
||||
{
|
||||
@@ -22,27 +29,18 @@ final class GameApiController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/echo', name: 'echo', methods: ['POST'])]
|
||||
public function echo(Request $request): JsonResponse
|
||||
#[Route('/message', name: 'message', methods: ['POST'])]
|
||||
public function message(Request $request): JsonResponse
|
||||
{
|
||||
$raw = (string) $request->getContent();
|
||||
$data = null;
|
||||
if ($raw !== '') {
|
||||
try {
|
||||
/** @var array<string,mixed>|null $decoded */
|
||||
$decoded = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
||||
$data = $decoded;
|
||||
} catch (\Throwable $e) {
|
||||
return $this->json([
|
||||
'ok' => false,
|
||||
'error' => 'Invalid JSON: ' . $e->getMessage(),
|
||||
], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
$data = $this->gameResponseService->getGameResponse($raw);
|
||||
}
|
||||
|
||||
return $this->json([
|
||||
'ok' => true,
|
||||
'received' => $data,
|
||||
'result' => $data,
|
||||
'ts' => date('c'),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ final class GameController extends AbstractController
|
||||
#[Route(path: '', name: 'game')]
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->render('game/index.html.twig');
|
||||
return $this->render('game/index.html.twig', [
|
||||
'user_id' => $this->getUser()?->getId(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user