Ejemplo n.º 1
0
        public ViewResult Register(UserRegistration userRegistration)
        {
            var errors = userRegistration.Validate();

            if (errors == null)
            {
                IList <UserFlavor> userFlavors = Session["UserFlavorSelected"] as List <UserFlavor>;
                IList <EventType>  eventTypes  = Session["EventTypeSelected"] as List <EventType>;
                IList <Garment>    mygarments  = Session["MyGarments"] as List <Garment>;
                IList <Garment>    mywishlist  = Session["MyWishList"] as List <Garment>;

                PublicUser user = new PublicUser();
                user.EmailAddress = userRegistration.Email;
                user.ChangeZipCode(userRegistration.ZipCode);
                user.SetFlavors(userFlavors);
                user.Size = new UserSize(Convert.ToInt32(userRegistration.UserSize));

                //TODO: Get the UserId from ASP.NET Membership
                MembershipCreateStatus status;
                MembershipUser         mu = Membership.CreateUser(userRegistration.UserName, userRegistration.Password, userRegistration.Email, securityQuestionRepository.Get(Convert.ToInt32(userRegistration.SecurityQuestion)).Description, userRegistration.SecurityAnswer, true, out status);
                if (status != MembershipCreateStatus.Success)
                {
                    errors = new ErrorSummary();
                    errors.RegisterErrorMessage("MembershipUser", status.ToString());
                    return(RegistrationError(userRegistration, errors.ErrorMessages));
                }
                user.MembershipUserId = Convert.ToInt32(mu.ProviderUserKey);
                user.FirstName        = string.Empty;
                user.LastName         = string.Empty;
                user.PhoneNumber      = string.Empty;

                if (eventTypes != null)
                {
                    foreach (EventType eventType in eventTypes)
                    {
                        user.AddEventType(eventType);
                    }
                }

                registeredUserRepository.SaveOrUpdate(user);
                Closet closet = new Closet();
                closet.User         = user;
                closet.PrivacyLevel = PrivacyLevel.Private;

                closetRepository.SaveOrUpdate(closet);
                if (mygarments != null)
                {
                    foreach (Garment garment in mygarments)
                    {
                        closet.AddGarment(garment);
                    }
                    closetRepository.SaveOrUpdate(closet);
                }
                user.Closet = closet;

                registeredUserRepository.SaveOrUpdate(user);

                if (mywishlist != null && mywishlist.Count > 0)
                {
                    WishList wl = new WishList();
                    wl.User = user;
                    foreach (Garment wishlist in mywishlist)
                    {
                        wl.AddGarment(wishlist);
                    }
                    wishListRepository.SaveOrUpdate(wl);
                }
                closetRepository.GenerateCloset(user);

                Session.Abandon();
                Session["UserRegistration"] = mu;
                return(View("RegistrationFinish", userRegistration));
            }

            return(RegistrationError(userRegistration, errors.ErrorMessages));
        }