示例#1
0
        public void UpdateNutritionGoal(NutritionGoalDetails goal)
        {
            foreach (var period in goal.Periods.Where(p => p.Id == Guid.Empty))
            {
                period.Id = Guid.NewGuid();
            }
            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        var periodFilter = "SELECT Id FROM NutritionGoalPeriod WHERE NutritionGoalId=@Id";
                        conn.Execute($"DELETE FROM NutritionGoalValue WHERE NutritionGoalPeriodId IN ({periodFilter})", new { goal.Id }, tran);
                        conn.Execute($"DELETE FROM NutritionGoalMeal WHERE NutritionGoalPeriodId IN ({periodFilter})", new { goal.Id }, tran);
                        conn.Execute("DELETE FROM NutritionGoalPeriod WHERE NutritionGoalId=@Id", new { goal.Id }, tran);

                        conn.Execute("UPDATE NutritionGoal SET Name=@Name WHERE Id=@Id", goal, tran);
                        conn.Execute("INSERT NutritionGoalPeriod(Id,NutritionGoalId,[Index],Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,ExerciseDay,RestDay,WholeDay) VALUES (@Id,@NutritionGoalId,@index,@Monday,@Tuesday,@Wednesday,@Thursday,@Friday,@Saturday,@Sunday,@ExerciseDay,@RestDay,@WholeDay)", goal.Periods.Select((p, index) => new
                        {
                            p.Id,
                            NutritionGoalId = goal.Id,
                            index,
                            p.Monday,
                            p.Tuesday,
                            p.Wednesday,
                            p.Thursday,
                            p.Friday,
                            p.Saturday,
                            p.Sunday,
                            p.ExerciseDay,
                            p.RestDay,
                            p.WholeDay
                        }), tran);
                        conn.Execute("INSERT INTO NutritionGoalMeal(NutritionGoalPeriodId,MealDefinitionId) VALUES(@Id,@MealDefinitionId)", goal.Periods.SelectMany(p => p.MealDefinitions.Select(m => new
                        {
                            p.Id,
                            MealDefinitionId = m
                        })), tran);
                        conn.Execute("INSERT INTO NutritionGoalValue(NutritionGoalPeriodId,NutrientId, Min,Max) VALUES(@Id,@NutrientId,@Min,@Max)", goal.Periods.SelectMany(p => p.Nutrients.Select(n => new
                        {
                            p.Id,
                            n.NutrientId,
                            n.Min,
                            n.Max
                        })), tran);
                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
        }
示例#2
0
        public void CreateNutritionGoal(NutritionGoalDetails goal)
        {
            goal.Id = Guid.NewGuid();
            foreach (var period in goal.Periods)
            {
                period.Id = Guid.NewGuid();
            }

            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        conn.Execute("INSERT INTO NutritionGoal(Id,UserId,Name,Active,Created) VALUES(@Id,@UserId,@Name,@Active,@Created)", goal, tran);
                        conn.Execute("INSERT NutritionGoalPeriod(Id,NutritionGoalId,[Index],Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,ExerciseDay,RestDay,WholeDay) VALUES (@Id,@NutritionGoalId,@index,@Monday,@Tuesday,@Wednesday,@Thursday,@Friday,@Saturday,@Sunday,@ExerciseDay,@RestDay,@WholeDay)", goal.Periods.Select((p, index) => new
                        {
                            p.Id,
                            NutritionGoalId = goal.Id,
                            index,
                            p.Monday,
                            p.Tuesday,
                            p.Wednesday,
                            p.Thursday,
                            p.Friday,
                            p.Saturday,
                            p.Sunday,
                            p.ExerciseDay,
                            p.RestDay,
                            p.WholeDay
                        }), tran);
                        conn.Execute("INSERT INTO NutritionGoalMeal(NutritionGoalPeriodId,MealDefinitionId) VALUES(@Id,@MealDefinitionId)", goal.Periods.SelectMany(p => p.MealDefinitions.Select(m => new
                        {
                            p.Id,
                            MealDefinitionId = m
                        })), tran);
                        conn.Execute("INSERT INTO NutritionGoalValue(NutritionGoalPeriodId,NutrientId, Min,Max) VALUES(@Id,@NutrientId,@Min,@Max)", goal.Periods.SelectMany(p => p.Nutrients.Select(n => new
                        {
                            p.Id,
                            n.NutrientId,
                            n.Min,
                            n.Max
                        })), tran);
                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
        }