// Add a workout to the database and collections.
        public void AddWorkout(Workout newWorkout)
        {
            // Add a workout to the data context.
            workoutDB.Workout.InsertOnSubmit(newWorkout);

            // Save changes to the database.
            workoutDB.SubmitChanges();

            // Add a workout to the "all" observable collection.
            AllWorkouts.Add(newWorkout);
        }
        // Remove a workout from the database and collections.
        public void DeleteWorkout(Workout workoutForDelete)
        {
            // Remove the workout from the "all" observable collection.
            AllWorkouts.Remove(workoutForDelete);

            // Remove the workout from the data context.
            workoutDB.Workout.DeleteOnSubmit(workoutForDelete);


            // Save changes to the database.
            workoutDB.SubmitChanges();
        }
        public ActionResult <AllWorkouts> GetAllWorkouts()
        {
            var userName  = User.Claims.Single(a => a.Type == ClaimTypes.NameIdentifier).Value;
            var exercises = _context.Exercises.Where(i => i.Creator == userName || i.Creator == "").ToList();

            if (exercises == null)
            {
                return(NotFound("Exercises not found"));
            }

            var UserData = _context.UserData.Where(i => i.User == userName).First();

            var result = _context.Workouts
                         .Where(i => i.UserData == UserData)
                         .Include(i => i.Exercises).ThenInclude(i => i.Sets)
                         .ToList();

            if (result == null)
            {
                return(NotFound("Workouts not found"));
            }

            AllWorkouts allworkouts = new AllWorkouts
            {
                Workouts = result.Select(i => new GymBuddyAPI.Models.WorkoutModel()
                {
                    Id        = i.Id,
                    Name      = i.WorkoutName,
                    Exercises = i.Exercises.Select(j => new ExerciseModel()
                    {
                        Id           = j.Id,
                        ExerciseName = j.Name,
                        Type         = j.ExerciseType,
                        Sets         = j.Sets.Select(k => new SetModel()
                        {
                            Id       = k.Id,
                            RepCount = k.RepCount,
                            Weights  = k.Weight
                        }).ToList()
                    }).ToList()
                }).ToList()
            };

            return(Ok(allworkouts));
        }
        public ActionResult <AllWorkouts> DeleteWorkout(long id)
        {
            var userName = User.Claims.Single(a => a.Type == ClaimTypes.NameIdentifier).Value;

            var value = _context.Workouts.First(i => i.UserData.User == userName && i.Id == id);

            if (value == null)
            {
                return(NotFound("Workout not found"));
            }

            var schedule = _context.UserData.Include(i => i.UserSchedule).First(i => i.User == userName).UserSchedule;

            if (schedule.Monday == value)
            {
                schedule.Monday = null;
            }
            if (schedule.Tuesday == value)
            {
                schedule.Tuesday = null;
            }
            if (schedule.Wednesday == value)
            {
                schedule.Wednesday = null;
            }
            if (schedule.Thursday == value)
            {
                schedule.Thursday = null;
            }
            if (schedule.Friday == value)
            {
                schedule.Friday = null;
            }
            if (schedule.Saturday == value)
            {
                schedule.Saturday = null;
            }
            if (schedule.Sunday == value)
            {
                schedule.Sunday = null;
            }

            _context.SaveChanges();

            var delete = _context.Workouts.Remove(value);

            var databaseFunction = _context.Workouts.Include(i => i.UserData).Select(i => i.UserData.User == userName).ToList();

            _context.SaveChanges();

            var result2 = new AllWorkouts()
            {
                Workouts = _context.Workouts
                           .Include(i => i.UserData)
                           .Where(i => i.UserData.User == userName)
                           .Select(i => new WorkoutModel
                {
                    Id   = i.Id,
                    Name = i.WorkoutName,
                }).ToList()
            };

            return(Ok(result2));
        }