public bool BookRoom(int roomId, DateTime newBookingStart, DateTime newBookingEnd, string customerComment) { //is the data valid? if (newBookingStart.Date > newBookingEnd.Date) { throw new ArgumentException("Illegal: start should be before end"); } //which room are we dealing with? Room room = roomRepo.GetRoomDetails(roomId); //Is the room available at this time? var bookings = repo.GetBookingsByRoom(room); foreach (var item in bookings) { if (!((item.startDate.Date > newBookingStart.Date && item.startDate.Date > newBookingEnd.Date) || (newBookingStart.Date > item.endDate.Date && newBookingEnd.Date > item.endDate.Date))) { throw new Exception("Room is not available in this period! It clashed with Booking ID: " + item.Id); } } //all is well. Let's create the booking. Booking booking = new Booking(); booking.room = room; booking.startDate = newBookingStart; booking.endDate = newBookingEnd; booking.totalPrice = (newBookingEnd.Day + 1 - newBookingStart.Day) * room.PricePerDay; booking.dateOfOrder = DateTime.Today; booking.customerComment = customerComment; return(repo.BookRoom(booking)); }
public void BookingService_FarIntoTheFuture_Valid() { //arrange repo = _serviceProvider.GetService <IBookingRepo>(); Booking booking = new Booking() { customerComment = "Just For the night, Thank you!", dateOfOrder = DateTime.Today.AddDays(200), startDate = DateTime.Today.AddDays(200), endDate = DateTime.Today.AddDays(200), Id = 9999999, totalPrice = 55.44f, room = new Room() { Id = 999, available = true, PricePerDay = 55.44f, roomType = Room.RoomType.Standard, status = "it's quite pretty" } }; //act var res = repo.BookRoom(booking); //assert Assert.AreEqual(true, res); }