Skip to content

Commit

Permalink
Section 14 - Check if booking exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhrugen Patel committed Dec 5, 2020
1 parent 92d3154 commit 9b1bda5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
46 changes: 45 additions & 1 deletion Business/Repository/HotelRoomRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public async Task<IEnumerable<HotelRoomDTO>> GetAllHotelRooms(string checkInDate
IEnumerable<HotelRoomDTO> hotelRoomDTOs =
_mapper.Map<IEnumerable<HotelRoom>, IEnumerable<HotelRoomDTO>>
(_db.HotelRooms.Include(x => x.HotelRoomImages));

if (!string.IsNullOrEmpty(checkInDateStr) && !string.IsNullOrEmpty(checkOutDatestr))
{
foreach (HotelRoomDTO hotelRoom in hotelRoomDTOs)
{
hotelRoom.IsBooked = await IsRoomBooked(hotelRoom.Id, checkInDateStr, checkOutDatestr);
}
}
return hotelRoomDTOs;
}
catch (Exception ex)
Expand All @@ -70,6 +76,11 @@ public async Task<HotelRoomDTO> GetHotelRoom(int roomId, string checkInDateStr,
HotelRoomDTO hotelRoom = _mapper.Map<HotelRoom, HotelRoomDTO>(
await _db.HotelRooms.Include(x => x.HotelRoomImages).FirstOrDefaultAsync(x => x.Id == roomId));

if (!string.IsNullOrEmpty(checkInDateStr) && !string.IsNullOrEmpty(checkOutDatestr))
{
hotelRoom.IsBooked = await IsRoomBooked(roomId, checkInDateStr, checkOutDatestr);
}

return hotelRoom;
}
catch (Exception ex)
Expand All @@ -78,6 +89,39 @@ public async Task<HotelRoomDTO> GetHotelRoom(int roomId, string checkInDateStr,
}
}


public async Task<bool> IsRoomBooked(int RoomId, string checkInDatestr, string checkOutDatestr)
{
try
{
if (!string.IsNullOrEmpty(checkOutDatestr) && !string.IsNullOrEmpty(checkInDatestr))
{
DateTime checkInDate = DateTime.ParseExact(checkInDatestr, "MM/dd/yyyy", null);
DateTime checkOutDate = DateTime.ParseExact(checkOutDatestr, "MM/dd/yyyy", null);

var existingBooking = await _db.RoomOrderDetails.Where(x => x.RoomId == RoomId && x.IsPaymentSuccessful &&
//check if checkin date that user wants does not fall in between any dates for room that is booked
(checkInDate < x.CheckOutDate && checkInDate.Date >= x.CheckInDate
//check if checkout date that user wants does not fall in between any dates for room that is booked
|| checkOutDate.Date > x.CheckInDate.Date && checkInDate.Date <= x.CheckInDate.Date
)).FirstOrDefaultAsync();

if (existingBooking != null)
{
return true;
}
return false;
}
return true;
}
catch (Exception ex)
{
throw ex;
}


}

//if unique returns null else returns the room obj
public async Task<HotelRoomDTO> IsRoomUnique(string name, int roomId = 0)
{
Expand Down
1 change: 1 addition & 0 deletions Business/Repository/IRepository/IHotelRoomRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface IHotelRoomRepository
public Task<int> DeleteHotelRoom(int roomId);
public Task<IEnumerable<HotelRoomDTO>> GetAllHotelRooms(string checkInDate = null, string checkOutDate = null);
public Task<HotelRoomDTO> IsRoomUnique(string name, int roomId = 0);
public Task<bool> IsRoomBooked(int RoomId, string checkInDate, string checkOutDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public interface IRoomOrderDetailsRepository
public Task<RoomOrderDetailsDTO> GetRoomOrderDetail(int roomOrderId);
public Task<IEnumerable<RoomOrderDetailsDTO>> GetAllRoomOrderDetails();
public Task<bool> UpdateOrderStatus(int RoomOrderId, string status);
public Task<bool> IsRoomBooked(int RoomId, DateTime checkInDate, DateTime checkOutDate);

}
}
15 changes: 0 additions & 15 deletions Business/Repository/RoomOrderDetailsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,7 @@ public async Task<RoomOrderDetailsDTO> GetRoomOrderDetail(int roomOrderId)
}
}

public async Task<bool> IsRoomBooked(int RoomId, DateTime checkInDate, DateTime checkOutDate)
{
var status = false;
var existingBooking = await _db.RoomOrderDetails.Where(x => x.RoomId == RoomId && x.IsPaymentSuccessful &&
//check if checkin date that user wants does not fall in between any dates for room that is booked
(checkInDate < x.CheckOutDate && checkInDate.Date > x.CheckInDate
//check if checkout date that user wants does not fall in between any dates for room that is booked
|| checkOutDate.Date > x.CheckInDate.Date && checkInDate.Date < x.CheckInDate.Date
)).FirstOrDefaultAsync();

if (existingBooking != null)
{
status = true;
}
return status;
}

public async Task<RoomOrderDetailsDTO> MarkPaymentSuccessful(int id)
{
Expand Down
1 change: 1 addition & 0 deletions Models/HotelRoomDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public class HotelRoomDTO
public virtual ICollection<HotelRoomImageDTO> HotelRoomImages { get; set; }

public List<string> ImageUrls { get; set; }
public bool IsBooked { get; set; }
}
}

0 comments on commit 9b1bda5

Please sign in to comment.