public async Task <IActionResult> RegisterAsync([FromBody] RegisterFlight command) { try { if (ModelState.IsValid) { // insert flight Flight flight = Mapper.Map <Flight>(command); _dbContext.Flights.Add(flight); await _dbContext.SaveChangesAsync(); // send event FlightRegistered e = Mapper.Map <FlightRegistered>(command); await _messagePublisher.PublishMessageAsync(e.MessageType, e, ""); // return result return(CreatedAtRoute("GetByFlightId", new { flightId = flight.FlightId }, flight)); } return(BadRequest()); } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
private async Task HandleAsync(FlightRegistered fr) { Flight flight = new Flight { FlightId = fr.FlightId, DepartureDate = fr.DepartureDate, Gate = fr.Gate, CheckInGate = fr.CheckInGate, ArrivalDate = fr.ArrivalDate, City = fr.City, Pilot = fr.Pilot }; await _repo.RegisterFlightAsync(flight); }