private async void BtnSave_Click(object sender, EventArgs e) { if (this.ValidateChildren()) { WorkoutCreateRequest createRequest = new WorkoutCreateRequest { Description = tbDescription.Text, Difficulty = cbDifficulty.Text, Duration = int.Parse(cbDuration.Text), Name = tbName.Text, TrainerId = int.Parse(cbTrainer.SelectedValue.ToString()), WorkoutTypeId = int.Parse(cbWorkoutType.SelectedValue.ToString()) }; if (_workout != null) { await _workoutApiService.Update <Workout>(_workout.Id, createRequest); } else { await _workoutApiService.Create <Workout>(createRequest); } WorkoutListForm workoutForm = new WorkoutListForm(_accessToken); workoutForm.MdiParent = this.MdiParent; workoutForm.WindowState = FormWindowState.Maximized; workoutForm.ControlBox = false; workoutForm.MaximizeBox = false; workoutForm.MinimizeBox = false; workoutForm.ShowIcon = false; workoutForm.Show(); } }
public async Task <IActionResult> CreateWorkout([FromBody] WorkoutCreateRequest request) { var created = await _workoutService.AddWorkout(request, HttpContext.GetUserId()); if (!created) { return(StatusCode(500)); } return(StatusCode(201)); }
public async Task <bool> AddWorkout(WorkoutCreateRequest workoutCreateRequest, string userId) { var workout = ConstructNewWorkout(workoutCreateRequest); workout.UserId = Guid.Parse(userId); await _context.Workouts.AddAsync(workout); var updated = await _context.SaveChangesAsync(); return(updated > 0); }
private Workout ConstructNewWorkout(WorkoutCreateRequest request) { var workout = new Workout { WorkoutId = Guid.NewGuid(), Created = DateTime.Now, Sets = request.Sets.Select(rs => new Set { SetId = Guid.NewGuid(), Exercise = rs.Exercise, Reps = rs.Reps, Weight = rs.Weight, Volume = rs.Reps * rs.Weight, OneRm = rs.Weight / (1.0278 - 0.0278 * rs.Reps) }).ToList(), Notes = request.Notes }; workout.TotalVolume = workout.Sets.Select(s => s.Volume).Sum(); workout.AverageOneRm = workout.Sets.Select(s => s.OneRm).Sum() / workout.Sets.Count; return(workout); }