Ejemplo n.º 1
0
        public async Task <IActionResult> PutCategory(int id, Category category)
        {
            if (id != category.Id)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutUserProfile(int id, UserProfile userProfile)
        {
            if (id != userProfile.Id)
            {
                return(BadRequest());
            }

            _context.Entry(userProfile).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutAllergies(int id, Allergies allergies)
        {
            if (id != allergies.Id)
            {
                return(BadRequest());
            }

            _context.Entry(allergies).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AllergiesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <User> > PostAdmin([FromBody] User user)
        {
            var role = _context.Role.SingleOrDefault(r => r.RoleName == "Admin");

            user.Role = role;
            _context.User.Add(user);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetUser", new { id = user.Id }, user));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <Recipe> > PutRecipe(int id, Recipe recipe)
        {
            var ExistingAllergies = _recipeRepository.GetAllergiesByRecipeId(id);

            foreach (var Allergy in ExistingAllergies)
            {
                if (!recipe.Allergies.Any(c => c.AllergyName == Allergy.AllergyName))
                {
                    _context.Allergies.Remove(Allergy);
                }
            }

            foreach (var Allergy in recipe.Allergies)
            {
                var existingAllergy = ExistingAllergies.FirstOrDefault(x => x.AllergyName == Allergy.AllergyName);

                if (existingAllergy != null)
                {
                    Console.WriteLine("Allergy exist");
                }
                else
                {
                    // Insert child
                    var Allergys = new Allergies
                    {
                        RecipeId    = recipe.Id,
                        AllergyName = Allergy.AllergyName
                    };
                    _context.Allergies.Add(Allergys);
                    Console.WriteLine("New Allergy added");
                }
            }

            var ExistingNutrition = _recipeRepository.GetNutritionsByRecipeId(id);

            foreach (var Nutrition in ExistingNutrition)
            {
                if (!recipe.Nutritions.Any(c => c.Nutrition == Nutrition.Nutrition))
                {
                    _context.Recipe_Nutrition.Remove(Nutrition);
                }
            }

            foreach (var Nutritions in recipe.Nutritions)
            {
                var existingNutrition = ExistingNutrition.FirstOrDefault(x => x.Nutrition == Nutritions.Nutrition);

                if (existingNutrition != null)
                {
                    Console.WriteLine("Nutrition exist");
                }
                else
                {
                    // Insert child
                    var Nutrition = Nutritions;
                    _context.Recipe_Nutrition.Add(Nutrition);
                }
            }



            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiBadRequestResponse(ModelState)));
            }

            if (id != recipe.Id)
            {
                return(NotFound(new ApiResponse(404, $"Recipe not found with id {id}")));
            }

            _context.Entry(recipe).State = EntityState.Modified;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(id))
                {
                    return(NotFound(new ApiResponse(200, $"Recipe not found with id {id}")));
                }
                else
                {
                    throw;
                }
            }

            //var data = _context.Recipe.Where(r => r.Id == recipe.Id);

            return(Ok(new ApiResponse(404, $"Recipe Sat {id}")));
        }