public async Task <bool> ChangeWorkoutOrders(PlannedWorkoutDateUpdate dateUpdate, int userId) { await Extensions.FindUser(userId, _userRepository, _cache); var existing = await _plannedWorkoutRepository.GetAll(dateUpdate.WorkoutOrders.Select(x => x.WorkoutId).ToList(), userId); if (existing.Count != dateUpdate.WorkoutOrders.Count) { throw new RestException(System.Net.HttpStatusCode.BadRequest, "Invalid workoutIds."); } var existingInTimeSpan = await _plannedWorkoutRepository.GetAllMatchingTimeSpan(DateTime.Parse(dateUpdate.ScheduledDate), dateUpdate.TimeOfDay, userId); if (existingInTimeSpan.Any(x => !existing.Contains(x))) { throw new RestException(System.Net.HttpStatusCode.BadRequest, "Does not contain all workouts in this time span."); } var toUpdate = existing.Select(x => { x.UpdateDateValues(dateUpdate); return(x); }).ToList(); return(await _plannedWorkoutRepository.UpdateMany(toUpdate) == toUpdate.Count); }
public void UpdateDateValues(PlannedWorkoutDateUpdate dateUpdate) { TimeOfDay = dateUpdate.TimeOfDay; ScheduledDate = DateTime.Parse(dateUpdate.ScheduledDate); Order = dateUpdate.WorkoutOrders.First(x => x.WorkoutId == Id).Order; }
public async Task <IActionResult> UpdateDates([FromBody] PlannedWorkoutDateUpdate dateUpdate, [FromRoute] int userId) { var result = await _plannedWorkoutService.ChangeWorkoutOrders(dateUpdate, userId); return(Ok(result)); }