public async Task <IActionResult> GetBookings(int hotelId, [FromQuery] BookingParam bookingParam) { var bookings = await _repo.GetBookings(bookingParam, hotelId); var bookingsToReturn = _mapper.Map <IEnumerable <BookingForListDto> >(bookings); Response.AddPagination(bookings.CurrentPage, bookings.PageSize, bookings.TotalCount, bookings.TotalPages); return(Ok(bookingsToReturn)); }
public async Task <PagedList <Booking> > GetBookings(BookingParam bookingParam, int HotelID) { var bookings = _context.Bookings.Where(h => h.HotelId == HotelID && h.IsMain == true && h.IsDeleted == false) .OrderBy(b => b.Id).AsQueryable(); if (!bookingParam.All) { if (!string.IsNullOrEmpty(bookingParam.RoomNumber)) { bookings = bookings.Where(b => b.RoomNumber == bookingParam.RoomNumber); } if (!string.IsNullOrEmpty(bookingParam.Fullname)) { bookings = bookings.Where(b => b.GuestName.Contains(bookingParam.Fullname)); } if (!string.IsNullOrEmpty(bookingParam.Email)) { bookings = bookings.Where(b => b.Email.Contains(bookingParam.Email)); } if (!string.IsNullOrEmpty(bookingParam.CheckInDate)) { DateTime dt; if (DateTime.TryParse(bookingParam.CheckInDate, out dt)) { bookings = bookings.Where(b => b.CheckIn.ToShortDateString() == dt.ToShortDateString()); } } } if (!string.IsNullOrEmpty(bookingParam.OrderBy)) { switch (bookingParam.OrderBy) { case "CheckIn": bookings = bookings.OrderByDescending(b => b.CheckIn); break; default: bookings = bookings.OrderBy(b => b.GuestName); break; } } return(await PagedList <Booking> .CreateAsync(bookings, bookingParam.PageNumber, bookingParam.PageSize)); }