コード例 #1
0
        public async Task <ActionResult> Create(EmployeeDto employeeDto)
        {
            if (string.IsNullOrEmpty(employeeDto.FirstName) ||
                string.IsNullOrEmpty(employeeDto.LastName) ||
                string.IsNullOrEmpty(employeeDto.Salary) ||
                string.IsNullOrEmpty(employeeDto.PositionId) ||
                !Guid.TryParse(employeeDto.PositionId, out var positionId) ||
                employeeDto.HireDate == null)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity));
            }
            var position = await positionRepository.GetAsync(positionId);

            if (position == null)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, $"Position with id {employeeDto.PositionId} was not found"));
            }

            var employee = new Employee(employeeDto.FirstName, employeeDto.LastName, employeeDto.Salary, employeeDto.HireDate.Value);

            employee.ChangePosition(position, employeeDto.HireDate.Value);
            if (employeeDto.DismissalDate.HasValue)
            {
                employee.DismissalEmployee(employeeDto.DismissalDate.Value);
            }
            await employeeRepository.InsertAsync(employee);

            return(CreatedAtAction(
                       nameof(Get),
                       new { id = employee.Id.ToString() },
                       EmployeeSummary.FromDomain(employee)));
        }
コード例 #2
0
        public async Task <IEnumerable <EmployeeSummary> > Get()
        {
            var employees = await employeeRepository.GetAsync();

            return(employees.Select(domain => EmployeeSummary.FromDomain(domain)));
        }