Exemplo n.º 1
0
        public async Task <ActionResult> Register(RegistrationTypes model, bool captchaValid)
        {
            bool isAdmin, isCoach, isAthlete;

            isAdmin = isCoach = isAthlete = false;
            string tempEmail, tempPassword, tempFName, tempLName, tempSex, tempGender;

            tempEmail = tempPassword = tempFName = tempLName = tempSex = tempGender = null;
            DateTime tempDOB = new DateTime(DateTime.MinValue.Ticks);
            int      tempHeight, tempWeight;

            tempHeight = tempWeight = 0;

            if (ModelState.IsValid)
            {
                if (model.adminVM != null)
                {
                    isAdmin      = true;
                    tempEmail    = model.adminVM.Email;
                    tempPassword = model.adminVM.Password;
                }
                if (model.coachVM != null)
                {
                    isCoach      = true;
                    tempEmail    = model.coachVM.Email;
                    tempPassword = model.coachVM.Password;
                    tempFName    = model.coachVM.FirstName;
                    tempLName    = model.coachVM.LastName;
                }
                if (model.athleteVM != null)
                {
                    isAthlete    = true;
                    tempEmail    = model.athleteVM.Email;
                    tempPassword = model.athleteVM.Password;
                    tempFName    = model.athleteVM.FirstName;
                    tempLName    = model.athleteVM.LastName;
                    tempDOB      = model.athleteVM.DOB;
                    string heightFeet   = Request.Form["feet"].ToString();
                    string heightInches = Request.Form["inches"].ToString();
                    if (heightFeet != "")
                    {
                        tempHeight = (Convert.ToInt32(heightFeet) * 12) + Convert.ToInt32(heightInches);
                    }
                    string weight = Request.Form["weight"].ToString();
                    if (weight != "")
                    {
                        tempWeight = Convert.ToInt32(weight);
                    }
                    string sex = Request.Form["sex"].ToString();
                    if (sex != "")
                    {
                        tempSex = sex;
                    }
                    string gender = Request.Form["gender"].ToString();
                    if (gender != "")
                    {
                        tempGender = gender;
                    }
                }

                //if(isCoach || isAthlete) {
                //    //confirm account via email
                //}

                var user = new ApplicationUser {
                    UserName = tempEmail, Email = tempEmail
                };
                var result = await UserManager.CreateAsync(user, tempPassword);

                if (result.Succeeded)
                {
                    string subject     = "Please confirm your Peak Performance email";
                    string callbackUrl = await SendConfirmationTokenAsync(user.Id, subject, tempFName);

                    var tempUser = new Person
                    {
                        FirstName        = tempFName,
                        LastName         = tempLName,
                        ASPNetIdentityID = user.Id,
                        Active           = true
                    };

                    PeakPerformanceContext db = new PeakPerformanceContext();

                    if (model.adminVM != null)
                    {
                        UserManager.AddToRole(user.Id, "Admin");
                    }
                    if (model.coachVM != null)
                    {
                        var newCoach = new Coach
                        {
                        };

                        newCoach.Person = tempUser;
                        db.Persons.Add(tempUser);
                        db.Coaches.Add(newCoach);
                        UserManager.AddToRole(user.Id, "Coach");
                    }
                    if (model.athleteVM != null)
                    {
                        var newAthlete = new Athlete
                        {
                            DOB    = tempDOB,
                            Height = tempHeight,
                            Weight = tempWeight,
                            Sex    = tempSex,
                            Gender = tempGender
                        };

                        newAthlete.Person = tempUser;
                        db.Persons.Add(tempUser);
                        db.Athletes.Add(newAthlete);
                        UserManager.AddToRole(user.Id, "Athlete");
                    }

                    await db.SaveChangesAsync();

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    if (isAdmin)
                    {
                        return(RedirectToAction("Index", "Home", new { area = "Admin" }));
                    }
                    else if (isCoach)
                    {
                        return(RedirectToAction("Index", "Home", new { area = "Coach" }));
                    }
                    else if (isAthlete)
                    {
                        return(RedirectToAction("Index", "Home", new { area = "Athlete" }));
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form

            return(View(model));
        }