public ActionResult Index(int day) { WorkoutModel response = new WorkoutModel(); int userId = Convert.ToInt32(HttpContext.User.Claims.First(x => x.Type == "Id").Value); Workout workout = new Workout(); using (FabioDbContext DbContext = new FabioDbContext()) { workout = DbContext.Workouts.FirstOrDefault(x => x.UserId == userId && x.Day == day); if (workout == null) { return(Json(response)); } response.day = workout.Day; response.id = workout.Id; response.exercises = DbContext.WorkoutExercises.Where(x => workout.Id == x.WorkoutID) .Select(x => new WorkoutExerciseModel { exerciseid = x.ExerciseID, repetitions = x.Repetitions, sets = x.Sets, workoutid = x.WorkoutID, exercise = DbContext.Exercises.First(z => z.Id == x.ExerciseID).Name, id = x.Id }) .ToList(); } return(Json(response)); }
public ActionResult AddWorkout([FromBody] WorkoutModel uvm) { WorkoutModel response = new WorkoutModel(); Workout workout = new Workout { Day = uvm.day, // UserId = uvm.userId, UserId = Convert.ToInt32(HttpContext.User.Claims.First(x => x.Type == "Id").Value), Id = uvm.id }; using (FabioDbContext DbContext = new FabioDbContext()) { if (uvm.id == 0) { DbContext.Workouts.Add(workout); DbContext.SaveChanges(); foreach (var wem in uvm.exercises) { var workoutExercise = new WorkoutExercise { WorkoutID = workout.Id, ExerciseID = wem.exerciseid, Repetitions = wem.repetitions, Sets = wem.sets, Id = wem.id }; DbContext.WorkoutExercises.Add(workoutExercise); } DbContext.SaveChanges(); } else { foreach (var wem in uvm.exercises) { if (wem.id > 0) { var workoutExercise = new WorkoutExercise { WorkoutID = workout.Id, ExerciseID = wem.exerciseid, Repetitions = wem.repetitions, Sets = wem.sets, Id = wem.id }; DbContext.WorkoutExercises.Update(workoutExercise); } else { var workoutExercise = new WorkoutExercise { WorkoutID = workout.Id, ExerciseID = wem.exerciseid, Repetitions = wem.repetitions, Sets = wem.sets, Id = wem.id }; DbContext.WorkoutExercises.Add(workoutExercise); } DbContext.SaveChanges(); } } response.day = workout.Day; response.id = workout.Id; response.exercises = DbContext.WorkoutExercises.Where(x => workout.Id == x.WorkoutID) .Select(x => new WorkoutExerciseModel { exerciseid = x.ExerciseID, repetitions = x.Repetitions, sets = x.Sets, workoutid = x.WorkoutID, exercise = DbContext.Exercises.First(z => z.Id == x.ExerciseID).Name, id = x.Id }) .ToList(); return(Json(response)); } }