コード例 #1
0
        public async Task can_create_employee()
        {
            var longTimeAgo = new DateTime(1956, 2, 1);
            var newGuy      = Employee.CreateNew(id: "1", dateOfBirth: longTimeAgo);

            await _employeesRepository.AddNew(newGuy);

            await _employeesRepository.CommitChanges(CancellationToken.None);

            var employee = await _employeesRepository.GetById("1");

            employee.ShouldNotBeNull();
            employee.DateOfBirth.ShouldBe(longTimeAgo);
        }
コード例 #2
0
        private async Task <(Employee NewEmployee, DomainError DomainError)> ImportEmployee(ImportEmployee command,
                                                                                            CancellationToken cancellationToken)
        {
            //We can predetermine our employee ID based on FistName + LastName + Age + Height.
            var employeeId = EmployeeIdGenerator.Create(Encoding.UTF8.GetBytes(command.ToUniqueIdentifier)).ToString();

            var employee = await _employeesRepository.GetById(employeeId, cancellationToken);

            if (employee != null)
            {
                return(employee, DomainError.None);
            }


            if (command.Age < 18) //lets assume minimum working age is 18
            {
                //hey we are doing something doggy here
                return(null, new DomainError("Child labor long time ago abolished. There must be a mistake somewhere"));
            }
            //we wont check for now the max life limit

            //This gets interesting since we don't have the actual date of birth. but our company needs it to send a birthday postcard every year
            employee =
                Employee.CreateNew(employeeId, _getUtcNow())
                //should be good approximation. Anyway we will mark that the real birthday is not supplied and later on in UI someone will notice
                .WithEstimatedBirthDate(_getUtcNow().AddYears(-command.Age))
                .WithFirstName(command.FirstName)
                .WithLastName(command.LastName)
                .WithHeightInMeters(command.HeightInMeters);

            await _employeesRepository.AddNew(employee);

            await _employeesRepository.CommitChanges(cancellationToken);

            _logger.LogDebug("Employee {displayName} imported", $"{command.FirstName} {command.LastName}");
            return(employee, DomainError.None);
        }