Beispiel #1
0
        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));
        }
Beispiel #2
0
 public IEnumerable <Exercise> Get()
 {
     using (FabioDbContext DbContext = new FabioDbContext())
     {
         return(DbContext.Exercises.ToList());
     }
 }
Beispiel #3
0
 //cookie
 public ActionResult Index()
 {
     if (HttpContext.User == null || HttpContext.User.Claims.Count() == 0)
     {
         return(Json(null));
     }
     else
     {
         int  userId = Convert.ToInt32(HttpContext.User.Claims.First(x => x.Type == "Id").Value);
         User user   = null;
         using (FabioDbContext DbContext = new FabioDbContext())
         {
             user = DbContext.Users.FirstOrDefault(x => x.Id == userId);
         }
         return(Json(user));
     }
 }
Beispiel #4
0
        public ActionResult CheckUser([FromBody] LoginViewModel uvm)
        {
            using (FabioDbContext DbContext = new FabioDbContext())
            {
                var user = DbContext.Users.FirstOrDefault(x => x.UserName == uvm.username);
                if (user == null)
                {
                    throw new Exception("Username doesn't exist");
                }

                var passwordBytes = Encoding.ASCII.GetBytes(uvm.password + UserAccountController.salt);

                var sha1 = System.Security.Cryptography.SHA1.Create();
                var hash = sha1.ComputeHash(passwordBytes);

                string hashedPassword = Encoding.ASCII.GetString(hash);

                var passwordsMatch = user.Password == hashedPassword;
                if (!passwordsMatch)
                {
                    throw new Exception("Incorrect password. ");
                }

                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Email, user.Email),
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim("Id", user.Id.ToString())
                };

                var claimsIdentity = new ClaimsIdentity(
                    claims,
                    CookieAuthenticationDefaults.AuthenticationScheme);

                HttpContext.Authentication.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc   = DateTime.UtcNow.AddDays(7)
                });

                return(Json(new { success = true }));
            }
        }
Beispiel #5
0
        public ActionResult DeleteWorkoutExercise(int id)
        {
            bool response = false;
            int  UserId   = Convert.ToInt32(HttpContext.User.Claims.First(x => x.Type == "Id").Value);

            if (UserId != 0)
            {
                using (FabioDbContext DbContext = new FabioDbContext())
                {
                    var exercise = DbContext.WorkoutExercises.FirstOrDefault(x => x.Id == id);
                    DbContext.WorkoutExercises.Remove(exercise);
                    response = true;
                    DbContext.SaveChanges();
                }
            }
            return(Json(response));
        }
Beispiel #6
0
        public ActionResult AddUser([FromBody] UserViewModel uvm)
        {
            using (FabioDbContext DbContext = new FabioDbContext())
            {
                try
                {
                    var exists = DbContext.Users.Any(x => x.UserName == uvm.username);
                    if (exists)
                    {
                        throw new Exception("This username already exists!!! suckaaaa");
                    }
                    var EmailExists = DbContext.Users.Any(x => x.Email == uvm.email);
                    if (EmailExists)
                    {
                        throw new Exception("Email already in use :(");
                    }

                    User user = new User
                    {
                        UserName = uvm.username,
                        Email    = uvm.email
                    };

                    var passwordBytes = Encoding.ASCII.GetBytes(uvm.password + salt);

                    var sha1 = System.Security.Cryptography.SHA1.Create();
                    var hash = sha1.ComputeHash(passwordBytes);

                    user.Password = Encoding.ASCII.GetString(hash);

                    DbContext.Users.Add(user);

                    DbContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    return(Json(new { error = ex.Message }));
                }
                return(Json(new { success = true }));
            }
        }
Beispiel #7
0
        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));
            }
        }