public IActionResult ByRestrictions([FromBody] IngredientsRestrictionsRequest req) { var itemsByStore = _productRepository.ListItemsByStoreId(req.StoreId); if (itemsByStore.Any()) { var itemsWithRestriction = itemsByStore.Where(item => item.Ingredients.All(ingredient => !req.Restrictions.Any(x => ingredient.Name.ToUpper().Contains(x.ToUpper())))); var responseRestrictionIngredients = MapResponseRestrictionItems(itemsWithRestriction); if (responseRestrictionIngredients.Any()) { return(Ok(responseRestrictionIngredients)); } } return(NotFound()); }
public async Task <IList <Product> > GetProductsByRestrictionsAsync(string storeName, string[] Restrictions) { IList <IngredientsRestrictionsResponse> ingredientsRestrictions = null; try { await _logServiceBus.SendMessagesAsync($"Recuperando produtos com as restrições \"{string.Join(',', Restrictions)} \"; storeName = {storeName}\"..."); IngredientsRestrictionsRequest ingredientsRestrictionsRequest = new IngredientsRestrictionsRequest() { Restrictions = Restrictions.ToList(), StoreId = GetStoreID(storeName) }; var content = new StringContent(JsonConvert.SerializeObject(ingredientsRestrictionsRequest), Encoding.Default, "application/json"); var result = await _httpClient.PostAsync($"{_configuration["API:Ingredients"]}", content); if (result.IsSuccessStatusCode) { ingredientsRestrictions = JsonConvert.DeserializeObject <IList <IngredientsRestrictionsResponse> >(await result.Content.ReadAsStringAsync()); } else { throw new Exception(result.StatusCode.ToString()); } } catch (Exception E) { await _logServiceBus.SendMessagesAsync($"Falha ao recuperar produtos com as restrições, segue a descrição \"{E.Message}\"."); if (_configuration["API:mocked"] == "true") { await _logServiceBus.SendMessagesAsync($"Criando dados fakes para produtos com as restrições"); var listProducts = new List <IngredientsRestrictionsResponse>() { new IngredientsRestrictionsResponse() { Ingredients = new List <string>() { "soy", "sugar" }, ProductId = Guid.NewGuid() }, new IngredientsRestrictionsResponse() { Ingredients = new List <string>() { "mustard", "onion" }, ProductId = Guid.NewGuid() }, new IngredientsRestrictionsResponse() { Ingredients = new List <string>() { "lettuce", "olive oil" }, ProductId = Guid.NewGuid() } }; ingredientsRestrictions = listProducts.Where(produto => produto.Ingredients.All(ingrediente => !Restrictions.Contains(ingrediente))) .ToList(); } else { throw; } } return(ingredientsRestrictions.Select(x => x.ToProduct()).ToList()); }