Beispiel #1
0
        new public TrainingDay Clone()
        {
            var copy = new TrainingDay(this);

            copy.BeginHour        = BeginHour;
            copy.EndHour          = EndHour;
            copy.ModificationDate = ModificationDate;
            copy.Unit             = Unit;
            if (TrainingExercises != null)
            {
                copy.TrainingExercises = new List <TrainingExercise>();
                foreach (var trainingExercise in TrainingExercises)
                {
                    if (trainingExercise == null)
                    {
                        copy.TrainingExercises.Add(null);
                    }
                    else
                    {
                        copy.TrainingExercises.Add(trainingExercise.Clone());
                    }
                }
            }
            return(copy);
        }
        /// <summary>
        /// Create data in database
        /// </summary>
        /// <param name="trainingJournalDay">Data</param>
        /// <returns>insert data</returns>
        public TrainingDay Create(TrainingDay trainingJournalDay)
        {
            if (trainingJournalDay == null || string.IsNullOrWhiteSpace(trainingJournalDay.UserId) ||
                trainingJournalDay.Year == 0 || trainingJournalDay.WeekOfYear == 0 ||
                trainingJournalDay.DayOfWeek < 0 || trainingJournalDay.DayOfWeek > 6 || trainingJournalDay.TrainingDayId == 0)
                return null;

            var row = new TrainingDayRow();
            TrainingDayTransformer.ToRow(trainingJournalDay, row);
            _dbContext.TrainingDay.Add(row);
            _dbContext.SaveChanges();
            return TrainingDayTransformer.ToBean(row);
        }
        public static void ToRow(TrainingDay bean, TrainingDayRow row)
        {
            if (bean == null)
                return;

            row.UserId = bean.UserId;
            row.Year = bean.Year;
            row.WeekOfYear = bean.WeekOfYear;
            row.DayOfWeek = bean.DayOfWeek;
            row.TrainingDayId = bean.TrainingDayId;
            row.BeginHour = DbUtils.DateToUtc(bean.BeginHour);
            row.EndHour = DbUtils.DateToUtc(bean.EndHour);
            row.ModificationDate = DbUtils.DateToUtc(Utils.DateTimeWithoutMs); // Set modificationDate
        }
Beispiel #4
0
        public static void TrainingDayChange(ApplicationDbContext dbContext, TrainingDay trainingDay, bool deleted=false)
        {
            if (trainingDay == null || (!deleted && trainingDay.ModificationDate == null))
                return;

            var trainingWeekKey = new TrainingWeekKey()
            {
                UserId = trainingDay.UserId,
                Year = trainingDay.Year,
                WeekOfYear = trainingDay.WeekOfYear
            };
            var modificationDate = deleted ? Utils.DateTimeWithoutMs : trainingDay.ModificationDate;
            UpdateTrainingWeekModificationDate(dbContext, modificationDate, trainingWeekKey);
        }
        internal static TrainingDay ToBean(TrainingDayRow row)
        {
            if (row == null)
                return null;

            var bean = new TrainingDay();
            bean.UserId = row.UserId;
            bean.Year = row.Year;
            bean.WeekOfYear = row.WeekOfYear;
            bean.DayOfWeek = row.DayOfWeek;
            bean.TrainingDayId = row.TrainingDayId;
            bean.BeginHour = DbUtils.DbDateToUtc(row.BeginHour);
            bean.EndHour = DbUtils.DbDateToUtc(row.EndHour);
            bean.ModificationDate = DbUtils.DbDateToUtc(row.ModificationDate);
            return bean;
        }
Beispiel #6
0
 public new TrainingDay Clone()
 {
     var copy = new TrainingDay(this);
     copy.BeginHour = BeginHour;
     copy.EndHour = EndHour;
     copy.ModificationDate = ModificationDate;
     if (TrainingExercises != null)
     {
         copy.TrainingExercises = new List<TrainingExercise>();
         foreach (var trainingExercise in TrainingExercises)
         {
             if (trainingExercise == null)
                 copy.TrainingExercises.Add(null);
             else
                 copy.TrainingExercises.Add(trainingExercise.Clone());
         }
     }
     return copy;
 }
        public static TrainingDayViewModel TrainingDayToViewModel(TrainingDay trainingDay, UserInfo userInfo)
        {
            var result = new TrainingDayViewModel()
            {
                UserId = trainingDay.UserId,
                Year = trainingDay.Year,
                WeekOfYear = trainingDay.WeekOfYear,
                DayOfWeek = trainingDay.DayOfWeek,
                TrainingDayId = trainingDay.TrainingDayId
            };

            //convert date to user timezone
            var timeZoneInfo = TimeZoneMapper.GetTimeZoneByOlsonName(userInfo.TimeZoneName);
            if (timeZoneInfo == null)
                timeZoneInfo = TimeZoneInfo.Local;
            result.BeginHour = TimeZoneInfo.ConvertTime(trainingDay.BeginHour, timeZoneInfo);
            result.EndHour = TimeZoneInfo.ConvertTime(trainingDay.EndHour, timeZoneInfo);

            return result;
        }
 public TrainingDay CreateTrainingDay(TrainingDay trainingDay)
 {
     TrainingDay result = null;
     BeginTransaction();
     try
     {
         result = GetTrainingDayManager().CreateTrainingDay(trainingDay);
         CommitTransaction();
     }
     catch (Exception exception)
     {
         _logger.LogCritical("Unable to create training day", exception);
         RollbackTransaction();
         throw exception;
     }
     finally
     {
         EndTransaction();
     }
     return result;
 }
        private List<TrainingExercise> FindTrainingExercise(TrainingDay trainingDay)
        {
            if (trainingDay == null)
                return null;

            var criteria = new TrainingExerciseCriteria()
            {
                Year = new IntegerCriteria() { Equal = trainingDay.Year },
                WeekOfYear = new IntegerCriteria() { Equal = trainingDay.WeekOfYear },
                DayOfWeek = new IntegerCriteria() { Equal = trainingDay.DayOfWeek },
                TrainingDayId = new IntegerCriteria() { Equal = trainingDay.TrainingDayId },
                UserId = new StringCriteria() { Equal = trainingDay.UserId }
            };

            return _trainingExercisesService.FindTrainingExercise(criteria);
        }
Beispiel #10
0
        /// <summary>
        /// Update data in database
        /// </summary>
        /// <param name="trainingJournalDay">data</param>
        /// <returns>updated data</returns>
        public TrainingDay Update(TrainingDay trainingJournalDay)
        {
            if (trainingJournalDay == null || string.IsNullOrWhiteSpace(trainingJournalDay.UserId) ||
                trainingJournalDay.Year == 0 || trainingJournalDay.WeekOfYear == 0 ||
                trainingJournalDay.DayOfWeek < 0 || trainingJournalDay.DayOfWeek > 6 || trainingJournalDay.TrainingDayId == 0)
                return null;

            var trainingJournalRow = _dbContext.TrainingDay.Where(t=>t.UserId == trainingJournalDay.UserId &&
                                                                             t.Year == trainingJournalDay.Year &&
                                                                             t.WeekOfYear == trainingJournalDay.WeekOfYear &&
                                                                             t.DayOfWeek == trainingJournalDay.DayOfWeek &&
                                                                             t.TrainingDayId == trainingJournalDay.TrainingDayId).FirstOrDefault();
            if (trainingJournalRow == null)
            { // No data in database
                return Create(trainingJournalDay);
            }
            else
            { //Modify Data in database
                TrainingDayTransformer.ToRow(trainingJournalDay, trainingJournalRow);
                _dbContext.SaveChanges();
                return TrainingDayTransformer.ToBean(trainingJournalRow);
            }
        }
 private void CompleteTrainingDayWithExercise(TrainingDay trainingJournalDay)
 {
     if (trainingJournalDay != null)
     {
         var trainingExerciseCriteria = new TrainingExerciseCriteria()
         {
             UserId = new StringCriteria() { Equal = trainingJournalDay.UserId },
             Year = new IntegerCriteria() { Equal = trainingJournalDay.Year },
             WeekOfYear = new IntegerCriteria() { Equal = trainingJournalDay.WeekOfYear },
             DayOfWeek = new IntegerCriteria() { Equal = trainingJournalDay.DayOfWeek },
             TrainingDayId = new IntegerCriteria() { Equal = trainingJournalDay.TrainingDayId }
         };
         trainingJournalDay.TrainingExercises = _trainingWeeksService.FindTrainingExercise(trainingExerciseCriteria);
     }
 }
        private void ChangeDayOnTrainingDay(TrainingDay trainingDay, int newDayOfWeek)
        {
            if (trainingDay == null)
                return;

            trainingDay.DayOfWeek = newDayOfWeek;
            if (trainingDay.TrainingExercises != null)
            {
                foreach (var trainingExercise in trainingDay.TrainingExercises)
                {
                    trainingExercise.DayOfWeek = newDayOfWeek;
                    if (trainingExercise.TrainingExerciseSets != null)
                    {
                        foreach (var set in trainingExercise.TrainingExerciseSets)
                        {
                            set.DayOfWeek = newDayOfWeek;
                        }
                    }
                }
            }
        }
        internal TrainingDay UpdateTrainingDay(TrainingDay trainingDay, TrainingDayScenario trainingDayScenario)
        {
            TrainingDay trainingDayResult = null;

            trainingDayResult = _trainingDayModule.Update(trainingDay);
            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 = _trainingWeeksService.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))
                            _trainingWeeksService.DeleteTrainingExercise(trainingExerciseDb);
                    }
                }

                if (trainingDay.TrainingExercises != null)
                {
                    trainingDayResult.TrainingExercises = new List<TrainingExercise>();
                    foreach (var trainingExercise in trainingDay.TrainingExercises)
                    {
                        trainingDayResult.TrainingExercises.Add(_trainingWeeksService.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(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);
            SynchroManager.TrainingDayChange(DbContext, trainingDayResult);

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

            return trainingDayResult;
        }
        public IActionResult CreateTrainingDay(string userId, int year, int weekOfYear, int dayOfWeek)
        {
            if (string.IsNullOrWhiteSpace(userId) || year == 0 || weekOfYear == 0 || dayOfWeek < 0 || dayOfWeek > 6 || SessionUserId != userId)
                return RedirectToAction("Index");

            var userInfo = _userInfosService.GetUserInfo(new UserInfoKey() { UserId = SessionUserId });
            if (userInfo == null)
                return RedirectToAction("View", new { userId = userId, year = year, weekOfYear = weekOfYear, dayOfWeek = dayOfWeek });

            var trainingDayCriteria = new TrainingDayCriteria()
            {
                UserId = new StringCriteria() { Equal = userId },
                Year = new IntegerCriteria() { Equal = year },
                WeekOfYear = new IntegerCriteria() { Equal = weekOfYear }
            };
            var trainingDayScenario = new TrainingDayScenario()
            {
                ManageExercise = false
            };
            var trainingDayList = _trainingDaysService.FindTrainingDay(trainingDayCriteria, trainingDayScenario);

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

            var trainingDay = new TrainingDay()
            {
                UserId = userId,
                Year = year,
                WeekOfYear = weekOfYear,
                DayOfWeek = dayOfWeek,
                TrainingDayId = trainingDayId
            };

            ViewBag.UserUnit = userInfo.Unit;
            return View(TrainingViewModelTransformer.TrainingDayToViewModel(trainingDay, userInfo));
        }
Beispiel #16
0
        public static TrainingDay TransformViewModelToTrainingDay(TrainingDayViewModel viewModel)
        {
            TrainingDay trainingDay = new TrainingDay();

            trainingDay.UserId = viewModel.UserId;
            trainingDay.Year = viewModel.Year;
            trainingDay.WeekOfYear = viewModel.WeekOfYear;
            trainingDay.DayOfWeek = viewModel.DayOfWeek;
            trainingDay.TrainingDayId = viewModel.TrainingDayId;
            trainingDay.BeginHour = viewModel.BeginHour.ToUniversalTime();
            trainingDay.EndHour = viewModel.EndHour.ToUniversalTime();

            return trainingDay;
        }