public async Task <IActionResult> Reserve(ConferenceHallInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var userId = await this.usersService.GetUserIdAsync(this.User); input.UserId = userId; var result = await this.conferenceHallService.ReserveConferenceHall(input); var capacity = await this.conferenceHallService .GetAllHallsAsync <ConfHallAllViewModel>(input); if (result == false) { this.ModelState.AddModelError("EventDate", GlobalConstants.FreeSeatsForHallError + capacity); return(this.View(input)); } this.TempData["InfoMessage"] = GlobalConstants.ReserveConferenceHallTempDataSuccess; return(this.Redirect("/ConferenceHall/Reservations")); }
public async Task <int> GetAllHallsAsync <TViewModel>(ConferenceHallInputModel input) { var conferenceHall = this.conferenceHallRepository.All() .Where(x => x.IsDeleted == false && x.EventType == input.EventType) .First(); return(conferenceHall.CurrentCapacity); }
public async Task <IActionResult> Reserve() { var inputModel = new ConferenceHallInputModel { PhoneNumber = await this.usersService.GetUserPhoneNumberAsync(this.User), EventDate = DateTime.Now, }; return(this.View(inputModel)); }
public async Task Reserve_ConferenceHall_Should_Decrease_Halls_Capacity() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; this.dbContext = new ApplicationDbContext(options); var conferenceHallRepository = new EfDeletableEntityRepository <ConferenceHall>(this.dbContext); var conferenceHallReservationRepository = new EfDeletableEntityRepository <ConferenceHallReservation>(this.dbContext); await conferenceHallRepository.AddAsync(new ConferenceHall { Id = "conferenceid", CreatedOn = DateTime.UtcNow, IsDeleted = false, EventType = (ConfHallEventType)Enum.Parse(typeof(ConfHallEventType), "Conference"), Description = "Conference", CurrentCapacity = 100, MaxCapacity = 100, Price = 50, }); await conferenceHallRepository.SaveChangesAsync(); this.conferenceHallService = new ConferenceHallService(conferenceHallReservationRepository, conferenceHallRepository); var reservation = new ConferenceHallInputModel { EventDate = DateTime.Now.AddDays(1), CheckIn = DateTime.Now.AddDays(1).AddHours(10), CheckOut = DateTime.Now.AddDays(1).AddHours(11), NumberOfGuests = 50, EmailAddress = "*****@*****.**", FirstName = "Marian", LastName = "Kyuchukov", PhoneNumber = "0888186978", EventType = (ConfHallEventType)Enum.Parse(typeof(ConfHallEventType), "Conference"), }; var result = await this.conferenceHallService.ReserveConferenceHall(reservation); var parsedEventType = (ConfHallEventType)Enum.Parse(typeof(ConfHallEventType), "Conference"); var actualResult = conferenceHallRepository.All().FirstOrDefault(x => x.EventType == parsedEventType).CurrentCapacity; var expected = 50; Assert.Equal(expected, actualResult); }
public async Task <bool> ReserveConferenceHall(ConferenceHallInputModel input) { var conferenceHall = this.conferenceHallRepository.All() .Where(x => x.IsDeleted == false && x.EventType == input.EventType) .First(); if (conferenceHall != null && input.NumberOfGuests <= conferenceHall.MaxCapacity && input.EventDate.Day >= DateTime.Now.Day) { var conferenceHallReservation = new ConferenceHallReservation() { UserId = input.UserId, PhoneNumber = input.PhoneNumber, NumberOfGuests = input.NumberOfGuests, TotalPrice = 0, EventType = input.EventType, EventDate = input.EventDate, CheckIn = input.CheckIn, CheckOut = input.CheckOut, Message = input.Message, ConferenceHallId = conferenceHall.Id, }; var totalHours = (decimal)(conferenceHallReservation.CheckIn - conferenceHallReservation.CheckOut).TotalHours; var price = Math.Abs(conferenceHall.Price * totalHours); conferenceHallReservation.TotalPrice = price; var allReservationsForDateForHall = this.conferenceHallReservationRepository .All() .Where(x => x.EventDate == input.EventDate && x.EventType == input.EventType); var allReservations = this.conferenceHallReservationRepository.All().Select(x => x.EventDate).ToList(); var allReservationsForType = this.conferenceHallReservationRepository .All().Where(x => x.EventType == input.EventType).ToList(); if (allReservationsForDateForHall.Count() != 0) { foreach (var item in allReservationsForDateForHall) { if (conferenceHallReservation.NumberOfGuests > conferenceHall.CurrentCapacity) { return(false); } } } if (allReservations.Contains(input.EventDate) && allReservationsForType.Count > 0) { conferenceHall.CurrentCapacity -= input.NumberOfGuests; } else { conferenceHall.CurrentCapacity = conferenceHall.MaxCapacity; conferenceHall.CurrentCapacity -= input.NumberOfGuests; } await this.conferenceHallReservationRepository.AddAsync(conferenceHallReservation); await this.conferenceHallReservationRepository.SaveChangesAsync(); return(true); } throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionForConferenceHallReservation); }