Beispiel #1
0
        public void UnitTest4(SignUpForm input)
        {
            startup = new Startup("PorwalGeneralStore_RegisterUser_DB", false);
            _porwalGeneralStoreContext = startup._inMemoryContext;
            _userLayer = new UserLayer(_porwalGeneralStoreContext);
            var ActualResult   = _userLayer.RegisterUser(input);
            var ExpectedResult = _porwalGeneralStoreContext
                                 .CustomerInfo
                                 .Any(x => x.Phone.Equals(input.MobileNumber, StringComparison.OrdinalIgnoreCase));

            Assert.True(ActualResult);
            Assert.True(ExpectedResult);
        }
        public SignUpFormResponse RegistorUser(SignUpForm signUpForm)
        {
            SignUpFormResponse signUpFormResponse = new SignUpFormResponse()
            {
                StatusCode = 200
            };

            try
            {
                if (signUpForm == null)
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm),
                            Message   = nameof(signUpForm) + " can't be null"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.FirstName))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.FirstName),
                            Message   = nameof(signUpForm.FirstName) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.LastName))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.LastName),
                            Message   = nameof(signUpForm.LastName) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.UserName))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.UserName),
                            Message   = nameof(signUpForm.UserName) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.Password))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.Password),
                            Message   = nameof(signUpForm.Password) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.City))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.City),
                            Message   = nameof(signUpForm.City) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (string.IsNullOrWhiteSpace(signUpForm.MobileNumber))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.MobileNumber),
                            Message   = nameof(signUpForm.MobileNumber) + " can't be blank"
                        }
                    };
                    return(signUpFormResponse);
                }

                if (!Regex.IsMatch(signUpForm.MobileNumber, RegexPattern.mobile_number_validation_Patterns.GetCombinedPattern()))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.MobileNumber),
                            Message   = nameof(signUpForm.MobileNumber) + " should be valid. Format -: xxxxxxxxxx "
                        }
                    };
                    return(signUpFormResponse);
                }

                if (!Regex.IsMatch(signUpForm.Password, RegexPattern.password_validation_pattern))
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.Password),
                            Message   = nameof(signUpForm.Password) + " should be valid. Format -: atleast one uppercase,one lowercase, one special character and one digit "
                        }
                    };
                    return(signUpFormResponse);
                }

                bool isMobileNumberExist = _userLayer.isExistPhoneNumber(signUpForm.MobileNumber);
                if (isMobileNumberExist)
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(signUpForm.MobileNumber),
                            Message   = nameof(signUpForm.MobileNumber) + " already exist , please use different number."
                        }
                    };
                    return(signUpFormResponse);
                }

                bool isRegisteredSuccessfully = _userLayer.RegisterUser(signUpForm);
                if (isRegisteredSuccessfully)
                {
                    signUpFormResponse.StatusCode = 200;
                    signUpFormResponse.ErrorList  = null;
                    signUpFormResponse.Message    = "User is Successfully Registered.";
                }
                else
                {
                    signUpFormResponse.StatusCode = 400;
                    signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                    {
                        new SignUpValidationResponse()
                        {
                            Code    = 1001,
                            Message = "Error While creating account on server , please try after some time."
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                signUpFormResponse.StatusCode = 400;
                signUpFormResponse.ErrorList  = new List <SignUpValidationResponse>()
                {
                    new SignUpValidationResponse()
                    {
                        Code    = 1001,
                        Message = "Error While creating account on server , please try after some time." + ex.Message
                    }
                };
            }

            return(signUpFormResponse);
        }