Example #1
0
        /// <summary>
        /// Contains business logic to create an individual user.
        /// <para>
        /// @author: Brian Fann
        /// @updated: 04/25/2018
        /// </para>
        /// </summary>
        /// <param name="registerUserDto"></param>
        /// <returns>ResponseDto</returns>
        public ResponseDto <RegisterUserDto> CreateFirstTimeIndividualUser(RegisterUserDto registerUserDto)
        {
            // Validate incoming dto
            var preLogicValidation = new CreateFirstTimeIndividualPreLogicValidationStrategy(registerUserDto);
            var result             = preLogicValidation.ExecuteStrategy();

            if (result.Error != null)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = result.Error
                });
            }

            // Authenticate user credentials against the database
            var credentialsValidator = new CredentialsValidator();
            var credentialsResult    = credentialsValidator.IsCredentialsValid(registerUserDto.UserAccountDto.Username, registerUserDto.UserAccountDto.Password);

            if (!credentialsResult.Data)
            {
                return(new ResponseDto <RegisterUserDto>()
                {
                    Data = registerUserDto,
                    Error = credentialsResult.Error
                });
            }

            // Map dto to domain models
            var mappingResult = MapIndividualDtoToModel(registerUserDto, out var userAccount, out var passwordSalt, out var userClaims, out var userProfile, out var securityQuestions, out var securityAnswerSalts);

            // Map Id from database to the domain model
            var userIdResult = GetFirstTimeUserAccountId(userAccount.Username);

            if (userIdResult.Error != null)
            {
                return(new ResponseDto <RegisterUserDto>()
                {
                    Data = registerUserDto,
                    Error = userIdResult.Error
                });
            }

            userAccount.Id = userIdResult.Data;

            // Validate user domain model after mapping from dto
            var postLogicValidation = new CreateFirstTimeIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
            var validateResult      = postLogicValidation.ExecuteStrategy();

            if (!validateResult.Data)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var gatewayResult = userGateway.StoreIndividualUser(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
                if (gatewayResult.Data == false)
                {
                    return(new ResponseDto <RegisterUserDto>()
                    {
                        Data = registerUserDto,
                        Error = GeneralErrorMessages.GENERAL_ERROR
                    });
                }
            }

            return(new ResponseDto <RegisterUserDto>
            {
                Data = registerUserDto
            });
        }
Example #2
0
        /// <summary>
        /// Creates a restaurant user as part of first time registration
        /// </summary>
        /// <para>
        /// @author: Brian Fann
        /// @updated: 04/25/2018
        /// </para>
        /// <param name="registerRestaurantDto">Incoming Dto</param>
        /// <returns></returns>
        public ResponseDto <RegisterRestaurantDto> CreateFirstTimeRestaurantUser(RegisterRestaurantDto registerRestaurantDto)
        {
            // Validate incoming user account in the dto
            var userPreLogicValidationStrategy = new CreateFirstTimeIndividualPreLogicValidationStrategy(registerRestaurantDto);
            var userResult = userPreLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            // Validate incoming restaurant details in the dto
            var restaurantPreLogicValidationStrategy = new CreateRestaurantPreLogicValidationStrategy(registerRestaurantDto);
            var restaurantResult = restaurantPreLogicValidationStrategy.ExecuteStrategy();

            if (restaurantResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = restaurantResult.Error
                });
            }

            // Authenticate user credentials against the database
            var credentialsValidator = new CredentialsValidator();
            var credentialsResult    = credentialsValidator.IsCredentialsValid(registerRestaurantDto.UserAccountDto.Username, registerRestaurantDto.UserAccountDto.Password);

            if (!credentialsResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = credentialsResult.Error
                });
            }

            // Create a domain model based on the dto.
            var mappingResult = MapRestaurantDtoToModels(registerRestaurantDto, out var userAccount, out var passwordSalt, out var userClaims, out var userProfile, out var securityQuestions, out var securityAnswerSalts, out var restaurantProfile, out var businessHours, out var foodPreferences);

            if (!mappingResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = mappingResult.Error
                });
            }

            // Validate domain models created from dto
            var userPostLogicValidationStrategy = new CreateFirstTimeIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);

            userResult = userPostLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            // Map the user's id in the database to the generated domain model.
            var userIdResult = GetFirstTimeUserAccountId(userAccount.Username);

            if (userIdResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = userIdResult.Error
                });
            }

            userAccount.Id = userIdResult.Data;

            // Apply post logic validation to the user account information
            var userPostLogicValdiationStrategy = new CreateIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
            var userPostResult = userPostLogicValdiationStrategy.ExecuteStrategy();

            if (!userPostResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Apply post logic validation to the restaurant information
            var restaurantPostLogicValdiationStrategy = new CreateRestaurantPostLogicValidationStrategy(restaurantProfile, businessHours);
            var restaurantPostResult = restaurantPostLogicValdiationStrategy.ExecuteStrategy();

            if (!restaurantPostResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var createResult = userGateway.StoreRestaurantUser(userAccount, passwordSalt, userClaims, userProfile, restaurantProfile, securityQuestions, securityAnswerSalts, foodPreferences, businessHours);

                if (!createResult.Data)
                {
                    return(new ResponseDto <RegisterRestaurantDto>()
                    {
                        Data = registerRestaurantDto,
                        Error = createResult.Error
                    });
                }
            }

            return(new ResponseDto <RegisterRestaurantDto>
            {
                Data = registerRestaurantDto
            });
        }