private void SaveAutoCompleteSuggestions(CaseEditorDetailModel parsed)
        {
            if (string.IsNullOrEmpty(parsed.Jurisdiction?.Code))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(parsed.Court))
            {
                parsed.Court = null;
            }

            if (string.IsNullOrWhiteSpace(parsed.CourtEng))
            {
                parsed.CourtEng = null;
            }

            if (string.IsNullOrWhiteSpace(parsed.Source))
            {
                parsed.Source = null;
            }

            if (parsed.Keywords?.Length > 0)
            {
                var dbKeywords = this.context.Keywords.Select(x => x.Name).ToList();
                var entities   = parsed.Keywords.Except(dbKeywords).Select(x => new Keyword {
                    Name = x
                });
                this.context.Keywords.AddRange(entities); // context.save is in calling method
            }

            this.context.Database.ExecuteSqlCommand("suggest.InsertSuggestions @p0, @p1, @p2, @p3",
                                                    parsed.Jurisdiction.Code, parsed.Court, parsed.CourtEng, parsed.Source);
        }
        private void AutogenerateLegislationEntries(CaseEditorDetailModel content, ApplicationUser user)
        {
            var currentLegislations =
                this.context.Metadata.Where(x => x.IsDeleted != true).Select(x => x.Caption).ToHashSet();

            if (content.NationalLawRecords?.Length > 0)
            {
                var toBeInserted =
                    content.NationalLawRecords.Where(x => x.Include && !currentLegislations.Contains(x.Title));
                foreach (var legi in toBeInserted)
                {
                    if (string.IsNullOrWhiteSpace(legi.Title))
                    {
                        continue;
                    }

                    var contentJson = this.GetContent(content.Jurisdiction, legi);
                    var metaEntity  = new Metadata
                    {
                        Caption    = legi.Title,
                        LastChange = DateTime.Now,
                        User       = user,
                        Content    = contentJson
                    };
                    this.context.Metadata.Add(metaEntity);
                }
            }
        }
 private void SaveAutoCompleteSuggestionsAdapted(string modelContent)
 {
     if (!string.IsNullOrEmpty(modelContent))
     {
         var parsed  = JsonConvert.DeserializeObject <MetaEditorDetailModel>(modelContent);
         var adapted = new CaseEditorDetailModel
         {
             Jurisdiction = parsed.Jurisdiction,
             Source       = parsed.SourceName
         };
         this.SaveAutoCompleteSuggestions(adapted);
     }
 }
        public async Task SaveLegislationEntry(SaveLegislationModel entry)
        {
            var user = await this.usersService.GetCurrentApplicationUser();

            var model = new CaseEditorDetailModel()
            {
                Jurisdiction       = entry.Jurisdiction,
                NationalLawRecords = new[] { entry.LegislationEntry }
            };

            this.AutogenerateLegislationEntries(model, user);
            await this.context.SaveChangesAsync();
        }