示例#1
0
        public async Task <IHttpActionResult> AddCoachToAthleteAsync(CoachAthleteIds coachAthleteIds)
        {
            if (coachAthleteIds == null)
            {
                return(InternalServerError());
            }

            var athleteProfile = await _entitiesDb.AthleteProfiles.FirstOrDefaultAsync(p => p.IdentityId == coachAthleteIds.AthleteId);

            if (athleteProfile == null)
            {
                return(InternalServerError());
            }

            var coachProfile = await _entitiesDb.CoachProfiles.FirstOrDefaultAsync(p => p.IdentityId == coachAthleteIds.CoachId);

            if (coachProfile == null)
            {
                return(InternalServerError());
            }

            var coachAthlete = new CoachAthlete {
                AthlethProfileId = athleteProfile.Id,
                CoachProfileId   = coachProfile.Id
            };

            var alreadyInDb = await _entitiesDb.CoachAthlete.FirstOrDefaultAsync(u => u.AthlethProfileId == coachAthlete.AthlethProfileId && u.CoachProfileId == coachAthlete.CoachProfileId);

            if (alreadyInDb != null)
            {
                return(BadRequest("Already have this coach."));
            }

            _entitiesDb.CoachAthlete.Add(coachAthlete);
            await _entitiesDb.SaveChangesAsync();


            return(Ok());
        }
示例#2
0
        public static async Task Initialize(ApplicationDbContext context,
                                            UserManager <ApplicationUser> userManager,
                                            RoleManager <ApplicationRole> roleManager)
        {
            //context.Database.EnsureCreated();


            string password = "******";

            //foreach (var role in roles) {
            //    if (await roleManager.FindByNameAsync(role.Name) == null)
            //    {
            //        await roleManager.CreateAsync(role);
            //    }
            //}

            if (await roleManager.FindByNameAsync("Admin") == null)
            {
                ApplicationRole role = new ApplicationRole();
                role.Name = "Admin";
                await roleManager.CreateAsync(role);
            }
            if (await roleManager.FindByNameAsync("Coach") == null)
            {
                ApplicationRole role = new ApplicationRole();
                role.Name = "Coach";
                await roleManager.CreateAsync(role);
            }
            if (await roleManager.FindByNameAsync("Athlete") == null)
            {
                ApplicationRole role = new ApplicationRole();
                role.Name = "Athlete";
                await roleManager.CreateAsync(role);
            }


            foreach (var user in useres)
            {
                if (await userManager.FindByNameAsync(user.UserName) == null)
                {
                    var result = await userManager.CreateAsync(user);

                    if (result.Succeeded)
                    {
                        await userManager.AddPasswordAsync(user, password);

                        if (user.UserName == "mitchel")
                        {
                            await userManager.AddToRoleAsync(user, "Coach");
                        }
                        else
                        {
                            await userManager.AddToRoleAsync(user, "Athlete");
                        }
                    }
                }
            }

            #region TestType

            foreach (var type in testTypes)
            {
                if (context.TestType.FirstOrDefault(t => t.Name == type.Name) == null)
                {
                    context.TestType.Add(type);
                    context.SaveChanges();
                    if (type.ID > 0)
                    {
                        if (type.Name == "Coopertest")
                        {
                            foreach (var rating in cooperTestfitnessRatings)
                            {
                                rating.TestTypeID = type.ID;
                                context.FitnessRating.Add(rating);
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            foreach (var rating in sprintTestfitnessRatings)
                            {
                                rating.TestTypeID = type.ID;
                                context.FitnessRating.Add(rating);
                                context.SaveChanges();
                            }
                        }
                    }
                }
            }
            #endregion

            #region CoachAthlate
            var userList = await userManager.GetUsersInRoleAsync("Coach");

            var coachUser   = userList.FirstOrDefault();
            var athleteList = await userManager.GetUsersInRoleAsync("Athlete");

            var coachAtheles = context.CoachAthlete.Where(c => c.CoachID == coachUser.Id && !c.IsRemoved).ToList();
            if (coachAtheles == null || coachAtheles.Count <= 0)
            {
                foreach (var athlete in athleteList)
                {
                    var coachAthlete = new CoachAthlete();
                    coachAthlete.CoachID   = coachUser.Id;
                    coachAthlete.AthleteID = athlete.Id;
                    context.CoachAthlete.Add(coachAthlete);
                    context.SaveChanges();
                }
            }
            #endregion
        }