public async Task <IActionResult> Edit(SeatEditViewModel seatEditViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                var halls = await this.hallsService
                            .GetAllHallsAsync <HallDetailsViewModel>();

                seatEditViewModel.Halls = halls;

                return(this.View(seatEditViewModel));
            }

            await this.seatsService.EditAsync(seatEditViewModel);

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

            var seatEditViewModel = new SeatEditViewModel
            {
                Id        = this.firstSeat.Id,
                Number    = 2,
                RowNumber = 2,
                HallId    = 1,
                Category  = "Invalid category",
            };

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

            Assert.Equal(string.Format(ExceptionMessages.InvalidSeatCategoryType, seatEditViewModel.Category), exception.Message);
        }
        public async Task CheckEditingSeatWithMissingHall()
        {
            this.SeedDatabase();
            await this.SeedSeats();

            var seatEditViewModel = new SeatEditViewModel
            {
                Id        = this.firstSeat.Id,
                Number    = 2,
                RowNumber = 2,
                HallId    = 2,
                Category  = SeatCategory.Wheelchair.ToString(),
            };

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

            Assert.Equal(string.Format(ExceptionMessages.HallNotFound, seatEditViewModel.HallId), exception.Message);
        }
        public async Task CheckIfEditingSeatWorksCorrectly()
        {
            this.SeedDatabase();
            await this.SeedSeats();

            var seatEditViewModel = new SeatEditViewModel
            {
                Id        = this.firstSeat.Id,
                Number    = 2,
                RowNumber = 2,
                HallId    = 1,
                Category  = SeatCategory.Wheelchair.ToString(),
            };

            await this.seatsService.EditAsync(seatEditViewModel);

            Assert.Equal(seatEditViewModel.Number, this.firstSeat.Number);
            Assert.Equal(seatEditViewModel.RowNumber, this.firstSeat.RowNumber);
            Assert.Equal(seatEditViewModel.HallId, this.firstSeat.HallId);
            Assert.Equal(seatEditViewModel.Category, this.firstSeat.Category.ToString());
        }
        public async Task EditAsync(SeatEditViewModel seatEditViewModel)
        {
            if (!Enum.TryParse(seatEditViewModel.Category, true, out SeatCategory seatCategory))
            {
                throw new ArgumentException(
                          string.Format(ExceptionMessages.InvalidSeatCategoryType, seatEditViewModel.Category));
            }

            var seat = await this.seatsRepository
                       .All()
                       .FirstOrDefaultAsync(g => g.Id == seatEditViewModel.Id);

            if (seat == null)
            {
                throw new NullReferenceException(
                          string.Format(ExceptionMessages.SeatNotFound, seatEditViewModel.Id));
            }

            var hall = await this.hallsRepository
                       .All()
                       .FirstOrDefaultAsync(h => h.Id == seatEditViewModel.HallId);

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

            seat.Number     = seatEditViewModel.Number;
            seat.RowNumber  = seatEditViewModel.RowNumber;
            seat.HallId     = seatEditViewModel.HallId;
            seat.Category   = seatCategory;
            seat.ModifiedOn = DateTime.UtcNow;

            this.seatsRepository.Update(seat);
            await this.seatsRepository.SaveChangesAsync();
        }