public async Task <IActionResult> Get([FromQuery] RestaurantFilter filter) { if (filter.Limit > FilterConstants.MAX_LIMIT || filter.Limit < 1) { return(BadRequest(new ApiResponse { Success = false, ErrorCode = ErrorCodes.INVALID_LIMIT })); } if (filter.LastId != null && !filter.LastId.Equals("")) { var r = await _restaurantService.GetRestaurant(filter.LastId); if (!r.Success) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } if (r.Data == null) { return(BadRequest(new ApiResponse { Success = false, ErrorCode = ErrorCodes.INVALID_LAST_ID })); } } var res = await _restaurantService.GetAllRestaurant(UserId, Role, filter); if (!res.Success) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } return(Ok(res)); }
public IEnumerable <ViewRestaurant> GetRestaurants(RestaurantFilter restaurantFilter) { IEnumerable <ViewRestaurant> restaurants = _viewRestaurantRepository.GetAll() .OrderByDescending(p => _restaurantRatingTask.GetConsolidatedRating(p.Id)); if (restaurantFilter == null) { return(restaurants); } if (!string.IsNullOrWhiteSpace(restaurantFilter.Name)) { restaurants = restaurants.Where( p => p.Name.IndexOf(restaurantFilter.Name, StringComparison.InvariantCultureIgnoreCase) != -1); } if (restaurantFilter.Cuisine != null) { restaurants = restaurants?.Where(p => p.Type == restaurantFilter.Cuisine.ToString()); } return(restaurants); }
public static async Task <List <Restaurant> > GetRestaurants(RestaurantFilter filter) { List <Restaurant> restaurants = new List <Restaurant>(); HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GlobalAppData.Configuration["YelpConfig:Token"]); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); StringBuilder stringBuilder = new StringBuilder("https://api.yelp.com/v3/businesses/search"); // TODO: Good place to use the strategy pattern here? Inject a formatter? So could FormatForUrlParameters, and just append that to the yelp url. Look at how Google Maps API is queried. Could have API-specific (yelp, maps, etc.) query formatters stringBuilder.Append(filter.FormatForUrlParameters()); HttpResponseMessage response = await httpClient.GetAsync(stringBuilder.ToString()); string responseBody = await response.Content.ReadAsStringAsync(); dynamic jsonResponse = JsonConvert.DeserializeObject(responseBody); if (jsonResponse?.businesses != null) // TODO: Log error if null { foreach (dynamic business in jsonResponse.businesses) { //restaurants.Add(JsonConvert.DeserializeObject<Restaurant>(business)); // TODO: Can the below be moved to a custom deserializer so something close to just the above code can be ran here Restaurant restaurant = new Restaurant(); restaurant.Name = business?.name; Location location = new Location(); location.StreetAddress = $"{business?.location?.address1} {business?.location?.address2} {business?.location?.address3}".Trim(); location.City = business?.location?.city; location.State = business?.location?.state; location.Zipcode = business?.location?.zip_code; restaurant.Location = location; if (business?.categories != null) // TODO: Log error if null { foreach (dynamic category in business.categories) { restaurant.Categories.Add((string)category?.alias); } } restaurants.Add(restaurant); } } // TODO: Max results. Yelp defaults to 20, max is 50. We can request up to 1000 from Yelp by navigating their pagination with the offset parameter return(restaurants); }
public async Task <List <Restaurant> > GetAllWithFilter(RestaurantFilter restaurantFilter) { Expression <Func <Restaurant, bool> > filter = null; Func <IQueryable <Restaurant>, IOrderedQueryable <Restaurant> > ordeBy = null; int?skip = null; int?take = 25; if (!string.IsNullOrWhiteSpace(restaurantFilter.Name)) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.Name == restaurantFilter.Name); } if (!string.IsNullOrWhiteSpace(restaurantFilter.Description)) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.Description == restaurantFilter.Description); } if (!string.IsNullOrWhiteSpace(restaurantFilter.CategoryName)) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.CategoryRestaurant.Name == restaurantFilter.CategoryName); } if (!string.IsNullOrWhiteSpace(restaurantFilter.Order)) { switch (restaurantFilter.Order) { case "Id": ordeBy = x => x.OrderBy(n => n.Id); break; case "Name": ordeBy = x => x.OrderBy(n => n.Name); break; case "Description": ordeBy = x => x.OrderBy(n => n.Description); break; case "CreatedAt": ordeBy = x => x.OrderBy(n => n.CreatedAt); break; } } if (restaurantFilter.Id != Guid.Empty) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.Id == restaurantFilter.Id); } if (restaurantFilter.CategoryId != Guid.Empty) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.CategoryRestaurant.Id == restaurantFilter.CategoryId); } if (restaurantFilter.CreatedAt.Year > 1) { if (filter == null) { filter = PredicateBuilder.New <Restaurant>(true); } filter = filter.And(x => x.Id == restaurantFilter.Id); } if (restaurantFilter.PageIndex != null) { if (restaurantFilter.PageIndex > 1) { skip = restaurantFilter.PageIndex * 25; } } if (restaurantFilter.PageSize != null) { if (restaurantFilter.PageSize > 0) { take = restaurantFilter.PageSize; } } return(await _unitOfWork .RepositoryFactory .RestaurantRepository .Search( filter, ordeBy, skip, take)); }
public async Task <ApiResponse <List <Restaurant> > > GetAllRestaurant(string userId, string role, RestaurantFilter filter) { List <Restaurant> list = new List <Restaurant>(); try { DocumentSnapshot last = null; if (filter.LastId != null && !filter.LastId.Equals("")) { last = await _firebaseService.GetFirestoreDb().Collection(TableNames.RESTAURANTS).Document(filter.LastId).GetSnapshotAsync(); } while (true) { Query query = getAllRestaurantsReference(userId, role); query = query.OrderBy(TableNames.RESTAURANTS_NAME); if (filter.Name != null && !filter.Name.Equals("")) { query = query.WhereGreaterThanOrEqualTo(TableNames.RESTAURANTS_NAME, filter.Name).WhereLessThanOrEqualTo(TableNames.RESTAURANTS_NAME, filter.Name + Constants.Unicodes.HIGHEST); } if (last != null) { query = query.StartAfter(last); } query = query.Limit(filter.Limit); var res = await query.GetSnapshotAsync(); int k = res.Count; if (res.Count == 0) { //boloa break; } foreach (var v in res) { last = v; if (UserRoles.USER.Equals(role)) { var blocked = await IsBlocked(userId, v.Id); if (blocked.Data) { continue; } } Restaurant r = v.ConvertTo <Restaurant>(); r.Id = v.Id; list.Add(r); if (list.Count == filter.Limit) { break; } } //boloa if (k < filter.Limit) { break; } if (UserRoles.OWNER.Equals(role)) { break; } if (list.Count == filter.Limit) { break; } } return(new ApiResponse <List <Restaurant> > { Data = list, Success = true }); } catch { return(new ApiResponse <List <Restaurant> > { Success = false, ErrorCode = ErrorCodes.UNKNOWN_ERROR }); } }
public IEnumerable <ViewRestaurant> GetRestaurants(RestaurantFilter restaurantFilter) => _restaurantTask.GetRestaurants(restaurantFilter);
public IEnumerable <string> GetRestaurantNames([FromBody] RestaurantFilter restaurantFilter) { return(restaurantFilter == null? _restaurantTask.GetRestaurants(null).Select(p => p.Name) : _restaurantTask.GetRestaurants(restaurantFilter).Select(p => p.Name)); }
public async Task <OkObjectResult> Get([FromQuery] RestaurantFilter filter) { List <Restaurant> restaurants = await YelpQueryService.GetRestaurants(filter); return(Ok(JsonConvert.SerializeObject(restaurants))); }
public async Task <IEnumerable <RestaurantViewModel> > GetAllWithFilter([FromQuery] RestaurantFilter filter) { return(_mapper.Map <IEnumerable <RestaurantViewModel> >(await _restaurantService.GetAllWithFilter(filter))); }