Exemplo n.º 1
0
        public async Task <bool> MakeBooking(FlightBookingRequest bookingRequest, CancellationToken cancellationToken)
        {
            if (bookingRequest == null)
            {
                throw new ArgumentException($"Invalid request", nameof(bookingRequest));
            }
            if (bookingRequest.FlightDate < DateTime.Now.Date || bookingRequest.FlightDate == DateTime.MinValue)
            {
                throw new ArgumentException($"Invalid Flight date {bookingRequest.FlightDate}", nameof(bookingRequest.FlightDate));
            }
            if (bookingRequest.FlightId == Guid.Empty)
            {
                throw new ArgumentException($"Flight Id cannot be empty {bookingRequest.FlightId}", nameof(bookingRequest.FlightId));
            }
            if (string.IsNullOrWhiteSpace(bookingRequest.PassengerName))
            {
                throw new ArgumentException($"Passenger name cannot be empty {bookingRequest.PassengerName}", nameof(bookingRequest.PassengerName));
            }
            var canBook = await this.IsItStillAvailable(bookingRequest.FlightId, bookingRequest.FlightDate, 1, cancellationToken);

            if (!canBook)
            {
                throw new Exception($"No Seats available");
            }
            var bookingRequestDto = this._mapper.Map <FlightBookingsDTO>(bookingRequest);

            return(await this._flightBookingsRepository.MakeBooking(bookingRequestDto, cancellationToken));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> MakeBooking([FromBody] FlightBookingRequest bookingRequest, CancellationToken cancellationToken)
        {
            try
            {
                var result = await this._bookingsService.MakeBooking(bookingRequest, cancellationToken);

                return(new ObjectResult(result));
            }
            catch (ArgumentException ae)
            {
                return(BadRequest(ae));
            }
        }
Exemplo n.º 3
0
            public async Task Test_MakeBooking_Happy()
            {
                //Arrange
                var booking = new FlightBookingRequest
                {
                    FlightId      = Guid.NewGuid(),
                    FlightDate    = DateTime.Now.Date,
                    PassengerName = "Max"
                };

                this.MockRepository(booking.FlightId, booking.FlightDate);

                //Act
                var result = await this.service.MakeBooking(booking, CancellationToken.None);

                //Assert
                Assert.IsTrue(result);
            }