Exemple #1
0
        private async Task <Unit> UpdateEmployee(UpdateEmployeeCommand command, CancellationToken cancellationToken)
        {
            var employee = await _context.Employees.FindAsync(command.Id);

            if (employee == null)
            {
                throw new EntityNotFoundException($"An employee having id '{command.Id}' could not be found");
            }

            employee.Address    = command.Address;
            employee.BirthDate  = command.BirthDate;
            employee.City       = command.City;
            employee.Country    = command.Country;
            employee.Email      = command.Email;
            employee.Fax        = command.Fax;
            employee.FirstName  = command.FirstName;
            employee.HireDate   = command.HireDate;
            employee.LastName   = command.LastName;
            employee.Phone      = command.Phone;
            employee.PostalCode = command.PostalCode;
            employee.State      = command.State;
            employee.Title      = command.Title;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #2
0
        private async Task <Unit> DeleteEmployee(int employeeId)
        {
            var employee = await _context.Employees.FindAsync(employeeId);

            if (employee == null)
            {
                throw new EntityNotFoundException($"An employee having id '{employeeId}' could not be found");
            }

            _context.Employees.Remove(employee);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemple #3
0
        private async Task <EmployeeFromCreate> CreateEmployee(CreateEmployeeCommand command, CancellationToken cancellationToken)
        {
            var employee = new Employee
            {
                BirthDate = command.BirthDate,
                FirstName = command.FirstName,
                HireDate  = command.HireDate,
                LastName  = command.LastName,
                Title     = command.Title
            };

            _context.Employees.Add(employee);
            await _context.SaveChangesAsync(cancellationToken);

            employee.Email = await _emailAssignmentService.AssignEmailAsync(employee.Id);

            employee.Fax = await _faxNumberAssignmentService.AssignFaxNumber(employee.Id);

            employee.Phone = await _phoneNumberAssignmentService.AssignPhoneNumber(employee.Id);

            await _context.SaveChangesAsync(cancellationToken);

            return(new EmployeeFromCreate(employee));
        }
        private async Task <Unit> UpdateEmployeeAddress(UpdateEmployeeAddressCommand address, CancellationToken cancellationToken)
        {
            var employee = await _context.Employees.FindAsync(address.EmployeeId);

            if (employee == null)
            {
                throw new EntityNotFoundException($"An employee having id '{address.EmployeeId}' could not be found");
            }

            employee.Address    = address.Address;
            employee.City       = address.City;
            employee.Country    = address.Country;
            employee.PostalCode = address.PostalCode;
            employee.State      = address.State;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }