feat(infrastructure): Enhance router and API endpoint with proper error handling
This commit is contained in:
@@ -8,17 +8,15 @@ use DistributingCarriers\Infrastructure\CassandraEventStore;
|
||||
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
|
||||
use DistributingCarriers\Application\Handlers\RegisterCarrierHandler;
|
||||
|
||||
// Initialize Cassandra Connection
|
||||
$cluster = Cassandra::cluster()
|
||||
->withContactPoints(getenv('CASSANDRA_HOST') ?: 'cassandra')
|
||||
->withPort((int)(getenv('CASSANDRA_PORT') ?: 9042))
|
||||
->build();
|
||||
|
||||
$session = $cluster->connect('event_store');
|
||||
|
||||
// Initialize Infrastructure
|
||||
// Note: In a real app, you'd configure the Cassandra session here.
|
||||
// For this demo, we'll mock it or assume it's passed in.
|
||||
// Since we don't have a running Cassandra instance we can connect to easily without extension,
|
||||
// we will assume the $session is created.
|
||||
// $cluster = Cassandra::cluster()->build();
|
||||
// $session = $cluster->connect('distributing_carriers');
|
||||
|
||||
// Mocking session for demonstration if extension is missing, or just placeholder.
|
||||
$session = null; // Placeholder
|
||||
|
||||
$eventStore = new CassandraEventStore($session);
|
||||
$commandBus = new CommandBus();
|
||||
|
||||
@@ -29,15 +27,22 @@ $commandBus->register(RegisterCarrierCommand::class, new RegisterCarrierHandler(
|
||||
$router = new Router();
|
||||
|
||||
$router->add('POST', '/register-carrier', function () use ($commandBus) {
|
||||
// In a real app, parse body.
|
||||
// For demo, we'll just create a dummy command.
|
||||
$command = new RegisterCarrierCommand("Test Carrier", "test@example.com");
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($input['name']) || !isset($input['email'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields: name, email']);
|
||||
return;
|
||||
}
|
||||
|
||||
$command = new RegisterCarrierCommand($input['name'], $input['email']);
|
||||
$commandBus->dispatch($command);
|
||||
|
||||
http_response_code(201);
|
||||
echo json_encode(['message' => 'Carrier registered successfully']);
|
||||
});
|
||||
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
||||
|
||||
$router->dispatch($method, $uri);
|
||||
|
||||
Reference in New Issue
Block a user