Пример #1
0
        public async Task <Recipe> UpdateRecipe(Recipe recipe)
        {
            DataRecipe returned     = BusinessToData(recipe);
            string     recipeAsJson = JsonSerializer.Serialize(returned, serializeOptions);

            Console.WriteLine(recipeAsJson);
            HttpContent content = new StringContent(
                recipeAsJson,
                Encoding.UTF8,
                "application/json");
            HttpResponseMessage response = await client.PutAsync(uri + "/UpdateRecipe", content);

            if (!response.IsSuccessStatusCode)
            {
                APIError apiError = JsonSerializer.Deserialize <APIError>(await response.Content.ReadAsStringAsync());
                throw new Exception($@"Error: {apiError.message}");
            }

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

            DataRecipe updatedRecipe = JsonSerializer.Deserialize <DataRecipe>(result,
                                                                               new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });
            Recipe secondReturned = DataToBusiness(updatedRecipe);

            Console.WriteLine("result" + result);
            return(secondReturned);
        }
Пример #2
0
        public Recipe DataToBusiness(DataRecipe dataRecipe)
        {
            Recipe returned = new Recipe();

            returned.Ingredient    = new List <RecipeItem>();
            returned.Name          = dataRecipe.Name;
            returned.Id            = dataRecipe.Id;
            returned.MinutesToMake = dataRecipe.MinutesToMake;
            returned.Description   = dataRecipe.Description;
            returned.Calories      = dataRecipe.Calories;
            returned.NutritionType = dataRecipe.NutritionType;
            returned.Image         = dataRecipe.Image;
            returned.Price         = dataRecipe.Price;

            List <string> ids      = dataRecipe.IngredientIdList.Split(",").ToList();
            List <string> quantity = dataRecipe.IngredientQuantityList.Split(",").ToList();

            for (int i = 0; i < ids.Count - 1; i++)
            {
                RecipeItem temp = new RecipeItem();
                temp.IngredientId = Convert.ToInt32(ids[i]);
                temp.Amount       = Convert.ToInt32(quantity[i]);
                returned.Ingredient.Add(temp);
            }

            return(returned);
        }
        public IDataRecipe Get(int id)
        {
            var success = new DataRecipe {
                Id   = id,
                Name = "Mock Data Recipe",
            };
            IDataRecipe fail = null;

            return(id < 0 ? fail : success);
        }
        public void Constructor_ThrowsArgumentExceptionOnDataRecipeIngredient()
        {
            IDataRecipe            recipe      = new DataRecipe();
            List <IDataIngredient> ingredients = new List <IDataIngredient> {
                new DataIngredient()
            };
            List <IDataRecipeIngredient> recipeIngredients = new List <IDataRecipeIngredient>();
            Action action = () => new Recipe(recipe, ingredients, recipeIngredients);

            action();
        }
        public void Get_ReturnIngredientsForRecipe2()
        {
            var recipeId = 0;
            var expected = new DataRecipe {
                Id   = 0,
                Name = "Recipe 1",
            };

            var actual = this.recipeRepository.Get(recipeId);
            var result = expected.Id == actual.Id &&
                         expected.Name == actual.Name;

            Assert.IsTrue(result);
        }
Пример #6
0
        public async Task <Recipe> GetRecipe(int id)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(uri + "/GetRecipe" + $"?id={@id}");

            if (!responseMessage.IsSuccessStatusCode)
            {
                APIError apiError = JsonSerializer.Deserialize <APIError>(await responseMessage.Content.ReadAsStringAsync());
                throw new Exception($@"Error: {apiError.message}");
            }

            string result = await responseMessage.Content.ReadAsStringAsync();

            DataRecipe gotRecipe = JsonSerializer.Deserialize <DataRecipe>(result,
                                                                           new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });
            Recipe secondReturned = DataToBusiness(gotRecipe);

            Console.WriteLine("result" + result);
            return(secondReturned);
        }
Пример #7
0
        public DataRecipe BusinessToData(Recipe recipe)
        {
            DataRecipe returned = new DataRecipe();

            returned.Name          = recipe.Name;
            returned.Id            = recipe.Id;
            returned.MinutesToMake = recipe.MinutesToMake;
            returned.Description   = recipe.Description;
            returned.Calories      = recipe.Calories;
            returned.NutritionType = recipe.NutritionType;
            returned.Image         = recipe.Image;
            returned.Price         = recipe.Price;

            foreach (var item in recipe.Ingredient)
            {
                returned.IngredientIdList       += item.IngredientId + ",";
                returned.IngredientQuantityList += item.Amount + ",";
            }

            return(returned);
        }