Esempio n. 1
0
        private async Task InitializeNewsComments()
        {
            if (_context.MaterialComments.Any()) return;
            var newsComments = new List<MaterialComment>();
            var random = new Random((int)DateTime.UtcNow.Ticks);
            for (int i = 0; i < CountNewsComments; i++)
            {
                var comment = new MaterialComment()
                {
                    MaterialId = new Random().Next(_context.Materials.First(x => x.Type == NewsType).Id, _context.Materials.Last(x => x.Type == NewsType).Id),
                    AdditionTime = DateTime.Now.AddDays(random.NextDouble() * 10),
                    Answer = i % 5 == 0 ? "answer" : string.Empty,
                    AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                    Message = "message " + i,
                    Pending = (i + 3) % 5 == 0,
                    MaterialType = NewsType,
                    LastModified = DateTime.Now,
                };
                if (i % 3 == 0)
                {
                    comment.Children = new List<MaterialComment>()
                    {
                        new MaterialComment()
                        {
                            ParentId = i + 1,
                            Pending = false,
                            MaterialId = comment.MaterialId,
                            AdditionTime = DateTime.Now,
                    AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                            Message = "comment inside",
                            MaterialType = NewsType,
                            LastModified = DateTime.Now,
                        }
                    };
                    i += 1;
                }
                newsComments.Add(comment);
            }

            newsComments.ForEach(x => _context.MaterialComments.Add(x));
            await _context.SaveChangesAsync();

            var commentFirstNews = new MaterialComment()
            {
                Pending = false,
                MaterialId = _context.Materials.First(x => x.Type == NewsType).Id,
                AdditionTime = DateTime.Now,
                AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                Message = "comment first",
                MaterialType = NewsType,
                LastModified = DateTime.Now,
                Children = new List<MaterialComment>()
                {
                    new MaterialComment()
                    {
                        ParentId = CountNews + 1,
                        Pending = false,
                                MaterialId = _context.Materials.First(x => x.Type == NewsType).Id,
                        AdditionTime = DateTime.Now,
                    AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                        Message = "comment second inside",
                        MaterialType = NewsType,
                        LastModified = DateTime.Now,
                        Children = new List<MaterialComment>()
                        {
                            new MaterialComment()
                            {
                                ParentId = CountNews + 2,
                                Pending = false,
                                MaterialId = _context.Materials.First(x => x.Type == NewsType).Id,
                                AdditionTime = DateTime.Now,
                    AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                                Message = "comment three inside",
                                MaterialType = NewsType,
                                LastModified = DateTime.Now,
                            }
                        }
                    }
                }
            };

            _context.MaterialComments.Add(commentFirstNews);
            await _context.SaveChangesAsync();
        }
Esempio n. 2
0
        private static void UpdateComments()
        {
            Console.WriteLine("Start UpdateComments");
            using (FileStream fs = new FileStream(Path + "comments.txt", FileMode.Open))
            {
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, Convert.ToInt32(fs.Length));

                char[] chars = Encoding.UTF8.GetString(data).ToCharArray();
                var limit = chars.Length;
                if (UseLimit && MaxChars < chars.Length)
                {
                    limit = MaxChars * 10;
                }

                var news = MaterialRepository.GetAsync().Result;//n => n.NumberCommentaries > 0);
                                                                           //   var blogs = UnitOfWork.BlogItemRepository.GetAsync().Result;//n => n.NumberCommentaries > 0);

                for (int i = 0; i < limit; i++)
                {

                    // id
                    string id = null;
                    while (chars[i] != '|')
                    {
                        id += chars[i];
                        i++;
                    }
                    i++;

                    // module id
                    string moduleId = null;
                    while (chars[i] != '|')
                    {
                        moduleId += chars[i];
                        i++;
                    }
                    i++;
                    var ModuleId = int.Parse(moduleId);


                    //material id
                    string materialId = null;
                    while (chars[i] != '|')
                    {
                        materialId += chars[i];
                        i++;
                    }
                    i++;

                    // pending
                    char pending = '0';
                    while (chars[i] != '|')
                    {
                        pending = chars[i];
                        i++;
                    }
                    i++;
                    // add time
                    string additionTime = null;
                    while (chars[i] != '|')
                    {
                        additionTime += chars[i];
                        i++;
                    }
                    i++;

                    //author
                    string userName = null;
                    while (chars[i] != '|')
                    {
                        userName += chars[i];
                        i++;
                    }
                    i++;

                    //name
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i++;
                    //email
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i++;
                    //www
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i++;
                    //ip
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i++;
                    // message
                    string message = null;
                    while (chars[i] != '|')
                    {
                        message += chars[i];
                        i++;
                    }
                    i++;

                    // answer
                    string answer = null;
                    while (chars[i] != '|')
                    {
                        answer += chars[i];
                        i++;
                    }
                    i++;


                    // user id
                    string userId = null;
                    while (chars[i] != '|')
                    {
                        userId += chars[i];
                        i++;
                    }
                    i++;
                    // parent id
                    string parentId = null;
                    while (chars[i] != '|')
                    {
                        parentId += chars[i];
                        i++;
                    }
                    i++;


                    while (chars[i] != 10)
                    {
                        i++;
                    }

                    // user id
                    //string rate = null;
                    //while (chars[i] != '|')
                    //{
                    //    rate += chars[i];
                    //    i++;
                    //}
                    //i++;
                    //// rate
                    //string rateId = null;
                    //while (chars[i] != '|')
                    //{
                    //    rateId += chars[i];
                    //    i++;
                    //}
                    //i++;




                    //foreach (var comment in newsComments)
                    //{
                    //    foreach (var item in news.Where(newsItem => comment.MaterialId == newsItem.Id))
                    //    {
                    //        if (item.Children == null)
                    //        {
                    //            item.Children = new List<NewsComment>();
                    //        }
                    //        item.Children.Add(comment);
                    //        comment.MaterialId = item.Id;
                    //    }
                    //}
                    if (ModuleId != 1 && ModuleId != 2)
                    {
                        continue;
                        //moduleID (1=>'blog',2=>'news',3=>'publ',4=>'photo',5=>'load',6=>'dir',7=>'board')
                    }
                    var materialType = MaterialType.Error;
                    if (ModuleId == 2)
                    {
                        materialType = MaterialType.News;
                    }
                    else if (ModuleId == 1)
                    {
                        materialType = MaterialType.Blog;
                    }
                    MaterialComment comment = new MaterialComment()
                    {
                        MaterialType = materialType,
                        MaterialId = int.Parse(materialId),
                        OldId = int.Parse(id),
                    };

                    comment.Material = news.FirstOrDefault(newsItem => comment.MaterialId == newsItem.OldId && comment.MaterialType == materialType);
                    if (comment.Material == null)
                    {
                        continue;
                    }
                    if (comment.Material.Comments == null)
                    {
                        comment.Material.Comments = new List<MaterialComment>();
                    }
                    comment.Material.Comments.Add(comment);
                    if (pending == '1')
                        comment.Pending = true;
                    comment.AdditionTime = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(additionTime));
                    comment.Author = UserRepository.FindByNameAsync(userName).Result;
                    if (comment.Author == null)
                    {
                        comment.AuthorId = _deleted.Id;
                    }
                    comment.Answer = answer;
                    comment.Message = message;
                    var parId = int.Parse(parentId);
                    if (parId > 0)
                    {
                        comment.Parent = MaterialCommentRepository.GetByIdAsync(parId).Result;
                    }
                    //comment.ParentId = int.Parse(parentId);

                    MaterialCommentRepository.AddAsync(comment);

                }
                MaterialCommentRepository.SaveChangesAsync().RunSynchronously();
                //?? UnitOfWork.Save();
            }
        }