Пример #1
0
        public bool AddUser(string firname, string surname, string email, DateTime dateOfBirth, int clientId)
        {
            var clientRepository = new ClientRepository();
            var client           = clientRepository.GetById(clientId);
            var user             = new User
            {
                Client       = client,
                DateOfBirth  = dateOfBirth,
                EmailAddress = email,
                Firstname    = firname,
                Surname      = surname
            };

            if (!user.IsValid)
            {
                return(false);
            }
            UserDataAccess.AddUser(user);
            return(true);
        }
        public bool AddUser(string firname, string surname, string email, DateTime dateOfBirth, int clientId)
        {
            if (string.IsNullOrEmpty(firname) || string.IsNullOrEmpty(surname))
            {
                return(false);
            }

            if (email.Contains("@") && !email.Contains("."))
            {
                return(false);
            }

            if (CalculateAge(dateOfBirth) < 21)
            {
                return(false);
            }

            var user = new User
            {
                Client       = _clientRepository.GetById(clientId),
                DateOfBirth  = dateOfBirth,
                EmailAddress = email,
                Firstname    = firname,
                Surname      = surname
            };

            SetCreditLimit(user);

            if (user.HasCreditLimit && user.CreditLimit < 500)
            {
                return(false);
            }

            UserDataAccess.AddUser(user);

            return(true);
        }
Пример #3
0
        public bool AddUser(string firname, string surname, string email, DateTime dateOfBirth, int clientId)
        {
            if (string.IsNullOrEmpty(firname) || string.IsNullOrEmpty(surname))
            {
                return(false);
            }

            if (!email.Contains("@") && !email.Contains("."))
            {
                return(false);
            }

            var now = DateTime.Now;
            int age = now.Year - dateOfBirth.Year;

            if (now.Month < dateOfBirth.Month || (now.Month == dateOfBirth.Month && now.Day < dateOfBirth.Day))
            {
                age--;
            }

            if (age < 21)
            {
                return(false);
            }

            var clientRepository = new ClientRepository();
            var client           = clientRepository.GetById(clientId);

            var user = new User
            {
                Client       = client,
                DateOfBirth  = dateOfBirth,
                EmailAddress = email,
                Firstname    = firname,
                Surname      = surname
            };

            if (client.Name == "VeryImportantClient")
            {
                // Skip credit check
                user.HasCreditLimit = false;
            }
            else if (client.Name == "ImportantClient")
            {
                // Do credit check and double credit limit
                user.HasCreditLimit = true;
                using (var userCreditService = new UserCreditServiceClient())
                {
                    var creditLimit = userCreditService.GetCreditLimit(user.Firstname, user.Surname, user.DateOfBirth);
                    creditLimit      = creditLimit * 2;
                    user.CreditLimit = creditLimit;
                }
            }
            else
            {
                // Do credit check
                user.HasCreditLimit = true;
                using (var userCreditService = new UserCreditServiceClient())
                {
                    var creditLimit = userCreditService.GetCreditLimit(user.Firstname, user.Surname, user.DateOfBirth);
                    user.CreditLimit = creditLimit;
                }
            }

            if (user.HasCreditLimit && user.CreditLimit < 500)
            {
                return(false);
            }

            UserDataAccess.AddUser(user);

            return(true);
        }
Пример #4
0
        public bool AddUser(string firname, string surname, string email, DateTime dateOfBirth, int clientId)
        {
            if (string.IsNullOrEmpty(firname) || string.IsNullOrEmpty(surname))
            {
                return(false);
            }

            // Moved the email validation logic inside its own class (Single Responsability)
            if (!EmailValidation.IsValid(email))
            {
                return(false);
            }

            // Moved the age calculation logic inside its own class (Single Responsability)
            int age = Age.GetAge(dateOfBirth);

            if (age < 21)
            {
                return(false);
            }

            var clientRepository = new ClientRepository();

            Client client;

            try
            {
                client = clientRepository.GetById(clientId);

                if (client == null)
                {
                    Console.WriteLine(String.Format("UserService.AddUser: clientRepository.GetById({0}) returned NULL", clientId));
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("UserService.AddUser: clientRepository.GetById({0}) thrown exception. Message: {1}", clientId, e.Message));
                return(false);
            }

            var user = new User
            {
                Client       = client,
                DateOfBirth  = dateOfBirth,
                EmailAddress = email,
                Firstname    = firname,
                Surname      = surname
            };

            // Implemented a Strategy Pattern.
            ICreditLimitStrategy creditLimitStrategy = CreditLimitStrategy.Get(client.ClientStatus);

            user.HasCreditLimit = creditLimitStrategy.HasCreditLimit();

            if (user.HasCreditLimit)
            {
                user.CreditLimit = creditLimitStrategy.CreditLimit(user);
            }

            if (IsUserCreditTooLow(user.HasCreditLimit, user.CreditLimit))
            {
                return(false);
            }

            UserDataAccess.AddUser(user);

            return(true);
        }