public IActionResult QueryUpdate(List <QueryPassageFormData> update, string url, int page, string?keys, string?isselected)
        {
            var identity  = (ClaimsIdentity)HttpContext.User.Identity;
            var judgename = identity.Claims.FirstOrDefault(c => c.Type == "name")?.Value;
            var email     = identity.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value;
            CategoryMatchField urlfields = new CategoryMatchField(UrlJoinHelper.geturlmatchfields(url.ToLower()));
            string             key       = urlfields.category + ":" + urlfields.matchfield;
            //key = "sac:ht204974";
            //var headers = Request.Headers;
            //string UEUrl = "xyz";
            //QEResponseData qeResponseData = new QEResponseData();
            string check = Request.Form["check"];
            //ViewBag.UEUrl = url;
            //HashSet<string> Urls = new HashSet<string> { url };
            //ViewBag.page = page;
            //var Passages = AzureSearchHelper.getPassagesfromUrls(Urls);
            // List<String> PassageIds = new List<string>();
            //foreach (var Passage in Passages)
            //{
            //Passage.RootQueries = Passage.RootQueries.Split(";").First();
            //}
            //ViewBag.Passages = Passages;
            //ViewBag.Url = url;
            //ViewBag.PassageTitle = Passages.FirstOrDefault().HtmlHeadTitle;
            //ViewBag.PassageCount = Passages.Count();
            List <RootQueryCandidate> updatedata = new List <RootQueryCandidate>();

            if (update.Count != 0)
            {
                updatedata.AddRange(_context.getQueryCandidatesByUrl(key));

                foreach (var item in update)
                {
                    item.selectedanswerid = item.selectedanswerid ?? "none";
                    var i = updatedata.Where(a => (a.queryid == item.queryid) &&
                                             (a.selectedanswerid != item.selectedanswerid)).FirstOrDefault();
                    if (i != null)
                    {
                        i.selectedanswerid = item.selectedanswerid;
                        i.lastmodifiedby   = email;
                        i.lastmodifiedon   = DateTime.Now;
                    }
                }

                _context.SaveChanges();
            }

            //ViewBag.PassageIds = PassageIds;

            return(RedirectToAction("UrlExpansion", new { url = url, page = page, judgename = judgename, keys = keys, isselected = isselected }));
        }
        public static List <PassageData> getPassagesfromUrls(HashSet <string> urls)
        {
            List <PassageData> allPD = new List <PassageData>();


            ISearchServiceClient serviceClient = new SearchServiceClient("passageranker", new SearchCredentials("61438B42BF9E15FB1FBAAA6963C3FCB8"));
            ISearchIndexClient   indexClient   = serviceClient.Indexes.GetClient("techpassagesdata");

            HashSet <string> aids = new HashSet <string>();

            foreach (var item in urls)
            {
                CategoryMatchField urlfields = new CategoryMatchField(UrlJoinHelper.geturlmatchfields(item.ToLower()));
                string             key       = urlfields.category + ":" + urlfields.matchfield;
                string             filter    = "urlcategory eq '" + urlfields.category + "' and urlmatchfield eq '" + urlfields.matchfield + "'";
                SearchParameters   parameters;
                DocumentSearchResult <PassageData> results;
                parameters =
                    new SearchParameters()
                {
                    Filter = filter
                };

                string test = "";
                results = indexClient.Documents.Search <PassageData>(test, parameters);
                foreach (var item2 in results.Results)
                {
                    PassageData pd = new PassageData();
                    try
                    {
                        pd.Url              = item2.Document.Url;
                        pd.FinalTitle       = item2.Document.FinalTitle;
                        pd.FinalTitleQASKey = item2.Document.FinalTitleQASKey;
                        pd.HtmlHeadTitle    = item2.Document.HtmlHeadTitle;
                        pd.PassageId        = item2.Document.PassageId;
                        pd.RawAnswer        = item2.Document.RawAnswer;
                        pd.RootQueries      = item2.Document.RootQueries;
                        pd.RubatoAnswer     = item2.Document.RubatoAnswer;
                        pd.Urlcategory      = item2.Document.Urlcategory;
                        pd.Urlmatchfield    = item2.Document.Urlmatchfield;

                        allPD.Add(pd);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            return(allPD);
        }
        public IActionResult AddQuery(string queries, string selectedanswerid, string url, int page, string?keys)
        {
            var identity  = (ClaimsIdentity)HttpContext.User.Identity;
            var judgename = identity.Claims.FirstOrDefault(c => c.Type == "name")?.Value;
            var email     = identity.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value;

            CategoryMatchField urlfields = new CategoryMatchField(UrlJoinHelper.geturlmatchfields(url.ToLower()));
            string             key       = urlfields.category + ":" + urlfields.matchfield;

            string[] allqueries = queries.Split("||");

            //key = "sac:ht204974";
            //var headers = Request.Headers;
            //string UEUrl = "xyz";
            //QEResponseData qeResponseData = new QEResponseData();
            //string check = Request.Form["check"];
            //ViewBag.UEUrl = url;
            //HashSet<string> Urls = new HashSet<string> { url };
            //ViewBag.page = page;
            //var Passages = AzureSearchHelper.getPassagesfromUrls(Urls);
            // List<String> PassageIds = new List<string>();
            //foreach (var Passage in Passages)
            //{
            //Passage.RootQueries = Passage.RootQueries.Split(";").First();
            //}
            //ViewBag.Passages = Passages;
            //ViewBag.Url = url;
            //ViewBag.PassageTitle = Passages.FirstOrDefault().HtmlHeadTitle;
            //ViewBag.PassageCount = Passages.Count();
            List <RootQueryCandidate> updatedata = new List <RootQueryCandidate>();

            if (allqueries.Count() != 0)
            {
                updatedata.AddRange(_context.getQueryCandidatesByUrl(key));

                foreach (var query in allqueries)
                {
                    var contains         = _context.getQueryCandidatesByUrl(key).Where(x => x.pwquery == query);
                    RootQueryCandidate i = new RootQueryCandidate();
                    if (contains.Count() == 0)
                    {
                        i.queryid          = Guid.NewGuid().ToString(); //todo check for guid presence in db
                        i.pwquery          = query;
                        i.url              = key;
                        i.selectedanswerid = selectedanswerid;
                        i.lastmodifiedby   = email;
                        i.lastmodifiedon   = DateTime.Now;
                        i.impression       = -1;
                        _context.AddAsync(i);
                        _context.SaveChangesAsync();
                    }

                    else
                    {
                        var temp = updatedata.Where(a => (a.pwquery == query) &&
                                                    (a.selectedanswerid != selectedanswerid)).FirstOrDefault();
                        if (temp != null)
                        {
                            temp.selectedanswerid = selectedanswerid;
                            temp.lastmodifiedby   = email;
                            temp.lastmodifiedon   = DateTime.Now;
                        }
                        _context.SaveChangesAsync();
                    }

                    _context.SaveChangesAsync();
                }
            }

            //ViewBag.PassageIds = PassageIds;

            return(RedirectToAction("UrlExpansion", new { url = url, page = page, judgename = judgename, keys = keys }));
        }
        public IActionResult UrlExpansion(string url, int?page, string?keys, string?isselected)
        {
            var identity  = (ClaimsIdentity)HttpContext.User.Identity;
            var judgename = identity.Claims.FirstOrDefault(c => c.Type == "name")?.Value;
            var email     = identity.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value;
            CategoryMatchField        urlfields      = new CategoryMatchField(UrlJoinHelper.geturlmatchfields(url.ToLower()));
            string                    key            = urlfields.category + ":" + urlfields.matchfield;
            List <RootQueryCandidate> qeResponseData = new List <RootQueryCandidate>();
            string                    check          = Request.Form["check"];

            ViewBag.url = url;
            HashSet <string> Urls = new HashSet <string> {
                url
            };

            var Passages = AzureSearchHelper.getPassagesfromUrls(Urls);

            //List<String> PassageIds = new List<string>();

            ViewBag.Url          = Passages.FirstOrDefault().Url;
            ViewBag.PassageTitle = Passages.FirstOrDefault().HtmlHeadTitle;
            ViewBag.PassageCount = Passages.Count();
            ViewBag.Judgename    = judgename;
            ViewBag.Keys         = keys;

            ViewBag.IsSelected = isselected;
            if (Passages.Count != 0)
            {
                if (isselected == "selected")
                {
                    qeResponseData = _context.getQueryCandidatesByUrl(key).Where(m => m.selectedanswerid != "none").ToList();
                }
                else if (isselected == "none")
                {
                    qeResponseData = _context.getQueryCandidatesByUrl(key).Where(m => m.selectedanswerid == "none").ToList();
                }
                else
                {
                    qeResponseData = _context.getQueryCandidatesByUrl(key);
                }
            }



            if (keys != null && qeResponseData.Count > 0)
            {
                string[] terms = keys.Split("||");
                foreach (var term in terms)
                {
                    qeResponseData = qeResponseData.Where(q => q.pwquery.Contains(term)).ToList();
                }
            }

            foreach (var Passage in Passages)
            {
                Passage.selectedcount = qeResponseData.Where(x => x.selectedanswerid == Passage.PassageId).Count();
                Passage.RootQueries   = string.Join(";", Passage.RootQueries.Split(";").Take(5));
            }
            ViewBag.Passages = Passages;


            //ViewBag.PassageIds = PassageIds;
            var        pageNumber       = page ?? 1; // if no page is specified, default to the first page (1)
            int        pageSize         = 500;       // Get 25 students for each requested page.
            IPagedList onePageOfQueries = qeResponseData.ToPagedList(pageNumber, pageSize);

            ViewBag.page = pageNumber;
            return(View(onePageOfQueries));
        }