private void UpdateAuthors(Project project) { if (project.Authors == null) { project.Authors = new List <ProjectOriginalAuthor>(); } var dbAuthors = project.Authors.Select(x => x.OriginalAuthor).ToList(); var newAuthors = m_importedRecord.ImportedProject.Authors.Select(x => new OriginalAuthor { FirstName = x.FirstName, LastName = x.LastName }) .ToList(); var comparer = new AuthorNameEqualityComparer(); var authorsToAdd = newAuthors.Except(dbAuthors, comparer).ToList(); var authorsToRemove = dbAuthors.Except(newAuthors, comparer).ToList(); if (authorsToAdd.Count == 0 && authorsToRemove.Count == 0) { return; } foreach (var author in authorsToRemove) { var projectAuthor = project.Authors.Single(x => x.OriginalAuthor.Id == author.Id); m_projectRepository.Delete(projectAuthor); } for (var i = 0; i < newAuthors.Count; i++) { var newAuthor = newAuthors[i]; if (authorsToAdd.Contains(newAuthor, comparer)) { var dbAuthor = GetOrCreateAuthor(newAuthor.FirstName, newAuthor.LastName); var newProjectAuthor = new ProjectOriginalAuthor { OriginalAuthor = dbAuthor, Project = m_projectRepository.Load <Project>(m_projectId), Sequence = i + 1 }; m_projectRepository.Create(newProjectAuthor); } else { var projectAuthor = project.Authors.Single(x => x.OriginalAuthor.FirstName == newAuthor.FirstName && x.OriginalAuthor.LastName == newAuthor.LastName); projectAuthor.Sequence = i + 1; m_projectRepository.Update(projectAuthor); } } }
public void UpdateAuthors(long projectId, BookData bookData) { var dbProjectAuthors = m_projectRepository.GetProjectOriginalAuthorList(projectId, true); var dbAuthors = dbProjectAuthors.Select(x => x.OriginalAuthor).ToList(); var newAuthors = bookData.Authors?.Select(x => PersonHelper.ConvertToOriginalAuthor(x.Name)).ToList() ?? new List <OriginalAuthor>(); var comparer = new AuthorNameEqualityComparer(); var authorsToAdd = newAuthors.Except(dbAuthors, comparer).ToList(); var authorsToRemove = dbAuthors.Except(newAuthors, comparer).ToList(); if (authorsToAdd.Count == 0 && authorsToRemove.Count == 0) { return; } foreach (var author in authorsToRemove) { var projectAuthor = dbProjectAuthors.Single(x => x.OriginalAuthor.Id == author.Id); m_projectRepository.Delete(projectAuthor); } for (int i = 0; i < newAuthors.Count; i++) { var newAuthor = newAuthors[i]; if (authorsToAdd.Contains(newAuthor, comparer)) { var dbAuthor = GetOrCreateAuthor(newAuthor.FirstName, newAuthor.LastName); var newProjectAuthor = new ProjectOriginalAuthor { OriginalAuthor = dbAuthor, Project = m_projectRepository.Load <Project>(projectId), Sequence = i + 1 }; m_projectRepository.Create(newProjectAuthor); } else { var projectAuthor = dbProjectAuthors.Single(x => x.OriginalAuthor.FirstName == newAuthor.FirstName && x.OriginalAuthor.LastName == newAuthor.LastName); projectAuthor.Sequence = i + 1; m_projectRepository.Update(projectAuthor); } } }