public async Task BookingForTimeBookedSeatReturnsDuplicateResult() { // Arrange FlightBookInfo bookInfo = new FlightBookInfo() { AccountId = 1, BookType = BookType.AwaitingPayment, FlightId = 1, BookTime = DateTimeOffset.Now, SeatBooks = new SeatBook[] { new SeatBook() { SeatId = 1, FlightBookInfoId = 1 } } }; // Act AddResult bookResult = await _bookingService.BookForTimeAsync(bookInfo); // Assert Assert.AreEqual(ResultTypes.Duplicate, bookResult.ResultType); }
public async Task FinalBookingReturnsOkResult() { // Arrange FlightBookInfo bookInfo = new FlightBookInfo() { Id = 1, AccountId = 1, BookType = BookType.AwaitingPayment, FlightId = 1, BookTime = DateTimeOffset.Now, SeatBooks = new SeatBook[] { new SeatBook() { SeatId = 1 } } }; // Act ResultTypes bookResult = await _bookingService.FinalBookAsync(bookInfo.Id.Value, "transaction"); // Assert Assert.AreEqual(ResultTypes.Ok, bookResult); }
public async Task FinalBookingWithoutBookingForTimeReturnsNotFoundResult() { // Arrange FlightBookInfo bookInfo = new FlightBookInfo() { Id = 3, AccountId = 1, FlightId = 1, BookTime = DateTimeOffset.Now, SeatBooks = new SeatBook[] { new SeatBook() { SeatId = 2, FlightBookInfoId = 1 } } }; // Act ResultTypes bookResult = await _bookingService.FinalBookAsync(bookInfo.Id.Value, "transaction"); // Assert Assert.AreEqual(ResultTypes.NotFound, bookResult); }
public async Task BookingForTimeSeatWithNonexistentSeatReturnsNotFoundResult() { // Arrange FlightBookInfo bookInfo = new FlightBookInfo() { Id = 1, AccountId = 1, BookType = BookType.AwaitingPayment, FlightId = 1, BookTime = DateTimeOffset.Now, SeatBooks = new SeatBook[] { new SeatBook() { SeatId = 200 } } }; // Act AddResult bookResult = await _bookingService.BookForTimeAsync(bookInfo); // Assert Assert.AreEqual(ResultTypes.NotFound, bookResult.ResultType); }
public async Task <AddResult> BookForTimeAsync(FlightBookInfo bookInfo) { FlightEntity flight = await _flightRepository.GetByIdAsync(bookInfo.FlightId); if (flight == null) { return(new AddResult(ResultTypes.NotFound, null)); } for (int seatBookIndex = 0; seatBookIndex < bookInfo.SeatBooks.Length; seatBookIndex++) { SeatBook seatBook = bookInfo.SeatBooks[seatBookIndex]; AirplaneSeatEntity seat = await _airplaneRepository.GetSeatById(seatBook.SeatId); if (seat == null) { return(new AddResult(ResultTypes.NotFound, null)); } bool canBook = await _flightRepository.CheckSeatBookAvailabilityAsync( bookInfo.FlightId, seatBook.SeatId ); if (!canBook) { return(new AddResult(ResultTypes.Duplicate, null)); } } bookInfo.BookType = BookType.AwaitingPayment; bookInfo.BookTime = DateTimeOffset.Now; bookInfo.AccountId = _accountId; FlightBookInfoEntity bookInfoDal = _mapper.Map <FlightBookInfoEntity>(bookInfo); int accountFlightInfoId = await _flightRepository.AddAccountFlightInfoAsync(bookInfoDal); foreach (SeatBook seatBook in bookInfo.SeatBooks) { seatBook.FlightBookInfoId = accountFlightInfoId; SeatBookEntity seatBookDal = _mapper.Map <SeatBookEntity>(seatBook); await _flightRepository.BookSeatAsync(seatBookDal); } return(new AddResult(ResultTypes.Ok, accountFlightInfoId));; }
public async Task <IActionResult> BookForTimeAsync([FromBody] FlightBookInfo flightBookInfo) { BlFlightBookInfo flightBookInfoBl = _mapper.Map <BlFlightBookInfo>(flightBookInfo); AddResult bookResult = await _bookingService.BookForTimeAsync(flightBookInfoBl); switch (bookResult.ResultType) { case ResultTypes.Duplicate: return(BadRequest()); case ResultTypes.NotFound: return(NotFound()); } return(Ok(new { Id = bookResult.ItemId })); }