public async Task <IActionResult> Create(string city, string restaurant, CreateReservationBindingModel viewModel)
        {
            var dateTime    = DateTime.Parse(viewModel.Date + " " + viewModel.Time);
            var isDateValid = await reservationsService.IsDateValid(dateTime);

            if (isDateValid)
            {
                RestaurantServiceModel restaurantFromDb = await restaurantService.GetRestaurantByNameAndCity(city, restaurant);

                ReserveTableUserServiceModel user = await usersService.GetUserByUsername(this.User.Identity.Name);

                ReservationServiceModel reservation = await reservationsService.MakeReservation(viewModel, user, restaurantFromDb);

                if (reservation == null)
                {
                    ModelState.AddModelError("NoAvailableTables", "There are no available tables.");

                    return(this.View());
                }

                return(this.Redirect("/Reservations/My"));
            }
            else
            {
                ModelState.AddModelError("InvalidData", "Your date is not valid.");
            }

            return(this.View());
        }
Example #2
0
        public async Task <ReserveTableUserServiceModel> GetUserById(string id)
        {
            var user = await dbContext.Users.FindAsync(id);

            ReserveTableUserServiceModel userServiceModel = AutoMapper.Mapper.Map <ReserveTableUserServiceModel>(user);

            return(userServiceModel);
        }
        public async Task GetUserById_WithNonExistentId_ShouldReturnNull()
        {
            string errorMessage = "UserService GetById() method does not work properly.";

            var context = ReserveTableDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.userService = new UserService(context);

            ReserveTableUserServiceModel actualData = await this.userService.GetUserById("invalid");

            Assert.True(actualData == null, errorMessage);
        }
Example #4
0
        public async Task MakeReservation_WithNoAvailableTables_ShouldReturnNull()
        {
            string errorMessage = "ReservationService MakeReservation() method does not work properly.";

            var context = ReserveTableDbContextInMemoryFactory.InitializeContext();

            this.reservationService = new ReservationService(context);
            await SeedData(context);

            CreateReservationBindingModel reservationBindingModel = new CreateReservationBindingModel
            {
                Date       = "09/08/2019 20:00:00",
                SeatsCount = 4,
                Restaurant = "Happy"
            };

            ReserveTableUserServiceModel user = new ReserveTableUserServiceModel
            {
                Id       = "1",
                Email    = "*****@*****.**",
                UserName = "******"
            };

            RestaurantServiceModel restaurant = new RestaurantServiceModel
            {
                Id            = "1",
                Address       = "New Street",
                AverageRating = 10,
                CityId        = "1",
                Name          = "Happy",
                PhoneNumber   = "0888888888",
                Photo         = "/src/happy.jpg",
                Tables        = new List <TableServiceModel>()
                {
                    new TableServiceModel
                    {
                        Id           = "1",
                        SeatsCount   = 2,
                        RestaurantId = "1",
                        Reservations = new List <ReservationServiceModel>()
                    }
                }
            };

            var actualResult = await reservationService.MakeReservation(reservationBindingModel, user, restaurant);

            Assert.True(actualResult == null, errorMessage);
        }
        public async Task GetUserByUsername_WithExistentUsername_ShouldReturnCorrectResult()
        {
            string errorMessagePrefix = "UserService GetUserByUsername() method does not work properly.";

            var context = ReserveTableDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.userService = new UserService(context);

            ReserveTableUserServiceModel expectedData = context.Users.First().To <ReserveTableUserServiceModel>();
            ReserveTableUserServiceModel actualData   = await this.userService.GetUserByUsername(expectedData.UserName);

            Assert.True(expectedData.Id == actualData.Id, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(expectedData.UserName == actualData.UserName, errorMessagePrefix + " " + "UserName is not returned properly.");
            Assert.True(expectedData.Email == actualData.Email, errorMessagePrefix + " " + "Price is not returned properly.");
        }
Example #6
0
        public async Task <ReservationServiceModel> MakeReservation(CreateReservationBindingModel viewModel, ReserveTableUserServiceModel user, RestaurantServiceModel restaurant)
        {
            var dateTime = DateTime.Parse(viewModel.Date + " " + viewModel.Time);

            var tablesWithExactCountSeats = restaurant.Tables
                                            .Where(t => t.SeatsCount == viewModel.SeatsCount)
                                            .ToList();

            var tablesWithSeatsCountPlusOne = restaurant.Tables
                                              .Where(t => t.SeatsCount == viewModel.SeatsCount + 1)
                                              .ToList();

            ReservationServiceModel reservationServiceModel = new ReservationServiceModel();

            if (tablesWithExactCountSeats.Any())
            {
                foreach (var table in tablesWithExactCountSeats)
                {
                    return(await FindTable(viewModel, user, restaurant, dateTime, reservationServiceModel, table));
                }
            }
            else if (tablesWithSeatsCountPlusOne.Any())
            {
                foreach (var biggerTable in tablesWithSeatsCountPlusOne)
                {
                    return(await FindTable(viewModel, user, restaurant, dateTime, reservationServiceModel, biggerTable));
                }
            }

            return(null);
        }
Example #7
0
 private static void InitializeReservation(CreateReservationBindingModel viewModel, ReserveTableUserServiceModel user, RestaurantServiceModel restaurant, DateTime dateTime, ReservationServiceModel reservationServiceModel, TableServiceModel table)
 {
     reservationServiceModel.ForDate      = dateTime;
     reservationServiceModel.SeatsCount   = viewModel.SeatsCount;
     reservationServiceModel.UserId       = user.Id;
     reservationServiceModel.TableId      = table.Id;
     reservationServiceModel.RestaurantId = restaurant.Id;
 }
Example #8
0
        private async Task <ReservationServiceModel> FindTable(CreateReservationBindingModel viewModel, ReserveTableUserServiceModel user, RestaurantServiceModel restaurant, DateTime dateTime, ReservationServiceModel reservationServiceModel, TableServiceModel table)
        {
            if (table.Reservations.Any(t => (dateTime > t.ForDate && dateTime < t.EndOfReservation) && t.IsCancelled == false))
            {
                return(null);
            }
            else
            {
                InitializeReservation(viewModel, user, restaurant, dateTime, reservationServiceModel, table);

                var reservation = AutoMapper.Mapper.Map <Reservation>(reservationServiceModel);

                await dbContext.Reservations.AddAsync(reservation);

                await dbContext.SaveChangesAsync();

                return(reservationServiceModel);
            }
        }
Example #9
0
        public async Task MakeReservation_WithCorrectData_ShouldCreateSucessfully(int seats)
        {
            string errorMessage = "ReservationService MakeReservation() method does not work properly.";

            var context = ReserveTableDbContextInMemoryFactory.InitializeContext();

            this.reservationService = new ReservationService(context);
            await SeedData(context);

            CreateReservationBindingModel reservationBindingModel = new CreateReservationBindingModel
            {
                Date       = "09/08/2019 20:00:00",
                SeatsCount = seats,
                Restaurant = "Happy"
            };

            ReserveTableUserServiceModel user = new ReserveTableUserServiceModel
            {
                Id       = "1",
                Email    = "*****@*****.**",
                UserName = "******"
            };

            RestaurantServiceModel restaurant = new RestaurantServiceModel
            {
                Id            = "1",
                Address       = "New Street",
                AverageRating = 10,
                CityId        = "1",
                Name          = "Happy",
                PhoneNumber   = "0888888888",
                Photo         = "/src/happy.jpg",
                Tables        = new List <TableServiceModel>()
                {
                    new TableServiceModel
                    {
                        Id           = "1",
                        SeatsCount   = 2,
                        RestaurantId = "1",
                        Reservations = new List <ReservationServiceModel>()
                    }
                }
            };

            var expectedResult = new ReservationServiceModel
            {
                ForDate    = new DateTime(2019, 08, 09, 20, 0, 0),
                Restaurant = new RestaurantServiceModel
                {
                    Id            = "1",
                    Address       = "New Street",
                    AverageRating = 10,
                    CityId        = "1",
                    Name          = "Happy",
                    PhoneNumber   = "0888888888",
                    Photo         = "/src/happy.jpg",
                    Tables        = new List <TableServiceModel>()
                    {
                        new TableServiceModel
                        {
                            Id           = "1",
                            SeatsCount   = 2,
                            RestaurantId = "1",
                            Reservations = new List <ReservationServiceModel>()
                        }
                    }
                },
                SeatsCount = seats,
                UserId     = "1"
            };
            var actualResult = await reservationService.MakeReservation(reservationBindingModel, user, restaurant);

            Assert.True(expectedResult.SeatsCount == actualResult.SeatsCount, errorMessage + " " + "Seats Count is not returned properly");
            Assert.True(expectedResult.ForDate == actualResult.ForDate, errorMessage + " " + "For Date is not returned properly");
            Assert.True(expectedResult.UserId == actualResult.UserId, errorMessage + " " + "User Id is not returned properly");
        }