/// <summary> /// Removes customer departments /// </summary> /// <param name="existingDepartments">customer departments</param> /// <param name="customerId">customer id</param> private void RemoveCustomerDepartments(List <int> existingDepartments, int customerId) { var handler = new RemoveDepartmentHandler(_unitOfWork); if (customerId == 0) { return; } else { var departmentsForDelete = _unitOfWork.DepartmentRepository.Get() .Where(d => d.CustomerId == customerId && !existingDepartments.Contains(d.Id)).ToList(); foreach (var d in departmentsForDelete) { handler.Execute(d.Id); } } }
/// <summary> /// Removes customer /// </summary> /// <param name="id">customer id</param> public void Execute(int id) { var removeDepartamentHandler = new RemoveDepartmentHandler(_unitOfWork); var customerDeps = _unitOfWork.DepartmentRepository.Get().Where(e => e.CustomerId == id).ToList(); //delete departments of the customer with inner users deleting foreach (var dep in customerDeps) { removeDepartamentHandler.Execute(dep.Id); } //remove contacts of the customer var contactsToRemove = _unitOfWork.ContactRepository.Get().Where(c => c.CustomerId == id).ToList(); foreach (var item in contactsToRemove) { _unitOfWork.ContactRepository.Delete(item); } //delete customer _unitOfWork.CustomerRepository.Delete(id); _unitOfWork.Save(); }