Exemplo n.º 1
0
        public async Task <IActionResult> CreateRecipe([FromBody] RecipeModel model)
        {
            string recipeError = ValidateRecipe(model);

            if (!String.IsNullOrEmpty(recipeError))
            {
                return(BadRequest(recipeError));
            }

            // create Recipe
            Recipe recipe = new Recipe();

            recipe.Name            = model.name;
            recipe.PreparationTime = model.preparationTime;
            recipe.Servings        = model.servings;
            context.Recipe.Add(recipe);

            // create RecipeFrontImage
            RecipeFrontImage recipeFrontImage = new RecipeFrontImage();

            recipeFrontImage.FrontImage = FileHelper.EncodeString(model.frontImage);
            context.RecipeFrontImage.Add(recipeFrontImage);
            recipeFrontImage.RecipeNavigation = recipe;

            // create multiple RecipeIngredientMeasure
            foreach (IngredientModel ingredient in model.ingredients)
            {
                RecipeIngredientMeasure recipeIngredientMeasure = new RecipeIngredientMeasure();
                recipeIngredientMeasure.Ingredient       = Int32.Parse(ingredient.ingredientValue);
                recipeIngredientMeasure.Amount           = ingredient.amount;
                recipeIngredientMeasure.Measure          = Int32.Parse(ingredient.measureValue);
                recipeIngredientMeasure.RecipeNavigation = recipe;
                context.RecipeIngredientMeasure.Add(recipeIngredientMeasure);
            }

            // create multiple RecipeIngredientMeasure
            foreach (DirectionModel direction in model.directions)
            {
                RecipeDirection recipeDirection = new RecipeDirection();
                recipeDirection.Sort             = direction.sortNumber;
                recipeDirection.Direction        = direction.description.Trim();
                recipeDirection.RecipeNavigation = recipe;
                context.RecipeDirection.Add(recipeDirection);
            }

            // create multiple RecipeCategory
            foreach (FilterModel category in model.categories)
            {
                RecipeCategory recipeCategory = new RecipeCategory();
                recipeCategory.Category         = Int32.Parse(category.value);
                recipeCategory.RecipeNavigation = recipe;
                context.RecipeCategory.Add(recipeCategory);
            }

            await context.SaveChangesAsync();

            return(Ok(recipe.Id));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateRecipe(int recipeId, [FromBody] RecipeModel model)
        {
            string recipeError = ValidateRecipe(model);

            if (!String.IsNullOrEmpty(recipeError))
            {
                return(BadRequest(recipeError));
            }

            // update Recipe
            Recipe recipe = await context.Recipe.SingleOrDefaultAsync(x => x.Id == recipeId);

            if (recipe == null)
            {
                return(NotFound("Not Found Recipe"));
            }
            recipe.Name = model.name;
            context.Recipe.Update(recipe);

            // update RecipeFrontImage
            RecipeFrontImage recipeFrontImage = await context.RecipeFrontImage.SingleOrDefaultAsync(x => x.Recipe == recipeId);

            if (recipeFrontImage == null)
            {
                return(NotFound("Not Found Recipe Front Image"));
            }
            recipeFrontImage.FrontImage = FileHelper.EncodeString(model.frontImage);
            context.RecipeFrontImage.Update(recipeFrontImage);

            // delete old RecipeIngredientMeasures
            var recipeIngredientMeasures = await context.RecipeIngredientMeasure.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeIngredientMeasure.RemoveRange(recipeIngredientMeasures);
            // create multiple RecipeIngredientMeasure
            foreach (IngredientModel ingredient in model.ingredients)
            {
                RecipeIngredientMeasure recipeIngredientMeasure = new RecipeIngredientMeasure();
                recipeIngredientMeasure.Ingredient       = Int32.Parse(ingredient.ingredientValue);
                recipeIngredientMeasure.Amount           = ingredient.amount;
                recipeIngredientMeasure.Measure          = Int32.Parse(ingredient.measureValue);
                recipeIngredientMeasure.RecipeNavigation = recipe;
                context.RecipeIngredientMeasure.Add(recipeIngredientMeasure);
            }

            // delete old RecipeDirections
            var recipeDirections = await context.RecipeDirection.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeDirection.RemoveRange(recipeDirections);
            // create multiple RecipeDirections
            foreach (DirectionModel direction in model.directions)
            {
                RecipeDirection recipeDirection = new RecipeDirection();
                recipeDirection.Sort             = direction.sortNumber;
                recipeDirection.Direction        = direction.description.Trim();
                recipeDirection.RecipeNavigation = recipe;
                context.RecipeDirection.Add(recipeDirection);
            }

            // delete old RecipeCategory
            var recipeCategories = await context.RecipeCategory.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeCategory.RemoveRange(recipeCategories);
            // create multiple RecipeCategory
            foreach (FilterModel category in model.categories)
            {
                RecipeCategory recipeCategory = new RecipeCategory();
                recipeCategory.Category         = Int32.Parse(category.value);
                recipeCategory.RecipeNavigation = recipe;
                context.RecipeCategory.Add(recipeCategory);
            }

            await context.SaveChangesAsync();

            return(Ok(recipe.Id));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetRecipe(int id)
        {
            RecipeModel recipeModel = new RecipeModel();

            // get Recipe
            Recipe recipe = await context.Recipe.Where(x => x.Id == id).FirstOrDefaultAsync();

            if (recipe == null)
            {
                return(NotFound("Can't find recipe by id: " + id));
            }
            recipeModel.id              = recipe.Id;
            recipeModel.name            = recipe.Name;
            recipeModel.preparationTime = recipe.PreparationTime;
            recipeModel.servings        = recipe.Servings;

            // get RecipeFrontImage
            RecipeFrontImage recipeFrontImage = await context.RecipeFrontImage.Where(x => x.Recipe == id).FirstOrDefaultAsync();

            if (recipe == null)
            {
                return(NotFound("Can't find recipe image by recipe id: " + id));
            }
            recipeModel.frontImage = FileHelper.DecodeString(recipeFrontImage.FrontImage);

            // get RecipeIngredientMeasure
            List <IngredientModel> ingredientModels = new List <IngredientModel>();
            var recipeIngredientMeasures            = await context.RecipeIngredientMeasure.Where(x => x.Recipe == id).ToListAsync();

            foreach (var recipeIngredientMeasure in recipeIngredientMeasures)
            {
                Ingredient ingredient = context.Ingredient.Where(x => x.Id == recipeIngredientMeasure.Ingredient).FirstOrDefault();
                Measure    measure    = context.Measure.Where(x => x.Id == recipeIngredientMeasure.Measure).FirstOrDefault();

                IngredientModel ingredientModel = new IngredientModel();
                ingredientModel.id              = Guid.NewGuid().ToString();
                ingredientModel.ingredientName  = ingredient.Name;
                ingredientModel.ingredientValue = ingredient.Id.ToString();
                ingredientModel.amount          = recipeIngredientMeasure.Amount;
                ingredientModel.measureName     = measure.Name;
                ingredientModel.measureValue    = measure.Id.ToString();
                ingredientModel.flag            = false;

                ingredientModels.Add(ingredientModel);
            }
            recipeModel.ingredients = ingredientModels;


            // get RecipeDirection
            List <DirectionModel> directionModels = new List <DirectionModel>();
            var recipeDirections = await context.RecipeDirection.Where(x => x.Recipe == id).ToListAsync();

            foreach (var recipeDirection in recipeDirections)
            {
                DirectionModel directionModel = new DirectionModel();
                directionModel.id          = Guid.NewGuid().ToString();
                directionModel.sortNumber  = recipeDirection.Sort;
                directionModel.description = recipeDirection.Direction;
                directionModels.Add(directionModel);
            }
            recipeModel.directions = directionModels;

            // get RecipeCategory
            List <FilterModel> categoryModels = new List <FilterModel>();
            var recipeCategories = await context.RecipeCategory.Where(x => x.Recipe == id).ToListAsync();

            var categories = await context.Category.ToListAsync();

            foreach (var recipeCategory in recipeCategories)
            {
                FilterModel categoryModel = new FilterModel();
                categoryModel.value = recipeCategory.Category.ToString();
                categoryModel.name  = categories.Where(x => x.Id == recipeCategory.Category).Select(x => x.Name).FirstOrDefault();
                categoryModels.Add(categoryModel);
            }
            recipeModel.categories = categoryModels;

            return(Ok(recipeModel));
        }