public async Task <BaseResponse <List <Restaurant> > > GetRestaurantsAsync(RestaurantGetAllRequest request) { BaseResponse <List <Restaurant> > getRestaurantsResponse = new BaseResponse <List <Restaurant> >(); try { var filters = new List <FilterDefinition <Restaurant> >(); var builder = Builders <Restaurant> .Filter; if (!string.IsNullOrEmpty(request.CompanyId)) { filters.Add(builder.Eq(r => r.CompanyId, request.CompanyId)); } FilterDefinition <Restaurant> query = filters.Any() ? builder.And(filters) : builder.Empty; Task <List <Restaurant> > Restaurants = _mongoDatabase.GetCollection <Restaurant>(BEKLEMEYAPMA_RESTAURANT_COLLECTION_NAME) .Find(query) .Skip(request.Offset) .Limit(request.Limit) .ToListAsync(); Task <long> count = _mongoDatabase.GetCollection <Restaurant>(BEKLEMEYAPMA_RESTAURANT_COLLECTION_NAME) .CountDocumentsAsync(query); await Task.WhenAll(Restaurants, count); getRestaurantsResponse.Total = Convert.ToInt32(count.Result); getRestaurantsResponse.Data = Restaurants.Result; } catch (Exception ex) { getRestaurantsResponse.Errors.Add(ex.Message); _logger.LogError(ex, ex.Message); } return(getRestaurantsResponse); }
public async Task <IActionResult> Get([FromQuery] RestaurantGetAllRequest request) { BaseResponse <List <RestaurantGetResponse> > restaurants = await _restaurantsService.GetAllAsync(request); if (!restaurants.HasError && restaurants.Data != null && restaurants.Data.Any()) { PagedAPIResponse <List <RestaurantGetResponse> > response = new PagedAPIResponse <List <RestaurantGetResponse> >(); response.Items = new List <RestaurantGetResponse>(); response.Items.AddRange(restaurants.Data); PreprearePagination(request.Offset, request.Limit, restaurants.Total, "values", response); return(Ok(response)); } else if (!restaurants.HasError && restaurants.Data == null) { return(NotFound("No value found for requested filter.")); } else { return(BadRequest(restaurants.Errors)); } }
public async Task <IActionResult> Get([FromQuery] RestaurantGetAllRequest request) { BaseResponse <List <Restaurant> > RestaurantGetAllResponse = await _RestaurantService.GetRestaurantsAsync(request); if (RestaurantGetAllResponse.HasError) { return(BadRequest(RestaurantGetAllResponse.Errors)); } if (RestaurantGetAllResponse.Data == null || RestaurantGetAllResponse.Data.Count == 0) { return(NotFound("No Restaurant found for requested filter.")); } var response = new PagedAPIResponse <List <Restaurant> >(); response.Items = new List <Restaurant>(); response.Items.AddRange(RestaurantGetAllResponse.Data); PreparePagination(request.Offset, request.Limit, RestaurantGetAllResponse.Total, "Restaurants", response); return(Ok(response)); }
public async Task <BaseResponse <List <RestaurantGetResponse> > > GetAllAsync(RestaurantGetAllRequest request) { BaseResponse <List <RestaurantGetResponse> > response = new BaseResponse <List <RestaurantGetResponse> >(); var dataRequest = request.Map <RestaurantGetAllCoreRequest>(); try { if (request.Limit == 0) { request.Limit = 50; } var dataResponse = await _restaurantsRepository.GetAllAsync(dataRequest); List <RestaurantGetResponse> restaurants = dataResponse.Items.Map <List <RestaurantGetResponse> >(); if (restaurants != null && restaurants.Any()) { response.Data = new List <RestaurantGetResponse>(); response.Data.AddRange(restaurants); response.Total = response.Data.Count(); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); response.Errors.Add("An error occurred while processing your request."); } return(response); }