public async Task <IActionResult> Reserve([FromRoute] string roomId, ReserveRoomInputModel input)
        {
            var pictures = await this.picturesRepository.All().Where(x => x.RoomId == roomId).ToListAsync();

            var room = this.roomsService.GetRoomById(roomId);

            if (!this.ModelState.IsValid || room.NumberOfBeds < input.CountOfPeople)
            {
                this.ModelState.AddModelError("CountOfPeople", GlobalConstants.EnterValidNumberOfGuestsError + room.NumberOfBeds);
                input.Pictures = pictures.Select(x => x.Url).ToList();
                return(this.View(input));
            }

            var userId = await this.usersService.GetUserIdAsync(this.User);

            input.UserId = userId;
            input.RoomId = roomId;

            var result = await this.roomsService.ReserveRoom(input);

            if (result == false)
            {
                this.ModelState.AddModelError("NumberOfNights", GlobalConstants.EnterAtleastOneNightStandsError);
                input.Pictures = pictures.Select(x => x.Url).ToList();
                return(this.View(input));
            }

            this.TempData["InfoMessage"] = GlobalConstants.ReserveRoomTempDataSuccess;

            return(this.Redirect("/Room/Reservations"));
        }
Ejemplo n.º 2
0
        public async Task Reserve_Room_Should_Make_One_Reservation()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            this.dbContext = new ApplicationDbContext(options);

            var roomRepository            = new EfDeletableEntityRepository <Room>(this.dbContext);
            var roomReservationRepository = new EfDeletableEntityRepository <RoomReservation>(this.dbContext);
            var pictureRepository         = new EfDeletableEntityRepository <Picture>(this.dbContext);
            var pictureService            = new PictureService(pictureRepository);
            var moqCloudinaryService      = new Mock <ICloudinaryService>();

            await roomRepository.AddAsync(new Room
            {
                Id                = "1",
                RoomType          = (RoomType)Enum.Parse(typeof(RoomType), "SingleRoom"),
                Price             = 10,
                Pictures          = null,
                HasAirConditioner = true,
                HasBathroom       = true,
                HasHeater         = false,
                HasMountainView   = false,
                HasPhone          = false,
                HasRoomService    = false,
                HasSeaView        = false,
                HasTv             = false,
                HasWifi           = false,
                NumberOfBeds      = 3,
            });

            await roomRepository.SaveChangesAsync();

            this.roomService = new RoomsService(roomRepository, roomReservationRepository, pictureService, moqCloudinaryService.Object);


            var reservation = new ReserveRoomInputModel
            {
                CheckIn       = DateTime.Now.AddDays(1),
                CheckOut      = DateTime.Now.AddDays(3),
                CountOfPeople = 2,
                RoomId        = "1",
                Email         = "*****@*****.**",
                FirstName     = "Marian",
                LastName      = "Kyuchukov",
                PhoneNumber   = "0888186978",
            };

            var actualResult = await this.roomService.ReserveRoom(reservation);

            Assert.True(actualResult);
        }
        public async Task <IActionResult> Reserve([FromRoute] string roomId)
        {
            var inputModel = new ReserveRoomInputModel();

            var pictures = await this.picturesRepository.All().Where(x => x.RoomId == roomId).ToListAsync();

            inputModel.Pictures    = pictures.Select(x => x.Url).ToList();
            inputModel.PhoneNumber = await this.usersService.GetUserPhoneNumberAsync(this.User);

            inputModel.CheckIn  = DateTime.Now;
            inputModel.CheckOut = DateTime.Now;

            return(this.View(inputModel));
        }
Ejemplo n.º 4
0
        public async Task <bool> ReserveRoom(ReserveRoomInputModel input)
        {
            var room = this.repository.All()
                       .FirstOrDefault(r => r.Id == input.RoomId);

            if (room != null && input.CountOfPeople <= room.NumberOfBeds)
            {
                var reservation = new RoomReservation()
                {
                    UserId         = input.UserId,
                    RoomId         = room.Id,
                    RoomType       = room.RoomType,
                    PhoneNumber    = input.PhoneNumber,
                    CheckIn        = input.CheckIn,
                    CheckOut       = input.CheckOut,
                    TotalPrice     = 0,
                    NumberOfGuests = input.CountOfPeople,
                    NumberOfNights = 0,
                };

                var days = (reservation.CheckOut - reservation.CheckIn).TotalDays;

                var totalPrice = ((decimal)days * room.Price) * reservation.NumberOfGuests;

                reservation.NumberOfNights = (int)days;

                reservation.TotalPrice = totalPrice;

                await this.roomReservationRepository.AddAsync(reservation);

                int result = await this.roomReservationRepository.SaveChangesAsync();

                if (reservation.NumberOfNights <= 0)
                {
                    return(false);
                }

                return(true);
            }

            throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionForRoomReservation);
        }