public async Task <IActionResult> CreateFlight( [FromServices] ICommandHandler <CreateFlightCommand, CreateFlightResult> commandHandler, [FromServices] IQueryHandler <GetFlightDetailsQuery, FlightDetails?> queryHandler, [FromServices] IMapper mapper, [FromBody] CreateFlightInputModel inputModel) { var command = new CreateFlightCommand(inputModel.Capacity); var result = await commandHandler.HandleAsync(command); return(result switch { CreateFlightResult.Created created => CreatedAtAction( nameof(GetFlightById), new { id = created.Id.Value }, null), });
public async Task <IActionResult> Create( [FromServices] ICommandHandler <CreateFlight.Command, CreateFlight.Result> createFlight, [FromBody] CreateFlightInputModel inputModel) { var result = await createFlight.ExecuteAsync(new CreateFlight.Command( inputModel.FromAirport, inputModel.ToAirport, inputModel.DepartureTime, inputModel.ArrivalTime)); switch (result) { case CreateFlight.Result.Success success: return(Ok(new { id = success.FlightId })); default: throw new InvalidProgramException($"Unknown result: {result.GetType()}"); } }