public void FindAsync_Test_WhenUserOverBookedExceptionIsThrown_ExpectBadRequest() { // Arrange var header = new Dictionary <string, string> { { "UserId", "1" } }; var criteria = new FindRestaurantCriteria { AvailabilityDateTime = DateTime.Now, UserIds = new List <long> { 1, 2 } }; var mockFacade = new Mock <IRestaurantFacade>(); mockFacade.Setup(facade => facade.GetAsync(It.IsAny <FindRestaurantCriteria>(), It.IsAny <CancellationToken>())).ThrowsAsync(new UserOverBookedException("Ex")); var controller = GetControllerInstance(mockFacade.Object, header); // Act var badRequestResult = controller.GetRestaurantAsync(criteria, CancellationToken.None); // Assert Assert.IsType <BadRequestObjectResult>(badRequestResult.Result); }
public async Task <ActionResult> GetRestaurantAsync([FromBody] FindRestaurantCriteria criteria, CancellationToken cancellationToken) { try { var resturants = await _restaurantFacade.GetAsync(criteria, cancellationToken); return(Ok(resturants)); } catch (UserNotRegisteredException ex) { return(BadRequest(ex.Message)); } catch (UserOverBookedException ex) { return(BadRequest(ex.Message)); } catch (TableNotFoundException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void FindAsync_Test() { // Arrange var header = new Dictionary <string, string> { { "UserId", "1" } }; var restaurantList = new List <Restaurant> { new Restaurant { Id = 1, Location = new Location { Latitude = 0.0, Longitude = 0.0, Id = 1 }, LocationId = 1, Name = "La casa de Toño" } }; var criteria = new FindRestaurantCriteria { AvailabilityDateTime = DateTime.Now, UserIds = new List <long> { 1, 2 } }; var mockFacade = new Mock <IRestaurantFacade>(); mockFacade.Setup(facade => facade.GetAsync(It.IsAny <FindRestaurantCriteria>(), It.IsAny <CancellationToken>())).ReturnsAsync(restaurantList); var controller = GetControllerInstance(mockFacade.Object, header); // Act var okResult = controller.GetRestaurantAsync(criteria, CancellationToken.None); // Assert Assert.IsType <OkObjectResult>(okResult.Result); }
public async Task <List <Restaurant> > GetAsync(FindRestaurantCriteria criteria, CancellationToken cancellationToken) { await CheckRegisteredUsersAsync(criteria.UserIds, cancellationToken); var availableRestaurants = _dbContext.GetRestaurantByUsersDietaryRestriction(criteria.UserIds); // Now that we have all the restaurants that match the users dietary restrictions, we need to see if there are available tables // with the requested number of seats // now let's check if the table is available or not var availableTables = _dbContext.GetAvailableTablesByRestaurantIds(availableRestaurants.Select(r => r.Id).ToList(), criteria.UserIds.Count); if (availableTables != null && !availableTables.Any()) { throw new TableNotFoundException("There are no available restaurants/tables for the selected users."); } // There are available tables! var restaurants = await _dbContext.Restaurants .Where(rest => availableTables.Select(table => table.RestaurantId).Contains(rest.Id)) .ToListAsync(cancellationToken); foreach (var restaurant in restaurants) { var location = await _dbContext.Locations.Where(loc => loc.Id == restaurant.LocationId).FirstAsync(cancellationToken); restaurant.Location = new Location { Id = location.Id, Latitude = location.Latitude, Longitude = location.Longitude }; } return(restaurants); }
public async Task <List <Restaurant> > GetAsync(FindRestaurantCriteria criteria, CancellationToken cancellationToken) { return(await _restaurantManager.GetAsync(criteria, cancellationToken)); }