public void TestPreferredName_Null()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.PreferredName = null;
            result = validator.Validate(instance);
            Assert.IsTrue(result.IsValid);
        }
        public void TestSuffix_CheckFourthSuffixIsValid()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.Suffix = FullNameValidator.FOURTH_SUFFIX;
            result          = validator.Validate(instance);
            Assert.IsTrue(result.IsValid);
            Assert.AreEqual(0, result.Errors.Count);
        }
        public void TestSuffix_Null()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.Suffix = null;
            result          = validator.Validate(instance);
            Assert.IsTrue(result.IsValid);
            Assert.AreEqual(0, result.Errors.Count);
        }
        public void TestLastName_Null()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.LastName = null;
            result            = validator.Validate(instance);
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(
                string.Format(FullNameValidator.LAST_NAME_ERROR_MESSAGE, personType, FullNameValidator.LAST_NAME_MAX_LENGTH),
                result.Errors.First().ErrorMessage);
            Assert.IsInstanceOfType(result.Errors.First().CustomState, typeof(FullNameErrorPath));
        }
        public void TestPreferredName_ContainsDigitis()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.PreferredName = new string('1', FullNameValidator.PREFERRED_NAME_MAX_LENGTH);
            result = validator.Validate(instance);
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(
                string.Format(FullNameValidator.PREFFERED_NAME_ERROR_MESSAGE, personType, FullNameValidator.PREFERRED_NAME_MAX_LENGTH),
                result.Errors.First().ErrorMessage);
            Assert.IsInstanceOfType(result.Errors.First().CustomState, typeof(FullNameErrorPath));
        }
        public void TestPassportName_ExceedsMaxLength()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.PassportName = new string('c', FullNameValidator.PASSPORT_NAME_MAX_LENGTH + 1);
            result = validator.Validate(instance);
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(
                string.Format(FullNameValidator.PASSPORT_NAME_ERROR_MESSAGE, personType, FullNameValidator.PASSPORT_NAME_MAX_LENGTH),
                result.Errors.First().ErrorMessage);
            Assert.IsInstanceOfType(result.Errors.First().CustomState, typeof(FullNameErrorPath));
        }
        public void TestSuffix_UnknownSuffix()
        {
            var validator = new FullNameValidator(personType);
            var instance  = GetValidFullName();
            var result    = validator.Validate(instance);

            Assert.IsTrue(result.IsValid);

            instance.Suffix = "x";
            result          = validator.Validate(instance);
            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);

            Assert.AreEqual(
                string.Format(
                    FullNameValidator.SUFFIX_VALUE_ERROR_MESSAGE,
                    personType,
                    instance.Suffix,
                    string.Join(", ", FullNameValidator.JUNIOR_SUFFIX, FullNameValidator.SENIOR_SUFFIX, FullNameValidator.FIRST_SUFFIX, FullNameValidator.SECOND_SUFFIX, FullNameValidator.THIRD_SUFFIX, FullNameValidator.FOURTH_SUFFIX)),
                result.Errors.First().ErrorMessage);

            Assert.IsInstanceOfType(result.Errors.First().CustomState, typeof(FullNameErrorPath));
        }
Example #8
0
        public async Task <IActionResult> SaveCustomer([FromBody] CreateCustomerRequest request)
        {
            var errorInfo = new ErrorInfo();

            // Validate Fullname
            errorInfo = FullNameValidator.Validate(request.FullName, out string fullName);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Validate Date of Birth
            errorInfo = DateTimeValidator.Validate(request.DateOfBirth, out DateTime? validDate);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Check if user already exists
            var user = await _customerRepository.GetCustomerByFullNameAsync(fullName, validDate);

            if (user?.Id != null)
            {
                errorInfo.ErrorCode    = ErrorTypes.InvalidFullName;
                errorInfo.ErrorMessage = $"This name '{fullName}' is already exists.";
                throw new BadInputException(errorInfo);
            }

            // Validate ICollection Address
            foreach (var item in request.Address)
            {
                errorInfo = AddressValidator.Validate(item);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }
            }

            // Map request into model
            var customerModel = _mapper.Map <CustomerModel>(request);

            customerModel.DateOfBirth = validDate;
            customerModel.FullName    = fullName;

            await _customerRepository.CreateCustomerAsync(customerModel);

            return(Ok(customerModel));
        }
Example #9
0
        public async Task <IActionResult> UpdateCustomer(int id, UpdateCustomerRequest request)
        {
            var errorInfo = new ErrorInfo();

            // Verify customer identifier from route and request body
            errorInfo = IdentifierValidator.Validate(id, request.CustomerIdentifier);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Find customer to update
            var currentCustomer = await _customerRepository.GetCustomerByIdentifierAsync(id);

            // Customer
            errorInfo = CustomerObjectValidator.Validate(currentCustomer, id);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            bool isModified = false;

            // Fullname
            if (request.FullName != null && currentCustomer.FullName != request.FullName)
            {
                errorInfo = FullNameValidator.Validate(request.FullName, out string fullName);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }

                isModified = true;
                currentCustomer.FullName = fullName;
            }

            // Date of Birth
            if (request.DateOfBirth != null && currentCustomer.DateOfBirth.ToString() != request.DateOfBirth)
            {
                errorInfo = DateTimeValidator.Validate(request.DateOfBirth, out DateTime? validDate);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }

                isModified = true;
                currentCustomer.DateOfBirth = validDate;
                currentCustomer.Age         = CalculateAge.Calculate(request.DateOfBirth);
            }

            // Validate ICollection Address
            if (request?.Address != null)
            {
                foreach (var item in request.Address)
                {
                    errorInfo = AddressValidator.Validate(item);
                    if (errorInfo.ErrorCode != ErrorTypes.OK)
                    {
                        throw new BadInputException(errorInfo);
                    }
                }

                isModified = true;
                currentCustomer.Address = _mapper.Map <List <AddressModel> >(request.Address);
            }

            if (isModified)
            {
                // TODO: To implement the updated and created date in Model
                // newCustomer.UpdatedDate = DateTime.UtcNow;
                await _customerRepository.UpdateCustomerAsync(currentCustomer);
            }

            // Map Journal Model to Journal Dto
            var resultDto = _mapper.Map <CustomerDto>(currentCustomer);

            return(Ok(resultDto));
        }