internal TrainingWeek UpdateTrainingWeek(TrainingWeek trainingWeek, TrainingWeekScenario trainingWeekScenario) { TrainingWeek trainingWeekResult = null; trainingWeekResult = _trainingWeekModule.Update(trainingWeek); if (trainingWeekScenario != null && trainingWeekScenario.ManageTrainingDay) { var trainingDayScenario = new TrainingDayScenario() { ManageExercise = true }; var trainingDayCriteria = new TrainingDayCriteria() { UserId = new StringCriteria() { Equal = trainingWeek.UserId }, Year = new IntegerCriteria() { Equal = trainingWeek.Year }, WeekOfYear = new IntegerCriteria() { Equal = trainingWeek.WeekOfYear } }; var trainingDaysDb = _trainingDaysService.FindTrainingDay(AppUtils.GetUserUnit(_userInfosService, trainingWeek.UserId), trainingDayCriteria, trainingDayScenario); if (trainingDaysDb != null && trainingDaysDb.Count > 0) { foreach (var trainingDayDb in trainingDaysDb) { _trainingDaysService.DeleteTrainingDay(trainingDayDb); } } if (trainingWeek.TrainingDays != null) { trainingWeekResult.TrainingDays = new List <TrainingDay>(); foreach (var trainingDay in trainingWeek.TrainingDays) { trainingWeekResult.TrainingDays.Add(_trainingDaysService.UpdateTrainingDay(trainingDay, trainingWeekScenario.TrainingDayScenario)); } } } return(trainingWeekResult); }
public IActionResult Update([FromBody] TrainingDayWithScenario trainingDayWithScenario) { try { if (trainingDayWithScenario == null || trainingDayWithScenario.TrainingDay == null || trainingDayWithScenario.TrainingDayScenario == null) { return(BadRequest()); } var trainingDay = trainingDayWithScenario.TrainingDay; var trainingDayScenario = trainingDayWithScenario.TrainingDayScenario; if (string.IsNullOrWhiteSpace(trainingDay.UserId) || trainingDay.Year == 0 || trainingDay.WeekOfYear == 0 || trainingDay.DayOfWeek < 0 || trainingDay.DayOfWeek > 6 || SessionUserId != trainingDay.UserId) { return(BadRequest()); } //Verify valid week of year if (trainingDay.WeekOfYear > 0 && trainingDay.WeekOfYear <= 52) { trainingDay = _trainingDaysService.UpdateTrainingDay(trainingDay, trainingDayScenario); if (trainingDay == null) { return(BadRequest(new WebApiException(string.Format(Translation.IMPOSSIBLE_TO_UPDATE_P0, Translation.TRAINING_DAY)))); } else { return(new OkObjectResult(trainingDay)); } } else { return(BadRequest()); } } catch (Exception exception) { return(BadRequest(new WebApiException("Error", exception))); } }