internal static void UpdateFromModel(this NewsRelease dbPost, Models.Post post, HubDbContext dbContext) { dbContext.Entry(dbPost).CurrentValues.SetValues(post); dbPost.ReleaseType = Enum.Parse <ReleaseType>(post.Kind); var newsReleaseLanguage = dbPost.NewsReleaseLanguage.FirstOrDefault(); if (newsReleaseLanguage == null) { newsReleaseLanguage = new NewsReleaseLanguage() { LanguageId = Language.enCA }; dbPost.NewsReleaseLanguage.Add(newsReleaseLanguage); } newsReleaseLanguage.Location = post.Location; newsReleaseLanguage.Summary = post.Summary; dbPost.Ministry = dbContext.Ministry.FirstOrDefault(m => m.Key == post.LeadMinistryKey); if (post.MinistryKeys != null) { dbContext.NewsReleaseMinistry.RemoveRange(dbPost.NewsReleaseMinistry.Where(m => !post.MinistryKeys.Contains(m.Ministry.Key))); foreach (var newMinistry in post.MinistryKeys.Where(sh => !dbPost.NewsReleaseMinistry.Any(m => m.Ministry.Key == sh))) { dbPost.NewsReleaseMinistry.Add(new NewsReleaseMinistry { Release = dbPost, Ministry = dbContext.Ministry.Single(m => m.Key == newMinistry) }); } } dbPost.Timestamp = DateTimeOffset.Now; }
public static Models.Post ToModel(this NewsRelease dbPost, IMapper mapper) { var model = mapper.Map <Models.Post>(dbPost); var englishPost = dbPost.NewsReleaseLanguage.First(rl => rl.LanguageId == Language.enCA); model.Summary = englishPost.Summary; model.Location = englishPost.Location; var documents = new List <Models.Post.Document>(); foreach (var document in dbPost.NewsReleaseDocument.OrderBy(e => e.SortIndex)) { foreach (var documentLanguage in document.NewsReleaseDocumentLanguage.OrderBy(l => l.LanguageId != Language.enCA)) // English first { var documentModel = mapper.Map <Models.Post.Document>(documentLanguage); var contacts = new List <Models.Post.Document.DocumentContact>(); foreach (var contact in documentLanguage.NewsReleaseDocumentContact) { var postDocumentContact = new Models.Post.Document.DocumentContact(); string[] lines = contact.Information.Replace("\r\n", "\n").Split('\n'); postDocumentContact.Title = lines[0]; postDocumentContact.Details = contact.Information.Substring(lines[0].Length).TrimStart(); contacts.Add(postDocumentContact); } documentModel.Contacts = contacts; documents.Add(documentModel); } } model.LeadMinistryKey = dbPost.Ministry?.Key; model.LeadMinistryName = dbPost.Ministry?.DisplayName; model.Documents = documents; model.MinistryKeys = dbPost.NewsReleaseMinistry.Select(m => m.Release.Key).ToList(); model.Ministries = dbPost.NewsReleaseMinistry.Select(m => m.MinistryId.ToString()).ToList(); return(model); }