Example #1
0
        public async Task <JsonResult> MarkAsFavoriteConfirmed(int id)
        {
            var user = await userManager.FindByIdAsync(User.Identity.GetUserId());

            if (user == null)
            {
                return(Json(HttpStatusCode.Unauthorized.ToString()));
            }
            var exercise = exerciseTypeService.Get(id);

            if (exercise == null)
            {
                return(Json("Not marked as favorite"));
            }
            if (exercise.ApplicationUser.Id != user.Id)
            {
                return(Json("Not marked as favorite"));
            }
            exerciseTypeService.MarkAsFavorite(id);
            return(Json("Marked as favorite successfully"));
        }
        public async Task <ActionResult> Create(WorkoutViewModel model)
        {
            var user = await userManager.FindByIdAsync(User.Identity.GetUserId());

            if (user == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }
            var exerciseTypeOptions = exerciseTypeService.GetAll(user.Id).ToList();

            if (exerciseTypeOptions.Count == 0)
            {
                return(Redirect("/Exercises/All"));
            }
            ViewBag.ExerciseTypeOptions = exerciseTypeOptions;
            if (ModelState.IsValid)
            {
                var workout = MapWorkoutViewModel(model);

                var oldWorkout = workoutService.Find(w =>
                                                     w.ApplicationUser.Id == user.Id &&
                                                     w.WorkoutDate == workout.WorkoutDate).ToList();
                if (oldWorkout.Count == 0)
                {
                    foreach (var exercise in workout.Exercises)
                    {
                        exercise.ExerciseType = exerciseTypeService.Get(exercise.ExerciseType.Id);
                    }
                    workout.ApplicationUser = user;
                    workoutService.Create(workout);
                    return(Redirect("/Workouts/All"));
                }
                else
                {
                    ViewBag.Status = "A workout with such date already exists";
                    return(View(model));
                }
            }
            return(View(model));
        }