Exemplo n.º 1
0
        public Recipe FindRecipeById(int recipeId, OdbcConnection connection)
        {
            RecipeDto recipeDto;

            var commandText = $"SELECT {Recipes.Name} FROM {Recipes.TableName} WHERE {Recipes.Id} = {recipeId};";
            var command     = new OdbcCommand(commandText, connection);

            using (OdbcDataReader reader = command.ExecuteReader())
            {
                if (!reader.Read())
                {
                    throw new RecipeIdNotFoundOdbcDataException(recipeId);
                }

                recipeDto = new RecipeDto
                {
                    Id   = recipeId,
                    Name = reader.GetString(0)
                };
            }

            IEnumerable <Component> components = componentDataProvider.FindComponentsByRecipeId(recipeDto.Id, connection);

            return(new Recipe(
                       id: recipeDto.Id,
                       name: recipeDto.Name,
                       components: components
                       ));
        }
Exemplo n.º 2
0
        public IEnumerable <Recipe> FindAllRecipes(OdbcConnection connection)
        {
            var recipeDtos = new List <RecipeDto>();

            var commandText = $"SELECT {Recipes.Id} FROM {Recipes.TableName};";
            var command     = new OdbcCommand(commandText, connection);

            using (OdbcDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var recipeDto = new RecipeDto
                    {
                        Id = reader.GetInt32(0)
                    };

                    recipeDtos.Add(recipeDto);
                }
            }

            var recipes = new List <Recipe>();

            foreach (RecipeDto recipeDto in recipeDtos)
            {
                Recipe recipe = FindRecipeById(recipeDto.Id, connection);
                recipes.Add(recipe);
            }

            return(recipes);
        }