private static IDictionary<string, int> PrefillAuthors(IList<boeken> books, AuthorRepository authorsRepo, UnitOfWork unitOfWork) { var allAuthorsInOldDb = books.Select(_ => _.auteurs.Trim()).Distinct().ToList(); var newAuthors = authorsRepo.All.ToList(); var result = new Dictionary<string, int>(); Console.WriteLine("Found {0} authors in old db", allAuthorsInOldDb.Count()); foreach(var a in allAuthorsInOldDb) { var candidate = newAuthors.FirstOrDefault(_ => _.Name.ToLower() == a.ToLower()); if (candidate == null) { candidate = new Author() { State = State.Added, Name = a }; authorsRepo.InsertOrUpdate(candidate); unitOfWork.Save(); newAuthors.Add(candidate); } result[a.ToLower()] = candidate.Id; } return result; }
public JsonResult Create(string name) { var found = _authorRepository.FindAuthorByName(name); int authorId = 0; if (found.Count() == 0) { var author = new Author() { State = State.Added, Name = name }; _authorRepository.InsertOrUpdate(author); _unitOfWork.Save(); authorId = author.Id; } else { authorId = found.First().Id; } return Json(new { Id = authorId, Name = name }, JsonRequestBehavior.AllowGet); }