Exemplo n.º 1
0
        public async Task <IngredientModel> SearchIngredient(SearchSingle single)
        {
            try
            {
                using (IDbConnection connection = new SqlConnection(SqlHelper.connectionString))
                {
                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    single.name = removePlural(single.name);

                    // start search at data source 4 and decrementing data source until result is found
                    // exact word search
                    for (int i = 4; i > 0; i--)
                    {
                        var ingredient = await connection.QueryAsync <IngredientModel>($"SELECT TOP 1 F.ID, F.NAME, F.DIET_TYPE, D.NAME AS DIET_NAME, D.LEVEL AS DIET_LEVEL, " +
                                                                                       $"F.DESCRIPTION, F.[GROUP], F.SUB_GROUP, F.DATA_SOURCE FROM FOODS F INNER JOIN DIET_TYPES D ON(F.DIET_TYPE = D.ID) WHERE DATA_SOURCE = {i} AND F.NAME LIKE '{single.name}' OR F.NAME LIKE '%{single.name}%'");

                        if (ingredient != null && ingredient.Count() > 0)
                        {
                            return(ingredient.FirstOrDefault());
                        }
                    }
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Exemplo n.º 2
0
        public async Task <IngredientModel> SearchIngredient(SearchSingle single)
        {
            var data = await _ingredientService.SearchIngredient(single);

            if (data != null)
            {
                return(data);
            }
            return(null);
        }
        public async Task <ActionResult> SearchIngredient([FromBody] SearchSingle single)
        {
            var ingredient = await _ingredientRepository.SearchIngredient(single);

            if (ingredient == null)
            {
                return(StatusCode(403, new ErrorModel {
                    ErrorMessage = String.Format(ERROR_MESSAGE_SEARCH_SINGLE, single.name)
                }));
            }

            return(StatusCode(200, ingredient));
        }