public async Task <IActionResult> Create()
        {
            var halls = await this.hallsService
                        .GetAllHallsAsync <HallDetailsViewModel>();

            var viewModel = new SeatCreateInputModel
            {
                Halls = halls,
            };

            return(this.View(viewModel));
        }
        public async Task <IActionResult> Create(SeatCreateInputModel seatCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                var halls = await this.hallsService
                            .GetAllHallsAsync <HallDetailsViewModel>();

                seatCreateInputModel.Halls = halls;
                return(this.View(seatCreateInputModel));
            }

            await this.seatsService.CreateAsync(seatCreateInputModel);

            return(this.RedirectToAction("GetAll", "Seats", new { area = "Administration" }));
        }
        public async Task CheckAddingSeatWithInvalidSeatCategoryType()
        {
            this.SeedDatabase();

            var seat = new SeatCreateInputModel
            {
                Number    = 2,
                RowNumber = 2,
                HallId    = 1,
                Category  = "Invalid category",
            };

            var exception = await Assert
                            .ThrowsAsync <ArgumentException>(async() => await this.seatsService.CreateAsync(seat));

            Assert.Equal(string.Format(ExceptionMessages.InvalidSeatCategoryType, seat.Category), exception.Message);
        }
        public async Task CheckAddingSeatWithMissingHall()
        {
            this.SeedDatabase();

            var seat = new SeatCreateInputModel
            {
                Number    = 2,
                RowNumber = 2,
                HallId    = 2,
                Category  = SeatCategory.Normal.ToString(),
            };

            var exception = await Assert
                            .ThrowsAsync <NullReferenceException>(async() => await this.seatsService.CreateAsync(seat));

            Assert.Equal(string.Format(ExceptionMessages.HallNotFound, seat.HallId), exception.Message);
        }
        public async Task <SeatDetailsViewModel> CreateAsync(SeatCreateInputModel seatCreateInputModel)
        {
            if (!Enum.TryParse(seatCreateInputModel.Category, true, out SeatCategory seatCategory))
            {
                throw new ArgumentException(
                          string.Format(ExceptionMessages.InvalidSeatCategoryType, seatCreateInputModel.Category));
            }

            var hall = await this.hallsRepository
                       .All()
                       .FirstOrDefaultAsync(d => d.Id == seatCreateInputModel.HallId);

            if (hall == null)
            {
                throw new NullReferenceException(
                          string.Format(ExceptionMessages.HallNotFound, seatCreateInputModel.HallId));
            }

            var seat = new Seat
            {
                Number      = seatCreateInputModel.Number,
                RowNumber   = seatCreateInputModel.RowNumber,
                Category    = seatCategory,
                IsAvailable = true,
                Hall        = hall,
            };

            bool doesSeatExist = await this.seatsRepository
                                 .All()
                                 .AnyAsync(x => x.Number == seat.Number && x.RowNumber == seat.RowNumber);

            if (doesSeatExist)
            {
                throw new ArgumentException(
                          string.Format(ExceptionMessages.SeatAlreadyExists, seat.Number, seat.RowNumber));
            }

            await this.seatsRepository.AddAsync(seat);

            await this.seatsRepository.SaveChangesAsync();

            var viewModel = await this.GetViewModelByIdAsync <SeatDetailsViewModel>(seat.Id);

            return(viewModel);
        }
        public async Task CheckIfAddingSeatWorksCorrectly()
        {
            this.SeedDatabase();

            var model = new SeatCreateInputModel
            {
                Number    = 1,
                RowNumber = 1,
                HallId    = 1,
                Category  = SeatCategory.Normal.ToString(),
            };

            await this.seatsService.CreateAsync(model);

            var count = await this.seatsRepository.All().CountAsync();

            Assert.Equal(1, count);
        }
        public async Task CheckAddingAlreadyExistingSeat()
        {
            this.SeedDatabase();
            await this.SeedSeats();

            var seat = new SeatCreateInputModel
            {
                Number    = 1,
                RowNumber = 1,
                HallId    = 1,
                Category  = SeatCategory.Normal.ToString(),
            };

            var exception = await Assert
                            .ThrowsAsync <ArgumentException>(async() => await this.seatsService.CreateAsync(seat));

            Assert.Equal(string.Format(ExceptionMessages.SeatAlreadyExists, seat.Number, seat.RowNumber), exception.Message);
        }
        public async Task CheckIfAddingSeatReturnsViewModel()
        {
            this.SeedDatabase();

            var seat = new SeatCreateInputModel
            {
                Number    = 3,
                RowNumber = 3,
                HallId    = 1,
                Category  = SeatCategory.Normal.ToString(),
            };

            var viewModel = await this.seatsService.CreateAsync(seat);

            var dbEntry = await this.seatsRepository.All().FirstOrDefaultAsync();

            Assert.Equal(dbEntry.Id, viewModel.Id);
            Assert.Equal(dbEntry.Number, viewModel.Number);
            Assert.Equal(dbEntry.RowNumber, viewModel.RowNumber);
            Assert.Equal(dbEntry.HallId, viewModel.Hall.Id);
            Assert.Equal(dbEntry.Category, viewModel.Category);
        }
        public async Task CheckSettingsOfSeatProperties()
        {
            this.SeedDatabase();
            await this.SeedSeats();

            var model = new SeatCreateInputModel
            {
                Number    = 2,
                RowNumber = 2,
                HallId    = 1,
                Category  = SeatCategory.Wheelchair.ToString(),
            };

            await this.seatsService.CreateAsync(model);

            var seat = await this.seatsRepository.All().FirstOrDefaultAsync();

            Assert.Equal(this.firstSeat.Id, seat.Id);
            Assert.Equal(this.firstSeat.Number, seat.Number);
            Assert.Equal(this.firstSeat.RowNumber, seat.RowNumber);
            Assert.Equal(this.firstSeat.HallId, seat.HallId);
            Assert.Equal(this.firstSeat.Category, seat.Category);
        }