public async Task <IActionResult> Search(string name) { IEnumerable <FoodSearchResult> foods; if (EanUtils.IsAllNumbers(name)) { foods = nutritionRepository.SearchFoodsByEan(name, CurrentUserId); if (!foods.Any()) { var extFood = await SearchExternalFood(name); if (extFood != null) { var newFood = new FoodDetails { UserId = CurrentUserId, Ean = name, Manufacturer = extFood.Manufacturer, Name = extFood.Name, Nutrients = extFood.Nutrients.Select(n => new FoodNutrientAmount { NutrientId = n.NutrientId, Amount = n.Amount }).ToArray() }; nutritionRepository.CreateFood(newFood); foods = new[] { new FoodSearchResult { Id = newFood.Id, Created = newFood.Created, Ean = newFood.Ean, Manufacturer = newFood.Manufacturer, Name = newFood.Name, UserId = newFood.UserId } }; } } } else { foods = nutritionRepository.SearchFoods(name.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), CurrentUserId); } foods = foods.OrderBy(f => f.LatestUse.HasValue || f.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase) ? 1 : 2) .ThenBy(f => f.Name.StartsWith(name, StringComparison.CurrentCultureIgnoreCase) ? 1 : 2) .ThenBy(f => f.Name); var response = Mapper.Map <FoodSearchResultResponse[]>(foods); return(Ok(response)); }
public async Task <IActionResult> SearchExternal(string ean) { if (string.IsNullOrWhiteSpace(ean)) { return(NoContent()); } if (EanUtils.IsInternalEan13(ean)) { ean = EanUtils.NormalizeInternalEan13(ean); } var response = await SearchExternalFood(ean); if (response != null) { return(Ok(response)); } return(NoContent()); }