Esempio n. 1
0
        public static List <Article> GetArticlesByEntities(List <string> catLs, List <DateTime> dateLs)
        {
            Random ran = new Random();
            var    articlesCollection = Db.GetCollection <Article>("articles");
            var    articles           = new List <Article>();
            // FilterDefinition<Article> filter;
            var ls = MessageController.Articles;

            if (catLs.Count > 0 && MessageController.Model.TryGetValue(catLs.First(), out var vec))
            {
                ls = MessageController.Articles.OrderBy(
                    x => MessageController.DistanceBetweenVecs(x.Vector, vec)).Take(10).ToList();
            }
            else
            {
                ls = MessageController.Articles.OrderBy(
                    x => MessageController.DistanceBetweenVecs(x.Vector, MessageController.User.Vector)).Take(10).ToList();
            }


            // if (catLs.Count == 0)
            // {
            // if (dateLs.Count == 0)
            // {
            //     filter = Builders<Article>.Filter.Empty;
            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // else if (dateLs.Count == 1)
            // {
            //     filter = Builders<Article>.Filter.Regex("publishedLastTimeAt", dateLs.First().ToString("yyyy-MM-dd"));

            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // else
            // {
            //     filter = Builders<Article>.Filter.Regex("publishedLastTimeAt", "bidon");
            //     foreach (var item in dateLs)
            //     {
            //         Console.WriteLine(item.ToString("yyyy-MM-dd"));
            //         filter = Builders<Article>.Filter.Or(filter,
            //             Builders<Article>.Filter.Regex("publishedLastTimeAt", item.ToString("yyyy-MM-dd")));
            //     }
            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // }
            // else
            // {
            // if (dateLs.Count == 0)
            // {
            //     Console.WriteLine(catLs.First().First().ToString().ToUpper() + catLs.First().Substring(1).ToLower());
            //     filter = Builders<Article>.Filter.Regex("themeTag.name",
            //         catLs.First().First().ToString().ToUpper() + catLs.First().Substring(1).ToLower());
            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // else if (dateLs.Count == 1)
            // {
            //     filter = Builders<Article>.Filter.Regex("publishedLastTimeAt", dateLs.First().ToString("yyyy-MM-dd"));
            //     filter = Builders<Article>.Filter.Or(filter, Builders<Article>.Filter.Regex("themeTag.name",
            //         catLs.First().First().ToString().ToUpper() + catLs.First().Substring(1).ToLower()));

            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // else
            // {
            //     filter = Builders<Article>.Filter.Regex("publishedLastTimeAt", "bidon");
            //     foreach (var item in dateLs)
            //     {
            //         Console.WriteLine(item.ToString("yyyy-MM-dd"));
            //         filter = Builders<Article>.Filter.Or(filter,
            //             Builders<Article>.Filter.Regex("publishedLastTimeAt", item.ToString("yyyy-MM-dd")));
            //     }
            //     filter = Builders<Article>.Filter.Or(filter, Builders<Article>.Filter.Regex("themeTag.name",
            //         catLs.First().First().ToString().ToUpper() + catLs.First().Substring(1).ToLower()));

            //     var req = articlesCollection.Find(filter);
            //     var count = req.Count();
            //     articles = req.Skip(ran.Next((int)count)).Limit(3).ToList();
            // }
            // }

            Console.WriteLine(ls.Count);
            if (dateLs.Count != 0)
            {
                return(ls.Where(x => x.PublishedLastTimeAt.ToLower()
                                .Contains(dateLs.First().ToString("yyyy-MM-dd"))).Take(1).ToList());
            }
            else
            {
                return(ls.Skip(ran.Next(ls.Count)).Take(1).ToList());
            }
        }