private async Task InitializeForumThemes()
 {
     if (_context.ForumThemes.Any()) return;
     var themes = new List<ForumTheme>();
     for (int i = 0; i < 16; i++)
     {
         var messages = new List<ForumMessage>();
         for (int j = 0; j < 23; j++)
         {
             var message = new ForumMessage()
             {
                 Message = "message" + i,
                 AdditionTime = DateTime.Now.AddHours(-20 + i),
                 AuthorId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id),
                 LastModifiedTime = DateTime.Now.AddHours(-20 + i),
                 ThemeId = 1,
             };
             messages.Add(message);
         }
         var userId = new Random().Next(_context.Users.First().Id, _context.Users.Last().Id);
         var forumTheme = new ForumTheme()
         {
             Name = "theme " + i,
             Description = "theme description " + i,
             SubsectionId = 1,
             LastMessageAdditionTime = DateTime.Today,
             Messages = messages,
             AuthorId = userId,
             LastAnswerUserId = userId
         };
         themes.Add(forumTheme);
     }
 
     themes.ForEach(x => _context.ForumThemes.Add(x));
     await _context.SaveChangesAsync();
 }
Beispiel #2
0
        private static void UpdateForumComments()
        {
            Console.WriteLine("Start UpdateForumComments");
            using (FileStream fs = new FileStream(Path + "forump.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;
                }
                for (int i = 0; i < limit; i++)
                {
                    ForumMessage forumMessage = new ForumMessage();
                    // id
                    string id = null;
                    while (chars[i] != '|')
                    {
                        id += chars[i];
                        i++;
                    }
                    i++;
                    forumMessage.OldId = int.Parse(id);

                    // module id
                    string moduleId = null;
                    while (chars[i] != '|')
                    {
                        moduleId += chars[i];
                        i++;
                    }
                    i++;
                    //  var theme = UnitOfWork.ForumThemeRepository.GetAsync().Result.FirstOrDefault(x => x.IdOld == int.Parse(moduleId));
                    var theme = Themes.FirstOrDefault(x => x.IdOld == int.Parse(moduleId));
                    if (UseLimit && theme != null)
                    {
                        // theme.Messages = 
                        forumMessage.ThemeId = theme.Id;
                    }

                    //material id
                    string additionTime = null;
                    while (chars[i] != '|')
                    {
                        additionTime += chars[i];
                        i++;
                    }
                    i++;
                    forumMessage.AdditionTime = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(additionTime));
                    forumMessage.LastModifiedTime = forumMessage.AdditionTime;
                    // pending
                    while (chars[i] != '|')
                    {
                        if (chars[i] == 1)
                            forumMessage.IsFirstMessage = true;
                        i++;
                    }
                    i++;
                    // message
                    while (chars[i] != '|')
                    {
                        forumMessage.Message += chars[i];
                        i++;
                    }
                    i++;
                    //user is reg
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i++;
                    //author
                    string userName = null;
                    while (chars[i] != '|')
                    {
                        userName += chars[i];
                        i++;
                    }
                    i++;
                    var author = UserRepository.FindByNameAsync(userName).Result;
                    forumMessage.AuthorId = author?.Id ?? _deleted.Id;

                    while (chars[i] != 10)
                    {
                        i++;
                    }
                    if (UseLimit)
                    {
                        theme?.Messages.Add(forumMessage);
                        //  UnitOfWork.ForumMessageRepository.Add(forumMessage);
                    }


                }
                foreach (var item in Themes)
                {
                    if (item.Messages.Any())
                    {
                        ForumThemeRepository.AddAsync(item).RunSynchronously();
                    }
                }
                ForumThemeRepository.SaveChangesAsync().RunSynchronously();
            }
            Console.WriteLine("End UpdateForumComments");
        }
 public async Task DeleteAsync(ForumMessage entity)
 {
     await Task.FromResult(_context.ForumMessages.Remove(entity));
 }
 public void Update(ForumMessage entity)
 {
     _context.ForumMessages.Attach(entity);
     _context.Entry(entity).State = EntityState.Modified;
 }
 public async Task<ForumMessage> AddAsync(ForumMessage entity)
 {
     var addedEntity = await _context.ForumMessages.AddAsync(entity);
     return addedEntity.Entity;
 }