예제 #1
0
        public async Task <RegisterResponse> PerformRegister([FromBody] RegisterRequest register)
        {
            RegisterResponse response = new RegisterResponse();

            int?profileId = null, userId = null;

            //check for email conflicts in partnercode
            UserLookup user = new UserLookup();

            user.email       = register.user.Email;
            user.partnercode = register.user.Partnercode;
            User userModel = (await userRepository.Lookup(user)).FirstOrDefault();

            //if uniquenick set, check for uniquenick conflictss in namespaceid
            if (register.profile?.Uniquenick != null && userModel != null)
            {
                var checkData = await profileRepository.CheckUniqueNickInUse(register.profile.Uniquenick, register.profile.Namespaceid, register.user.Partnercode);

                if (checkData.Item1)
                {
                    if (userModel.Password.CompareTo(register.password) == 0)
                    {
                        profileId = checkData.Item2;
                        userId    = checkData.Item3;
                        throw new UniqueNickInUseException(profileId, userId);
                    }
                    else
                    {
                        throw new UserExistsException(userModel); //user exist... need to throw profileid due to GP
                    }
                }
            }
            else if (register.profile?.Uniquenick != null)
            {
                var checkData = await profileRepository.CheckUniqueNickInUse(register.profile.Uniquenick, register.profile.Namespaceid, register.user.Partnercode);

                if (checkData.Item1)
                {
                    throw new UniqueNickInUseException(null);
                }
            }
            if (userModel != null)
            {
                if ((userId.HasValue && userId.Value != userModel.Id) || userModel.Password.CompareTo(register.password) != 0)
                {
                    throw new UserExistsException(userModel); //user exist... need to throw profileid due to GP
                }
                response.user = userModel;
            }
            else
            {
                //if OK, create user, and profile
                userModel             = new User();
                userModel.Email       = register.user.Email;
                userModel.Partnercode = register.user.Partnercode;
                userModel.Password    = register.password;

                response.user = (await userRepository.Create(userModel));
            }



            if (register.profile != null)
            {
                Profile profileModel = new Profile();
                profileModel = register.profile;

                profileModel.Userid = response.user.Id;
                profileModel        = await profileRepository.Create(profileModel);

                response.profile = profileModel;
            }

            //send registration email

            return(response);
        }