Ejemplo n.º 1
0
        public void SetGoals(UserGoalsDto dto)
        {
            using (var trans = context.Database.BeginTransaction())
            {
                var id = userManagement.GetCurrentUserId();

                var goal = context.UserGoal.FirstOrDefault(x => x.UserId.Equals(id));

                if (goal == null)
                {
                    context.UserGoal.Add(new UserGoal()
                    {
                        UserId = id,
                        Calories = dto.Calories,
                        Carbohydrates = dto.Carbohydrates,
                        Fat = dto.Fat,
                        Proteins = dto.Proteins,
                        PercentageCarbs = dto.PercentageCarbs,
                        PercentageFat = dto.PercentageFat,
                        PercentageProtein = dto.PercentageProtein
                    });

                    context.SaveChanges();
                }
                else
                {
                    goal.Calories = dto.Calories;
                    goal.Carbohydrates = dto.Carbohydrates;
                    goal.Fat = dto.Fat;
                    goal.Proteins = dto.Proteins;
                    goal.PercentageProtein = dto.PercentageProtein;
                    goal.PercentageFat = dto.PercentageFat;
                    goal.PercentageCarbs = dto.PercentageCarbs;

                    context.UserGoal.Update(goal);
                    context.SaveChanges();
                }

                trans.Commit();
            }
        }
Ejemplo n.º 2
0
        public IActionResult UserGoals(UserGoalsView dto)
        {
            if (dto != null)
            {
                try
                {
                    var userGoals = new UserGoalsDto()
                    {
                        UserId = dto.UserId,
                        Calories = dto.Calories,
                        Carbohydrates = dto.Carbohydrates,
                        Fat = dto.Fat,
                        Proteins = dto.Proteins,
                        PercentageCarbs = dto.PercentageCarbs,
                        PercentageFat = dto.PercentageFat,
                        PercentageProtein = dto.PercentageProtein
                    };

                    userGoalsManamgenet.SetGoals(userGoals);
                }
                catch (Exception exc)
                {
                    ModelState.TryAddModelError("AdditionalValidation", exc.Message);
                }
            }

            return View(dto);
        }