public async Task <IActionResult> CreateNewBooking(NewBooking booking) { try { DateTime[] formatedDates = BookingsHelper.formatDates(booking.startDate, booking.endDate); booking.startDate = formatedDates[0]; booking.endDate = formatedDates[1]; List <string> errors = await BookingsHelper.validations(booking.startDate, booking.endDate, mySQLDBContext, booking.idRoom); errors.AddRange(BookingsHelper.validationsBooking(booking.startDate, booking.endDate, mySQLDBContext, booking.idRoom)); if (errors.Count != 0) { return(BadRequest(errors)); } Bookings newBooking = new Bookings { idRoom = booking.idRoom, startDate = booking.startDate, endDate = booking.endDate }; await mySQLDBContext.AddAsync(newBooking); await mySQLDBContext.SaveChangesAsync(); return(Ok(newBooking)); } catch (Exception e) { logger.LogError(e.Message); return(StatusCode(500)); } }
public async Task <IActionResult> AddRoom(Rooms room) { try { List <string> errors = await RoomsHelper.Validations(room, mySQLDBContext); if (errors.Count != 0) { return(BadRequest(errors)); } await mySQLDBContext.AddAsync(room); await mySQLDBContext.SaveChangesAsync(); return(Ok(room)); } catch (Exception e) { logger.LogError(e.Message); return(StatusCode(500)); } }
public async void TestAddRoom() { var options = new DbContextOptionsBuilder <MySQLDBContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; //need to use GUID for unit testing. EnsureDeleted() wasn't working for InMemoryDatabase // 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" }; var resultActionResult = await controller.AddRoom(room1); var result = resultActionResult as ObjectResult; // 3. Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.IsType <Rooms>(result.Value); } // 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 ObjectResult; // 3. Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.IsType <List <Rooms> >(result.Value); Assert.NotEmpty((System.Collections.IEnumerable)result.Value); Assert.Single((System.Collections.IEnumerable)result.Value); } }