public long CreateEmployee(EmployeeInput input) { return(doInTransaction( () => { var departments = departmentRepository.FindByIds(new List <long> { input.DepartmentId }); if (departments.Count == 0) { throw BusinessException.IllegalDepartmentId(input.DepartmentId); } if (input.SupervisorId != null) { var supervisors = employeeRepository.FindByIds(new List <long> { input.SupervisorId ?? 0 }); if (supervisors.Count == 0) { throw BusinessException.IllegalSupervisorId(input.SupervisorId ?? 0); } } return employeeRepository.Insert(input); } )); }
private void validateSupervisorReferenceCycle( long id, long?supervisorId, IList <Employee> supervisors//Chain ) { if (supervisorId == null) { return; } var supervisor = employeeRepository.FindByIds(new List <long> { supervisorId ?? 0 }).FirstOrDefault(); if (supervisor == null) { throw BusinessException.IllegalSupervisorId(supervisorId ?? 0); } supervisors.Add(supervisor); if (id == supervisorId) { IList <Employee> cycle = new List <Employee>(supervisors); cycle.Insert(0, supervisors[supervisors.Count - 1]); throw BusinessException.SupervisorCycle(id, cycle); } validateSupervisorReferenceCycle(id, supervisor.SupervisorId, supervisors); }