private void SeedExerciseTypes(ApplicationDbContext context)
        {
            var userStore           = new UserStore <ApplicationUser>(context);
            var userManager         = new ApplicationUserManager(userStore);
            var exerciseTypeService = new ExerciseTypeService(context);

            // adding ExerciseTypes
            var exerciseTypes    = new List <ExerciseType>(InitialData.ExerciseTypes);
            var applicationUsers = new List <ApplicationUser>(userManager.Users);

            foreach (var u in applicationUsers)
            {
                foreach (var e in exerciseTypes)
                {
                    e.ApplicationUser = u;
                    exerciseTypeService.Create(e);
                }
            }
        }
Beispiel #2
0
        public async Task <JsonResult> Create(CreateExerciseTypeViewModel viewModel)
        {
            var user = await userManager.FindByIdAsync(User.Identity.GetUserId());

            if (user == null)
            {
                return(Json(HttpStatusCode.Unauthorized.ToString()));
            }
            if (ModelState.IsValid)
            {
                var exercise = new ExerciseType
                {
                    Name = viewModel.Name
                };
                var oldExercise = exerciseTypeService.Find(e =>
                                                           e.ApplicationUser.Id == user.Id &&
                                                           e.Name == exercise.Name).ToList();
                if (oldExercise.Count == 0)
                {
                    exercise.ApplicationUser = user;
                    exerciseTypeService.Create(exercise);
                    return(Json("Created successfully"));
                }
                else
                {
                    return(Json("An exercise with such name already exists"));
                }
            }
            string errors = "";

            foreach (var modelState in ModelState.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    errors += error.ErrorMessage + ". ";
                }
            }
            // error
            return(Json(errors));
        }