Exemple #1
0
        public void BookFlightForNewPassenger_ShouldBookedFlight()
        {
            // Arrange
            var booking = new BookingFlightModel
            {
                BookNumber   = "WO-171576",
                FlightNumber = "PZ696",
                Customer     = new PassengerModel
                {
                    Name      = "Uladislau Pavlowski",
                    DateBirth = DateTime.Parse("1998-11-27T00:00:00"),
                    Gender    = GenderType.Male,
                    Address   = "fake town",
                    Email     = "*****@*****.**"
                },
                Passengers = new[]
                {
                    new PassengerModel
                    {
                        Name      = "Uladislau Pavlowski",
                        DateBirth = DateTime.Parse("1998-11-27T00:00:00"),
                        Gender    = GenderType.Male,
                        Address   = "fake town",
                        Email     = "*****@*****.**"
                    }
                }
            };

            // Act
            var result = _passengersService.BookFlight(booking);

            // Assert
            Assert.Contains(result, _mockBookingRepository.Object.GetAll().ToList());
        }
        public ActionResult <Booking> BookFlight([FromBody] BookingFlightModel model)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = "Model is not valid";
                _logger.LogError(errorMessage, HttpContext.Request.Path);

                return(BadRequest(new { ErrorMessage = errorMessage }));
            }

            var newBooking = _passengersService.BookFlight(model);

            if (newBooking == null)
            {
                var errorMessage = $"Flight № {model.FlightNumber} does not exist";
                _logger.LogError(errorMessage, HttpContext.Request.Path);

                return(BadRequest(new { ErrorMessage = errorMessage }));
            }

            return(CreatedAtAction(nameof(BookFlight), new { id = newBooking.Id }, newBooking));
        }