public PlannedWorkout(PlannedWorkoutVM viewModel, int userId, int id) : base(viewModel, userId, id) { TimeOfDay = viewModel.TimeOfDay.HasValue ? viewModel.TimeOfDay.Value : TimeOfDay.Any; ScheduledDate = DateTime.Parse(viewModel.ScheduledDate); Order = viewModel.Order; PlannedRepetitions = viewModel.PlannedRepetitions?.Select(x => new PlannedRepetition(x, ActivityType, userId, x.Id, id)).ToList(); }
public async Task <bool> Create(PlannedWorkoutVM workout, int userId) { await Extensions.FindUser(userId, _userRepository, _cache); await SetValidOrder(workout, userId); var model = new PlannedWorkout(workout, userId, 0); var entriesSaved = await _plannedWorkoutRepository.Create(model); return(entriesSaved == (model.PlannedRepetitions.Count + 1)); }
public async Task <bool?> UpdateWorkout(int userId, int workoutId, PlannedWorkoutVM updatedWorkout) { await Extensions.FindUser(userId, _userRepository, _cache); var workout = await _plannedWorkoutRepository.Get(workoutId); if (workout is null || userId != workout.UserId) { return(null); } if (workout.DatesAreEdited(updatedWorkout)) { throw new RestException(System.Net.HttpStatusCode.BadRequest, "Date information cannot be changed in this request."); } workout.UpdateFromVM(updatedWorkout); return((await _plannedWorkoutRepository.Update(workout)) > 0); }
private async Task SetValidOrder(PlannedWorkoutVM newWorkout, int userId) { var timeOfDay = newWorkout.TimeOfDay.HasValue ? newWorkout.TimeOfDay.Value : Database.AdditionalData.TimeOfDay.Any; var existing = await _plannedWorkoutRepository.GetAllMatchingTimeSpan(DateTime.Parse(newWorkout.ScheduledDate), timeOfDay, userId); if (existing is null || existing.Count == 0) { return; } var orders = existing.Select(x => x.Order).ToList(); orders.Add(newWorkout.Order); if (orders.IsDistinctOrder()) { return; } newWorkout.Order = orders.Max() + 1; }
public bool DatesAreEdited(PlannedWorkoutVM update) { return(update.Order != Order || update.ScheduledDate != ScheduledDate.ToString(Constants.DateOnlyFormatString) || update.TimeOfDay != TimeOfDay); }
public void UpdateFromVM(PlannedWorkoutVM viewModel) { base.UpdateFromVM(viewModel); PlannedRepetitions = GetPlannedRepetitionsFromVM(viewModel.PlannedRepetitions); }
public async Task <IActionResult> UpdateWorkout([FromRoute] int userId, [FromRoute] int workoutId, [FromBody] PlannedWorkoutVM updatedWorkout) { var result = await _plannedWorkoutService.UpdateWorkout(userId, workoutId, updatedWorkout); if (result.HasValue) { return(Ok(result)); } return(NotFound()); }
public async Task <IActionResult> CreateWorkout([FromRoute] int userId, [FromBody] PlannedWorkoutVM workout) { var result = await _plannedWorkoutService.Create(workout, userId); return(Ok(result)); }