public async Task <IActionResult> SearchRooms(SearchRooms roomSearch)
        {
            bool checkDates = roomSearch.CheckIn < roomSearch.CheckOut;

            // prepare the parameters to be inserted into the query
            if (ModelState.IsValid && checkDates)
            {
                var beds     = new SqliteParameter("beds", roomSearch.BedCount);
                var checkIn  = new SqliteParameter("checkInS", roomSearch.CheckIn);
                var checkOut = new SqliteParameter("checkOutS", roomSearch.CheckOut);

                // Use placeholders with the same names as the corresponding parameters
                var roomsAvailable = _context.Room.FromSql("select * from [Room] "
                                                           + "where [Room].BedCount = @beds and [Room].ID not in "
                                                           + "(select [Room].ID from [Room] inner join [Booking] on [Room].ID = [Booking].RoomID "
                                                           + "where @checkInS < [Booking].CheckOut and [Booking].CheckIn < @checkOutS)", beds, checkIn, checkOut)
                                     .Select(ro => new Room {
                    ID = ro.ID, Level = ro.Level, BedCount = ro.BedCount, Price = ro.Price
                });

                // Run the query and save the results in ViewBag for passing to view
                ViewBag.Rooms = await roomsAvailable.ToListAsync();

                return(View(roomSearch));
            }
            if (!checkDates)
            {
                ViewBag.DateError = "Sorry, check out date cannot be less than or the same as check in date";
            }

            // invoke the view with the ViewModel object
            return(View());
        }
Exemple #2
0
        //Searc Rooms for Reservations
        public ActionResult Index(SearchRooms searchRooms = null)
        {
            string token = Request.Cookies["token"].Value.ToString();
            User   user  = context.Users.FirstOrDefault(u => u.Token == token);

            ViewBag.User = user;
            if (user.Type == UserType.restaurant)
            {
                return(RedirectToAction("index", "login"));
            }

            if (searchRooms.Start == null)
            {
                searchRooms.Start = DateTime.Now;
            }
            if (searchRooms.End == null)
            {
                searchRooms.End = DateTime.Now.AddDays(1);
            }

            List <Room> rooms = context.Rooms.Include("Reservations").Where(r => r.IsDelete == false && r.Status == true &&
                                                                            (searchRooms.MinPrice != null ? r.Price >= searchRooms.MinPrice : true) &&
                                                                            (searchRooms.MaxPrice != null ? r.Price <= searchRooms.MaxPrice : true) &&
                                                                            (searchRooms.PairPersonBedroom != null ? r.PairPersonBedroom == searchRooms.PairPersonBedroom : true) &&
                                                                            (searchRooms.SinglePersonBedroom != null ? r.SinglePersonBedroom == searchRooms.SinglePersonBedroom : true) &&
                                                                            (searchRooms.ChildBedroom != null ? r.ChildBedroom == searchRooms.ChildBedroom : true)).ToList();
            List <Room> rooms1 = new List <Room>();

            rooms1.AddRange(rooms);
            foreach (var room in rooms)
            {
                foreach (var reservation in room.Reservations)
                {
                    if (reservation.Start < searchRooms.Start && reservation.End > searchRooms.Start || searchRooms.End > reservation.Start && searchRooms.Start < reservation.Start)
                    {
                        rooms1.Remove(room);
                        break;
                    }
                }
            }
            SearchRoomsToBook searchRoomsToBook = new SearchRoomsToBook
            {
                SearchRooms = searchRooms,
                Rooms       = rooms
            };

            if (searchRooms.Start >= searchRooms.End)
            {
                ModelState.AddModelError("Start", "Tarixləri düz yazın");
                return(View(searchRooms));
            }

            return(View(searchRoomsToBook));
        }
        public async Task <IActionResult> Create(SearchRooms searchingRoom)
        {
            if (ModelState.IsValid)
            {
                _context.Add(searchingRoom);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(searchingRoom));
        }
        public async Task <IActionResult> Search(SearchRooms basicSearch)
        {
            var bed         = new SqliteParameter("bed", basicSearch.roomBedCount);
            var cin         = new SqliteParameter("in", basicSearch.BookingCheckIn);
            var cout        = new SqliteParameter("out", basicSearch.BookingCheckOut);
            var searchRooms = _context.Room.FromSql("select * from [Room] "
                                                    + "where [Room].BedCount = @bed and [Room].ID not in "
                                                    + "(select [Room].ID from [Room] inner join [Booking] on [Room].ID = [Booking].RoomID where "
                                                    + "[Booking].CheckIn < @out AND @in < [Booking].CheckOut)", bed, cin, cout)
                              .Select(r => new Room {
                ID = r.ID, Level = r.Level, BedCount = r.BedCount, Price = r.Price
            });

            ViewBag.result = await searchRooms.ToListAsync();

            return(View(basicSearch));
        }
        public async Task <IActionResult> SearchForRooms(SearchRooms search)
        {
            if (ModelState.IsValid)
            {
                var bedCount  = new SqliteParameter("count", search.BedCount);
                var checkIn   = new SqliteParameter("in", search.CheckIn);
                var checkOut  = new SqliteParameter("out", search.CheckOut);
                var freeRooms = _context.Room.FromSql("select * from [Room] where [Room].BedCount = @count and [Room].ID not in "

                                                      + "(select [Room].ID from [Room] inner join [Booking] on [Room].ID = [Booking].RoomID "
                                                      + "where [Room].BedCount = @count and @in >= [Booking].checkIn" +
                                                      " and @out <= [Booking].checkOut)", bedCount, checkIn, checkOut)
                                .Select(mo => new Room {
                    ID = mo.ID, Level = mo.Level, BedCount = mo.BedCount, Price = mo.Price
                });

                ViewBag.FreeRooms = await freeRooms.ToListAsync();
            }
            return(View(search));
        }