public int SaveWorkoutAsync(WorkoutViewModel viewModel, int id = 0) { var workout = new Workout(); if (id != 0) { workout = _context.Workout.Find(id); if (workout == null) { return(0); } if (viewModel.Exercises != null) { workout.Exercises = viewModel.Exercises; } } workout.PersonWorkout = viewModel.PersonWorkout; workout.Name = viewModel.Name; workout.Description = viewModel.Description; workout.TagID = viewModel.TagId; //workout.Exercises = viewModel.Exercises; if (id == 0) { _context.Workout.Add(workout); _context.SaveChanges(); return(_context.Workout.LastAsync().Result.WorkoutID); } _context.Update(workout); _context.SaveChanges(); return(id); }
public async Task <int> SaveExerciseAsync(CreateExerciseViewModel viewModel, int id = 0) { var exercise = new Exercise(); if (id != 0) { exercise = await GetExerciseAsync(id); if (exercise == null) { return(0); } if (viewModel.Muscles != null) { exercise.Muscles = viewModel.Muscles; } } exercise.Name = viewModel.Name; exercise.Description = viewModel.Description; if (viewModel.image != null) { using (var memoryStream = new MemoryStream()) { await viewModel.image.CopyToAsync(memoryStream); exercise.ImageData = memoryStream.ToArray(); exercise.ImageMimeType = viewModel.image.ContentType; } } if (id == 0) { context.Exercise.Add(exercise); context.SaveChanges(); return(GetExercise(exercise.Name).ExerciseID); } context.Update(exercise); context.SaveChanges(); return(id); }