private void RemoveExistingProjectReference(SupervisorUserProject existingProjectReference) { if (existingProjectReference != null) { _dataContext.SupervisorUserProjects.Remove(existingProjectReference); } }
private async Task UpdateSupervisorProjectReferences(SupervisorUser user, SupervisorUserProject currentProjectReference, int?selectedProjectId) { var projectHasNotChanged = selectedProjectId.HasValue && selectedProjectId.Value == currentProjectReference?.ProjectId; if (projectHasNotChanged) { return; } if (selectedProjectId.HasValue) { var supervisorHasNotDeletedDataCollectors = await _dataContext.DataCollectors.Where(dc => dc.Supervisor == user) .AnyAsync(dc => dc.Project == dc.Supervisor.CurrentProject && !dc.DeletedAt.HasValue); if (supervisorHasNotDeletedDataCollectors) { throw new ResultException(ResultKey.User.Supervisor.CannotChangeProjectSupervisorHasDataCollectors); } var project = await _dataContext.Projects .Where(p => p.State == ProjectState.Open) .Where(p => user.UserNationalSocieties.Select(uns => uns.NationalSocietyId).Contains(p.NationalSociety.Id)) .SingleOrDefaultAsync(p => p.Id == selectedProjectId.Value); if (project == null) { throw new ResultException(ResultKey.User.Supervisor.ProjectDoesNotExistOrNoAccess); } await AttachSupervisorToProject(user, project); } RemoveExistingProjectReference(currentProjectReference); }