Beispiel #1
0
        public async void BookMovie_ValidDetails_ShowBookedMovie()
        {
            try
            {
                //Arrange
                UserBookingDto userBookingDto =
                    new UserBookingDto()
                {
                    UserId = 1,
                    ShowId = 1,
                    Seat   = new List <SeatDto>()
                    {
                        new SeatDto()
                        {
                            SeatId = 1
                        }, new SeatDto()
                        {
                            SeatId = 2
                        }
                    },
                    MultiplexId = 1,
                    BookingDate = DateTime.Now
                };

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.BookMovie(userBookingDto);

                //Assert
                var okResult = result as OkObjectResult;
                if (okResult != null)
                {
                    Assert.NotNull(okResult);
                }

                var model = okResult.Value as IEnumerable <UserBookingReturnDto>;
                if (model.Count() > 0)
                {
                    Assert.NotNull(model);
                    Assert.NotNull(model?.FirstOrDefault().Id);
                    var expected = userBookingDto?.UserId;
                    var actual   = model?.FirstOrDefault().UserId;

                    Assert.Equal(expected: expected, actual: actual);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
Beispiel #2
0
        public async void BookMovie_InvalidSeatList_NotFound()
        {
            try
            {
                //Arrange
                UserBookingDto userBookingDto =
                    new UserBookingDto()
                {
                    UserId = 1,
                    ShowId = 1,
                    Seat   = new List <SeatDto>()
                    {
                        new SeatDto()
                        {
                            SeatId = 1
                        },
                        new SeatDto()
                        {
                            SeatId = 50
                        }
                    },
                    MultiplexId = 1,
                    BookingDate = DateTime.Now
                };

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.BookMovie(userBookingDto);

                //Assert
                var notFoundResult = result as NotFoundResult;
                if (notFoundResult != null)
                {
                    Assert.NotNull(notFoundResult);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
Beispiel #3
0
        public async void AddShow_ModelValidationFail_BadRequest()
        {
            try
            {
                //Arrange
                UserBookingDto userBookingDto =
                    new UserBookingDto()
                {
                    UserId = 1,
                    ShowId = 1,
                    Seat   = new List <SeatDto>()
                    {
                        new SeatDto()
                        {
                            SeatId = 1
                        },
                        new SeatDto()
                        {
                            SeatId = 2
                        }
                    },
                    BookingDate = DateTime.Now
                };

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.BookMovie(userBookingDto);

                //Assert
                var badRequestResult = result as BadRequestObjectResult;
                if (badRequestResult != null)
                {
                    Assert.NotNull(badRequestResult);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
        public async Task <IActionResult> BookMovie(UserBookingDto bookingDetails)
        {
            bookingDetails.UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (await _repo.GetShow(bookingDetails.ShowId) == null || await _repo.GetMultiplexe(bookingDetails.MultiplexId) == null)
            {
                return(NotFound("Selected Show not found"));
            }

            var availableSeats = _mapper.Map <IEnumerable <SeatDto> >(await _repo.GetSeats(bookingDetails.MultiplexId, bookingDetails.ShowId));

            if (!bookingDetails.Seat.All(x => availableSeats.Any(y => x.SeatId == y.SeatId)))
            {
                return(NotFound("Selected seats are not available"));
            }

            var bookingToRepo = _mapper.Map <Booking>(bookingDetails);

            _repo.Add(bookingToRepo);

            if (await _repo.SaveAll())
            {
                var bookedSeatsToRepo = _mapper.Map <List <BookedSeat> >(bookingDetails.Seat);
                foreach (var bookedSeatToRepo in bookedSeatsToRepo)
                {
                    bookedSeatToRepo.BookingId = bookingToRepo.Id;
                    _repo.Add(bookedSeatToRepo);
                }

                var bookingReturnDto = _mapper.Map <UserBookingReturnDto>(bookingToRepo);

                if (await _repo.SaveAll())
                {
                    return(CreatedAtRoute("GetBookingById", new { controller = "Bookings", id = bookingToRepo.Id }, bookingReturnDto));
                }
            }

            throw new Exception("Booking cannot be completed");
        }