public void GetIdSeq_IdSeqFileExist_GetCorrectIdSeq()
        {
            var initIdSeq = Constants.InitIdSeq + 100;

            var jObject = new JObject {
                ["IdSeq"] = initIdSeq
            };
            var fullFileName = Path.Combine(_idSeqFilePath, _idSeqFileName);

            using (StreamWriter file = File.CreateText(fullFileName))
            {
                using (JsonTextWriter writer = new JsonTextWriter(file))
                {
                    writer.Formatting = Formatting.Indented;
                    jObject.WriteTo(writer);
                }
            }

            Assert.AreEqual(initIdSeq + 1, IdSeqGenerator.GetIdSeq());
        }
 public void GetIdSeq_IdSeqFileNotExist_GetCorrectIdSeq()
 {
     Assert.AreEqual(Constants.InitIdSeq + 1, IdSeqGenerator.GetIdSeq());
 }
        public bool Insert(Customer customer, out int id)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersService::Insert] Starting insert customer. Customer: {}", customer);
            }

            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException(
                              nameof(customer), "[CustomersService::Insert] Customer can not be null.");
                }

                if (string.IsNullOrEmpty(customer.UserName))
                {
                    throw new ArgumentNullException(
                              nameof(customer.UserName), "[CustomersService::Insert] UserName can not be null or empty.");
                }

                if (string.IsNullOrEmpty(customer.FirstName))
                {
                    throw new ArgumentNullException(
                              nameof(customer.FirstName), "[CustomersService::Insert] FirstName can not be null or empty.");
                }

                if (string.IsNullOrEmpty(customer.LastName))
                {
                    throw new ArgumentNullException(
                              nameof(customer.LastName), "[CustomersService::Insert] LastName can not be null or empty.");
                }

                if (string.IsNullOrEmpty(customer.PhoneNumber))
                {
                    throw new ArgumentNullException(
                              nameof(customer.PhoneNumber), "[CustomersService::Insert] PhoneNumber can not be null or empty.");
                }

                if (customer.DateOfBirth.AddYears(Constants.AgeLimit) < DateTime.Today)
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(customer.DateOfBirth), $"[CustomersService::Insert] Age can not larger than {Constants.AgeLimit}.");
                }

                // Generate a unique Id for the new customer
                int tempId = customer.Id;
                customer.Id = IdSeqGenerator.GetIdSeq();
                var isInserted = _customersRepository.Insert(ToCustomerEntity(customer));

                // Can't insert when there is a same userName
                if (!isInserted)
                {
                    _logger.Warn("[CustomersService::Insert] Can't insert when there is a same userName.");
                    id          = Constants.NotExistCustomerId;
                    customer.Id = tempId;
                    return(false);
                }

                // After creating customer successfully, create Call Note file for the user
                _callNotesService.CreateCallNotesFile(customer.UserName);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("[CustomersService::Insert] Insert customer Successfully. Inserted Customer: {}", customer);
                }

                id = customer.Id;
                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersService::Insert] Insert customer failed. Customer: {0}", customer);
                throw new BusinessLogicException("[CustomersService::Insert] Insert customer failed.", e);
            }
        }