public IActionResult Create(TrainingWeekViewModel viewModel) { if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(viewModel.UserId) || viewModel.Year == 0 || viewModel.Year == 0 || SessionUserId != viewModel.UserId) return View(viewModel); //Verify valide week of year if (viewModel.WeekOfYear > 0 && viewModel.WeekOfYear <= 52 && (viewModel.Unit == (int)TUnitType.Imperial || viewModel.Unit == (int)TUnitType.Metric)) { var trainingWeek = TransformViewModelToTrainingWeek(viewModel); var trainingWeekKey = new TrainingWeekKey() { UserId = trainingWeek.UserId, Year = trainingWeek.Year, WeekOfYear = trainingWeek.WeekOfYear }; var trainingWeekScenario = new TrainingWeekScenario() { ManageTrainingDay = false }; var existTrainingWeek = _trainingWeeksService.GetTrainingWeek(trainingWeekKey, trainingWeekScenario); if (existTrainingWeek != null) { ModelState.AddModelError(string.Empty, string.Format(Translation.P0_ALREADY_EXIST, Translation.TRAINING_WEEK)); return View(viewModel); } trainingWeek = _trainingWeeksService.CreateTrainingWeek(trainingWeek); if (trainingWeek == null) { ModelState.AddModelError(string.Empty, Translation.IMPOSSIBLE_TO_CREATE_NEW_TRAINING_JOURNAL); return View(viewModel); } return RedirectToAction("View", new { userId = trainingWeek.UserId, year = trainingWeek.Year, weekOfYear = trainingWeek.WeekOfYear }); } } return View(viewModel); }
public IActionResult Edit(TrainingWeekViewModel viewModel) { if (ModelState.IsValid) { ViewBag.UserUnit = GetUserUnit(viewModel.UserId); if (string.IsNullOrWhiteSpace(viewModel.UserId) || viewModel.Year == 0 || viewModel.WeekOfYear == 0 || SessionUserId != viewModel.UserId) return View(viewModel); //Verify valide week of year if (viewModel.WeekOfYear > 0 && viewModel.WeekOfYear <= 52 && (viewModel.Unit == (int)TUnitType.Imperial || viewModel.Unit == (int)TUnitType.Metric)) { var trainingWeek = TransformViewModelToTrainingWeek(viewModel); var trainingWeekKey = new TrainingWeekKey() { UserId = trainingWeek.UserId, Year = trainingWeek.Year, WeekOfYear = trainingWeek.WeekOfYear }; var trainingWeekScenario = new TrainingWeekScenario() { ManageTrainingDay = false }; var existTrainingWeek = _trainingWeeksService.GetTrainingWeek(trainingWeekKey, trainingWeekScenario); if (existTrainingWeek == null) { ModelState.AddModelError(string.Empty, string.Format(Translation.P0_NOT_EXIST, Translation.TRAINING_WEEK)); return View(viewModel); } //Create data in database. No need transaction, only header trainingWeekScenario = new TrainingWeekScenario() { ManageTrainingDay = false }; trainingWeek = _trainingWeeksService.UpdateTrainingWeek(trainingWeek, trainingWeekScenario); if (trainingWeek == null) { ModelState.AddModelError(string.Empty, string.Format(Translation.IMPOSSIBLE_TO_UPDATE_P0, Translation.TRAINING_JOURNAL)); return View(viewModel); } return RedirectToAction("View", new { userId = trainingWeek.UserId, year = trainingWeek.Year, weekOfYear = trainingWeek.WeekOfYear }); } } return View(viewModel); }
private TrainingWeek TransformViewModelToTrainingWeek(TrainingWeekViewModel viewModel) { TrainingWeek trainingJournal = new TrainingWeek(); trainingJournal.UserId = viewModel.UserId; trainingJournal.Year = viewModel.Year; trainingJournal.WeekOfYear = viewModel.WeekOfYear; trainingJournal.UserHeight = viewModel.UserHeight; trainingJournal.UserWeight = viewModel.UserWeight; trainingJournal.Unit = Utils.IntToEnum<TUnitType>(viewModel.Unit); return trainingJournal; }