internal TrainingDay UpdateTrainingDay(TrainingDay trainingDay, TrainingDayScenario trainingDayScenario)
        {
            TrainingDay trainingDayResult = null;

            trainingDayResult = _trainingDayModule.Update(trainingDay, AppUtils.GetUserUnit(_userInfosService, trainingDay.UserId));
            SynchroManager.TrainingDayChange(DbContext, trainingDayResult);

            if (trainingDayScenario != null && trainingDayScenario.ManageExercise)
            {
                var trainingExerciseCriteria = new TrainingExerciseCriteria()
                {
                    UserId = new StringCriteria()
                    {
                        Equal = trainingDay.UserId
                    },
                    Year = new IntegerCriteria()
                    {
                        Equal = trainingDay.Year
                    },
                    WeekOfYear = new IntegerCriteria()
                    {
                        Equal = trainingDay.WeekOfYear
                    },
                    DayOfWeek = new IntegerCriteria()
                    {
                        Equal = trainingDay.DayOfWeek
                    },
                    TrainingDayId = new IntegerCriteria()
                    {
                        Equal = trainingDay.TrainingDayId
                    }
                };
                var trainingExercisesDb = _trainingExercisesService.FindTrainingExercise(trainingExerciseCriteria);
                if (trainingExercisesDb != null && trainingExercisesDb.Count > 0)
                {
                    foreach (var trainingExerciseDb in trainingExercisesDb)
                    {
                        //remove only training exercises who do not present (for keep exercise tempos: retrocompatibility)
                        if (trainingDay.TrainingExercises == null || !trainingDay.TrainingExercises.Any(te => te.Id == trainingExerciseDb.Id))
                        {
                            _trainingExercisesService.DeleteTrainingExercise(trainingExerciseDb);
                        }
                    }
                }

                if (trainingDay.TrainingExercises != null)
                {
                    trainingDayResult.TrainingExercises = new List <TrainingExercise>();
                    foreach (var trainingExercise in trainingDay.TrainingExercises)
                    {
                        trainingDayResult.TrainingExercises.Add(_trainingExercisesService.UpdateTrainingExercise(trainingExercise, true));
                    }
                }
            }

            return(trainingDayResult);
        }
        internal TrainingDay CreateTrainingDay(TrainingDay trainingDay)
        {
            var trainingDayCriteria = new TrainingDayCriteria()
            {
                UserId = new StringCriteria()
                {
                    Equal = trainingDay.UserId
                },
                Year = new IntegerCriteria()
                {
                    Equal = trainingDay.Year
                },
                WeekOfYear = new IntegerCriteria()
                {
                    Equal = trainingDay.WeekOfYear
                },
                DayOfWeek = new IntegerCriteria()
                {
                    Equal = trainingDay.DayOfWeek
                },
            };
            var trainingDayScenario = new TrainingDayScenario()
            {
                ManageExercise = false
            };
            var trainingDayList = FindTrainingDay(AppUtils.GetUserUnit(_userInfosService, trainingDay.UserId), trainingDayCriteria, trainingDayScenario);
            int trainingDayId   = 1;

            if (trainingDayList != null && trainingDayList.Count > 0)
            {
                trainingDayId = trainingDayList.Max(td => td.TrainingDayId) + 1;
            }

            trainingDay.TrainingDayId = trainingDayId;
            // no need transaction, only header

            TrainingDay trainingDayResult = null;

            trainingDayResult = _trainingDayModule.Create(trainingDay, AppUtils.GetUserUnit(_userInfosService, trainingDay.UserId));
            SynchroManager.TrainingDayChange(DbContext, trainingDayResult);

            if (trainingDay.TrainingExercises != null)
            {
                trainingDayResult.TrainingExercises = new List <TrainingExercise>();
                foreach (var trainingExercise in trainingDay.TrainingExercises)
                {
                    trainingDayResult.TrainingExercises.Add(_trainingExercisesService.CreateTrainingExercise(trainingExercise));
                }
            }

            return(trainingDayResult);
        }
        public void DeleteTrainingExercise(TrainingExerciseKey key)
        {
            var trainingExercise = GetTrainingExercise(key);

            if (trainingExercise != null)
            {
                _trainingExerciseModule.Delete(trainingExercise);
                SynchroManager.TrainingExerciseChange(DbContext, trainingExercise, true);

                if (trainingExercise.TrainingExerciseSets != null)
                {
                    foreach (var trainingExerciseSet in trainingExercise.TrainingExerciseSets)
                    {
                        _trainingExerciseSetModule.Delete(trainingExerciseSet);
                    }
                }
            }
        }
        public TrainingExercise CreateTrainingExercise(TrainingExercise trainingExercise)
        {
            var result = _trainingExerciseModule.Create(trainingExercise);

            SynchroManager.TrainingExerciseChange(DbContext, result);

            if (result != null && trainingExercise.TrainingExerciseSets != null)
            {
                TrainingExerciseSet trainingExerciseSet;
                result.TrainingExerciseSets = new List <TrainingExerciseSet>();
                foreach (var set in trainingExercise.TrainingExerciseSets)
                {
                    trainingExerciseSet = _trainingExerciseSetModule.Create(set);
                    result.TrainingExerciseSets.Add(trainingExerciseSet);
                }
            }

            return(result);
        }
        public void DeleteTrainingDay(TrainingDayKey key)
        {
            var trainingDayScenario = new TrainingDayScenario()
            {
                ManageExercise = true
            };
            var trainingDay = GetTrainingDay(key, trainingDayScenario);

            if (trainingDay != null)
            {
                _trainingDayModule.Delete(trainingDay);
                SynchroManager.TrainingDayChange(DbContext, trainingDay, true);

                if (trainingDay.TrainingExercises != null)
                {
                    foreach (var trainingExercise in trainingDay.TrainingExercises)
                    {
                        _trainingExercisesService.DeleteTrainingExercise(trainingExercise);
                    }
                }
            }
        }
        public TrainingExercise UpdateTrainingExercise(TrainingExercise trainingExercise, bool manageDeleteLinkItem)
        {
            var result = _trainingExerciseModule.Update(trainingExercise);

            SynchroManager.TrainingExerciseChange(DbContext, result);

            if (result != null && trainingExercise.TrainingExerciseSets != null)
            {
                if (manageDeleteLinkItem)
                { // optimisation : massive delete
                    _trainingExerciseSetModule.Delete(new TrainingExerciseSetKey()
                    {
                        UserId             = trainingExercise.UserId,
                        Year               = trainingExercise.Year,
                        WeekOfYear         = trainingExercise.WeekOfYear,
                        DayOfWeek          = trainingExercise.DayOfWeek,
                        TrainingDayId      = trainingExercise.TrainingDayId,
                        TrainingExerciseId = trainingExercise.Id
                    });

                    result.TrainingExerciseSets = new List <TrainingExerciseSet>();
                    foreach (var set in trainingExercise.TrainingExerciseSets)
                    {
                        result.TrainingExerciseSets.Add(_trainingExerciseSetModule.Create(set));
                    }
                }
                else
                {
                    result.TrainingExerciseSets = new List <TrainingExerciseSet>();
                    foreach (var set in trainingExercise.TrainingExerciseSets)
                    {
                        result.TrainingExerciseSets.Add(_trainingExerciseSetModule.Update(set));
                    }
                }
            }

            return(result);
        }