void AddSearch(ISearch target) { target.Id = Guid.NewGuid(); target.CreatedOnUtc = System.DateTime.UtcNow; var search = new Search(); search.Id = target.Id; search.CreatedOnUtc = target.CreatedOnUtc; search.SearchName = target.SearchName; target.SearchTerms.ForEach(x => { x.Id = Guid.NewGuid(); x.CreatedOnUtc = System.DateTime.UtcNow; search.SearchTerms.Add(new SearchTerm { CreatedOnUtc = x.CreatedOnUtc, Id = x.Id, Score = x.Score, Term = x.Term }); }); using (var ctx = new SearcherEntities()) { ctx.Searches.Add(search); ctx.SaveChanges(); } }
void PersistSearchResults(List <SearchResult> results, string postType) { var searchResultDatas = new List <SearchResultData>(); results.ToList().ForEach(x => { var searchResultData = new SearchResultData { Id = Guid.NewGuid(), CreatedOnUtc = System.DateTime.UtcNow, PostDateTime = x.PostDate, PostUrl = x.Uri.ToString(), PostId = x.Key, PostTitle = x.Title, PostType = postType, KeywordScore = x.KeywordScore }; searchResultDatas.Add(searchResultData); }); using (var ctx = new SearcherEntities()) { ctx.SearchResultDatas.AddRange(searchResultDatas); ctx.SaveChanges(); } }
public ActionResult SaveChanges(FormCollection form) { using (var context = new SearcherEntities()) { form.AllKeys.Where(x => x.Contains("PostId_")).ToList().ForEach(x => { var id = Guid.Parse(x.Replace("PostId_", "")); context.SearchResultDatas.Single(z => z.Id == id).AppliedDateTime = System.DateTime.Now; }); form.AllKeys.Where(x => x.Contains("Hidden_")).ToList().ForEach(x => { var id = Guid.Parse(x.Replace("Hidden_", "")); context.SearchResultDatas.Single(z => z.Id == id).Hidden = true; }); context.SaveChanges(); } return(RedirectToAction("Index")); }
void UpdateSearch(ISearch target) { using (var ctx = new SearcherEntities()) { var original = ctx.Searches.Single(x => x.Id == target.Id); var targetTerms = target.SearchTerms.Select(x => x.Term.ToLower()); var sourceTerms = original.SearchTerms.Select(x => x.Term.ToLower()); //handle deletes original.SearchTerms.Where(x => !targetTerms.Contains(x.Term.ToLower())) .ToList() .ForEach(x => { ctx.SearchTerms.Remove(x); }); //handle updates to existing original.SearchTerms.ToList().ForEach(x => { var updated = target.SearchTerms.Single(n => n.Term.ToLower() == x.Term.ToLower()); if (updated.Score != x.Score) { x.Score = updated.Score; x.UpdatedOnUtc = System.DateTime.UtcNow; } }); //handle inserts target.SearchTerms.Where(x => !sourceTerms.Contains(x.Term.ToLower())).ToList().ForEach(x => { var newTerm = new SearchTerm { Id = Guid.NewGuid(), CreatedOnUtc = System.DateTime.UtcNow, Score = x.Score, Term = x.Term }; original.SearchTerms.Add(newTerm); }); ctx.SaveChanges(); } }