Example #1
0
        public override async Task <FoodResponse> FeedMe(IAsyncStreamReader <FoodMessage> requestStream, ServerCallContext context)
        {
            int total = 0;
            var items = new List <int>();

            await  foreach (var input in requestStream.ReadAllAsync(context.CancellationToken))
            {
                total += input.Value;
                items.Add(input.Value);
                _logger.LogInformation($"Nom Nom Nom {input.Value}");
                _logger.LogInformation($"More?");
            }

            var message = total switch
            {
                0 => "I am hungry",
                _ when total > 10 => "Some what full",
                _ when total > 300 => "I am full",
                _ => "What happed here?"
            };

            _logger.LogInformation(message);
            var response = new FoodResponse {
                Total = total, Message = message
            };

            response.AllValues.Add(items);
            return(response);
        }
Example #2
0
        // TODO: Doing a search for 'test' takes 12 seconds, so that's no good
        public async Task <IEnumerable <FoodResponse> > SearchFoods(string query, IEnumerable <string> searchFilter, int userId)
        {
            var foods            = new Food[] { };
            var foodsWithDetails = new FoodResponse[] { };

            // If attributes, query attributes tables, get ids of attributes, then go to vote tables, then get food ids from matching records
            if (searchFilter.Contains(FoodSearchType.Attributes))
            {
                var flavors = await _attributes.SearchAttributes(query, VotableAttributeType.Flavor);

                var f = flavors
                        .Join(_mouthfeel.AttributeVotes, flavor => flavor.Id, vote => vote.AttributeId, (flavor, vote) => new { flavor.Id, flavor.Name, vote.FoodId })
                        .Join(_mouthfeel.Foods, fv => fv.FoodId, food => food.Id, (fv, food) => new Food {
                    Id = food.Id, Name = food.Name, ImageUrl = food.ImageUrl
                });

                var textures = await _attributes.SearchAttributes(query, VotableAttributeType.Texture);

                var t = textures
                        .Join(_mouthfeel.AttributeVotes, texture => texture.Id, vote => vote.AttributeId, (texture, vote) => new { texture.Id, texture.Name, vote.FoodId })
                        .Join(_mouthfeel.Foods, tv => tv.FoodId, food => food.Id, (tv, food) => new Food {
                    Id = food.Id, Name = food.Name, ImageUrl = food.ImageUrl
                });

                var misc = await _attributes.SearchAttributes(query, VotableAttributeType.Miscellaneous);

                var m = misc
                        .Join(_mouthfeel.AttributeVotes, mis => mis.Id, vote => vote.AttributeId, (mis, vote) => new { mis.Id, mis.Name, vote.FoodId })
                        .Join(_mouthfeel.Foods, mv => mv.FoodId, food => food.Id, (mv, food) => new Food {
                    Id = food.Id, Name = food.Name, ImageUrl = food.ImageUrl
                });

                foods = foods.Concat(f).Concat(t).Concat(m).ToArray();
            }

            if (searchFilter.Contains(FoodSearchType.Ingredients))
            {
                var ingredients = await _ingredients.SearchIngredients(query);

                var compositions  = (await _mouthfeel.FoodCompositions.ToListAsync()).Where(c => ingredients.Any(i => i.Id == c.IngredientId));
                var matchingFoods = (await _mouthfeel.Foods.ToListAsync()).Where(f => compositions.Any(c => c.FoodId == f.Id));
                foods = foods.Concat(matchingFoods).ToArray();
            }

            if (searchFilter.Contains(FoodSearchType.Name))
            {
                var byName = (await _mouthfeel.Foods.ToListAsync()).Where(f => f.Name.ToLower().Contains(query.ToLower()));
                foods = foods.Concat(byName).ToArray();
            }

            foods = foods.DistinctBy(f => f.Name).ToArray();

            return(await GetManyFoodDetails(foods.Select(f => f.Id), userId));
        }
Example #3
0
        public async Task <Response> NewFoodAsync(string urlBase, string servicePrefix, string controller, FoodRequest model, string tokenType, string accessToken)
        {
            try
            {
                string        request = JsonConvert.SerializeObject(model);
                StringContent content = new StringContent(request, Encoding.UTF8, "application/json");
                HttpClient    client  = new HttpClient
                {
                    BaseAddress = new Uri(urlBase)
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
                string url = $"{servicePrefix}{controller}";
                HttpResponseMessage response = await client.PostAsync(url, content);

                string answer = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(new Response
                    {
                        IsSuccess = false,
                        Message = answer,
                    });
                }

                FoodResponse trip = JsonConvert.DeserializeObject <FoodResponse>(answer);
                return(new Response
                {
                    IsSuccess = true,
                    Result = trip,
                });
            }
            catch (Exception ex)
            {
                return(new Response
                {
                    IsSuccess = false,
                    Message = ex.Message,
                });
            }
        }
Example #4
0
        public async Task <IActionResult> GetFoodsAsync()
        {
            FoodResponse foodResponse = new FoodResponse();

            try
            {
                foodResponse.FoodViewModels = await _foodService.GetAllFoodViewModelsAsync();

                foodResponse.IsSuccess = true;
                foodResponse.Status    = (int)HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                foodResponse.Message        = ex.Message;
                foodResponse.MessageDetails = ex.ToString();
                foodResponse.Status         = foodResponse.Status > 0 ? foodResponse.Status : (int)HttpStatusCode.BadRequest;
            }

            return(StatusCode(foodResponse.Status, foodResponse));
        }
 public IActionResult SubmissionForm(FoodResponse foodResponse)
 {
     Hold.AddFood(foodResponse);
     return(View("Output", foodResponse));
 }