public async Task <ActionResult <CreateReservationDTO> > PostReservation(CreateReservationDTO reservationDTO) { if (reservationDTO == null) { return(BadRequest(new { Message = "No reservation input" })); } //TODO: check if reservation with flightId and userId exists. if it does do put, else continu post. try { Flight flight = await flightRepo.GetAsyncByGuid(reservationDTO.FlightId); if (flight != null) { Reservation reservation = mapper.Map <Reservation>(reservationDTO); foreach (ReservedSeat reservedSeat in reservation.ReservedSeats) { PriceClass price = await genericPriceRepo.GetAsyncByGuid(reservedSeat.PriceId); if (price != null) { Seat seat = await genericSeatRepo.GetAsyncByGuid(reservedSeat.SeatId); seat.Reserved = true; await genericSeatRepo.Update(seat, seat.Id); reservedSeat.TicketPrice = price.Value != 0 && flight.DistanceInKm != 0 ? price.Value * (flight.DistanceInKm / 1000) : 0; reservedSeat.PersonId = reservedSeat.Person.Id; reservation.TotalPrice += reservedSeat.TicketPrice; reservation.TotalSeats += 1; flight.Airplane = await airplaneRepo.GetAsyncByGuid(flight.AirplaneId.Value); flight.Airplane.ReservedSeats += 1; if (flight.Airplane.ReservedSeats == flight.Airplane.TotalSeats) { //TODO: realtime message to admin Console.WriteLine("Airplane is fully booked."); MessageObject message = new MessageObject() { Message = $"Airplane {flight.Airplane.Name} on flight with Id {flight.Id} is fully booked. Please add an extra plane." }; await sender.Send(message); } } else { return(RedirectToAction("HandleErrorCode", "Error", new { statusCode = 404, errorMessage = $"Could not find price. " })); } } var createdReservation = await reservationRepo.Create(reservation); if (createdReservation == null) { return(BadRequest(new { Message = $"Reservation could not be saved" })); } return(Created("/api/reservations", reservationDTO)); } else { return(RedirectToAction("HandleErrorCode", "Error", new { statusCode = 404, errorMessage = $"Could not find flight. " })); } } catch (Exception ex) { return(RedirectToAction("HandleErrorCode", "Error", new { statusCode = 400, errorMessage = $"Creating reservation failed {ex}" })); } }