public async void TestGetAll() { var options = new DbContextOptionsBuilder <MySQLDBContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; // Set up a context (connection to the "DB") for writing using (var context = new MySQLDBContext(options)) { // 2. Act var controller = new RoomsController(context); context.Hotel.Add(new Hotel { name = "TEST", phone = "1234567890" }); await context.SaveChangesAsync(); Rooms room1 = new Rooms { idHotel = 1, maxCapacity = 2, phoneExtension = 101, rate = 899.99f, roomNumber = "101" }; Rooms room2 = new Rooms { idHotel = 1, maxCapacity = 4, phoneExtension = 102, rate = 799.99f, roomNumber = "102" }; await controller.AddRoom(room1); await controller.AddRoom(room2); } // Set up a context (connection to the "DB") for writing using (var context = new MySQLDBContext(options)) { // 2. Act var controller = new RoomsController(context); var resultActionResult = controller.GetAll(); var result = resultActionResult as OkObjectResult; // 3. Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.IsType <List <Rooms> >(result.Value); Assert.NotEmpty((System.Collections.IEnumerable)result.Value); Assert.Equal(2, ((List <Rooms>)result.Value).Count); context.Database.EnsureDeleted(); } }
/// <summary> /// Checks if there's another booking that has conflict with the one we're trying to create. /// If it's for an update it ignores this booking since it will change. /// </summary> /// <param name="idRoom"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="mySQLDBContext"></param> /// <param name="isInsert"></param> /// <param name="idBooking"></param> /// <returns>A boolean to if the date was available or not</returns> public static Boolean isDateAvailable(int idRoom, DateTime startTime , DateTime endTime, MySQLDBContext mySQLDBContext , Boolean isInsert = true, int idBooking = 0) { try { if (isInsert) { int exists = mySQLDBContext.Bookings.Where(x => x.idRoom == idRoom && ((x.startDate >= startTime && x.startDate <= endTime) || (x.endDate >= startTime && x.endDate <= endTime)) ).Count(); if (exists == 0) { return(true); } } else { int exists = mySQLDBContext.Bookings.Where(x => x.idRoom == idRoom && ((x.startDate >= startTime && x.startDate <= endTime) || (x.endDate >= startTime && x.endDate <= endTime)) && x.id != idBooking ).Count(); if (exists == 0) { return(true); } } return(false); } catch (Exception e) { throw e; } }
/// <summary> /// Checks that the desired room is available and follow the business rule of the max stay /// </summary> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="mySQLDBContext"></param> /// <param name="idRoom"></param> /// <param name="isInsert"></param> /// <param name="bookingID"></param> /// <returns>Returns a list of errors</returns> public static List <string> validationsBooking(DateTime startTime, DateTime endTime , MySQLDBContext mySQLDBContext, int idRoom , Boolean isInsert = true, int bookingID = 0) { List <string> errors = new List <string>(); try { Boolean roomAvailable = isDateAvailable(idRoom, startTime, endTime, mySQLDBContext, isInsert, bookingID); if (!roomAvailable) { errors.Add(Constants.roomBookedError); } if ((endTime - startTime).TotalDays > Constants.maxStayDays) { errors.Add(Constants.datesLengthError); } return(errors); } catch (Exception e) { throw e; } }
public RoomsController(MySQLDBContext context, ILogger <RoomsController> _logger) { mySQLDBContext = context; logger = _logger; }
/// <summary> /// Validates all the variables from Rooms that need to be unique on the DB /// or that require values from another table /// </summary> /// <param name="room"></param> /// <param name="mySQLDBContext"></param> /// <param name="isInsert"></param> /// <returns></returns> public static async Task <List <string> > DatabaseValidations(Rooms room, MySQLDBContext mySQLDBContext, Boolean isInsert = true) { List <string> errors = new List <string>(); try { //if it's insert it checks the unique values don't already exist on the DB if (isInsert) { //Check if idHotel exists int hotelCount = await mySQLDBContext.Hotel.Where(x => x.id == room.idHotel).CountAsync(); //Check if combination idHotel and roomNumber exists int hotelRoomCount = await mySQLDBContext.Rooms.Where(x => x.idHotel == room.idHotel && x.roomNumber == room.roomNumber) .CountAsync(); //ToDo check PhoneExtension not on database int hotelPhoneExtensionCount = await mySQLDBContext.Rooms.Where(x => x.idHotel == room.idHotel && x.phoneExtension == room.phoneExtension) .CountAsync(); if (hotelCount != 1) { errors.Add(Constants.idHotelDoesntExistError); } if (hotelRoomCount != 0) { errors.Add(Constants.roomAlreadyExistError); } if (hotelPhoneExtensionCount != 0) { errors.Add(Constants.roomExtensionExistError); } return(errors); } //if it's an update it checks that the value don't exist previously for a record different //to the one that's been modified int hotelRoomCount2 = await mySQLDBContext.Rooms.Where(x => x.idHotel == room.idHotel && x.roomNumber == room.roomNumber && x.id != room.id) .CountAsync(); //ToDo check PhoneExtension not on database int hotelPhoneExtensionCount2 = await mySQLDBContext.Rooms.Where(x => x.idHotel == room.idHotel && x.phoneExtension == room.phoneExtension && x.id != room.id) .CountAsync(); if (hotelRoomCount2 != 0) { errors.Add(Constants.roomAlreadyExistError); } if (hotelPhoneExtensionCount2 != 0) { errors.Add(Constants.roomExtensionExistError); } } catch (Exception e) { throw e; } return(errors); }
/// <summary> /// Gets a list of date ranges where the room is available /// </summary> /// <param name="idRoom"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="mySQLDBContext"></param> /// <returns>A list of start and end dates for the availability of that room</returns> public static List <RoomAvailability> GetRoomAvailability(int idRoom, DateTime startTime , DateTime endTime, MySQLDBContext mySQLDBContext) { List <RoomAvailability> response = new List <RoomAvailability>(); try { //gets all the bookings on the room and dates they're asking List <Bookings> bookings = mySQLDBContext.Bookings.Where(x => x.idRoom == idRoom && ((x.startDate >= startTime && x.startDate <= endTime) || (x.endDate >= startTime && x.endDate <= endTime)) ).ToList(); //if there's no bookings that means it's all available so we just build that response and return if (bookings.Count == 0) { response.Add(new RoomAvailability { startDate = startTime, endDate = endTime }); return(response); } DateTime startDate = startTime; foreach (Bookings booking in bookings) { //if we have a time gap before the first booking on the interval //we add that to our response list if (booking.startDate > startDate) { response.Add(new RoomAvailability { startDate = startDate, endDate = booking.startDate.AddSeconds(-1) }); } //we set the next block startDate to after the current booking ends startDate = booking.endDate.AddSeconds(1); } //in case we had a gap at the end, we add it at the end if (startDate < endTime) { response.Add(new RoomAvailability { startDate = startDate, endDate = endTime }); } return(response); } catch (Exception e) { throw e; } }