public async Task <bool> CreateThemeAsync (string title, string description, string creatorId, int forumSectionId, DateTime publishedDate) { if (title == null || description == null || creatorId == null || forumSectionId == 0 || publishedDate == null) { return(false); } var theme = new ForumTheme { Title = title, Description = description, Creator = this.db.Users.Where(u => u.Id == creatorId).FirstOrDefault(), ForumSection = this.db.ForumSections.Where(fs => fs.Id == forumSectionId).FirstOrDefault(), PublishedDate = publishedDate }; this.db.Add(theme); await this.db.SaveChangesAsync(); return(true); }
public ActionResult AddTheme(ForumTheme theme) { theme.themeDate = DateTime.Now; cm.forumThemes.Add(theme); cm.SaveChanges(); return(RedirectToAction("Index")); }
public async Task AddForumTheme(AddForumThemeRequest request) { ForumTheme ft = new ForumTheme() { Header = request.Header, Description = request.Description, Icon = request.Icon }; await _repository.AddForumTheme(ft); }
public async Task <ForumTheme> AddAsync(ForumTheme entity) { var addedEntity = await _context.ForumThemes.AddAsync(entity); await _context.SaveChangesAsync(); return(addedEntity.Entity); }
public ActionResult Theme(int id) { ForumTheme theme = cm.forumThemes.Where(m => m.Id == id).FirstOrDefault(); List <Comment> themeComments = cm.comments.Where(a => a.theme.Id == id).ToList(); ViewBag.theme = theme; ViewBag.CommentList = themeComments; return(View()); }
public async Task BySectionIdAsyncReturnsTheRightAnswerOrderedProperly() { //Arrange var dbOptions = new DbContextOptionsBuilder <UndergroundStationDbContext>() .UseInMemoryDatabase("UndergroundStationTestDb") .Options; var db = new UndergroundStationDbContext(dbOptions); var themesService = new ThemesService(db); var firstTheme = new ForumTheme { Id = 1, PublishedDate = DateTime.UtcNow, ForumSectionId = 1 }; var secondTheme = new ForumTheme { Id = 2, PublishedDate = DateTime.UtcNow.AddDays(-1), ForumSectionId = 1 }; var thirdTheme = new ForumTheme { Id = 3, IsDeleted = true, ForumSectionId = 1 }; var fourthTheme = new ForumTheme { Id = 4, ForumSectionId = 2 }; db.AddRange(firstTheme, secondTheme, thirdTheme, fourthTheme); await db.SaveChangesAsync(); //Act var result = await themesService .BySectionIdAsync(1); //Assert result .Should() .Match (r => r.ElementAt(0).Id == 2 && r.ElementAt(1).Id == 1) .And .HaveCount(2); }
protected override void Seed(ApplicationDbContext context) { var userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(context)); var defaultUser = new ApplicationUser { Email = "*****@*****.**", UserName = "******" }; var defaultPassword = "******"; userManager.Create(defaultUser, defaultPassword); var user1 = new ApplicationUser { Email = "*****@*****.**", UserName = "******" }; var user1Password = "******"; userManager.Create(user1, user1Password); var themeMsg1 = new ThemeMessage { Text = "Some text 1", Publisher = user1 }; var themeMsg2 = new ThemeMessage { Text = "Some text 2", Publisher = user1 }; var forumTheme1 = new ForumTheme { Title = "Some title", Description = "Some description 1", Messages = new List <ThemeMessage> { themeMsg1, themeMsg2 }, Owner = defaultUser, IsPublished = true, PublishDate = DateTime.Now }; context.ForumThemes.Add(forumTheme1); context.SaveChanges(); }
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(); }
private static void UpdateForumThemes() { Console.WriteLine("Start UpdateForumThemes"); using (FileStream fs = new FileStream(Path + "forum.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++) { ForumTheme forumTheme = new ForumTheme(); // id string id = null; while (chars[i] != '|') { id += chars[i]; i++; } i++; forumTheme.IdOld = int.Parse(id); // section id string sectionId = null; while (chars[i] != '|') { sectionId += chars[i]; i++; } i++; var subsection = ForumSubsectionRepository.GetListAsync().Result.First(x => x.IdOld == int.Parse(sectionId)); forumTheme.SubsectionId = subsection.Id; // isPoll while (chars[i] != '|') { if (chars[i] == '1') forumTheme.IsPool = true; i++; } i++; // on top while (chars[i] != '|') { if (chars[i] == '1') forumTheme.OnTop = true; i++; } i++; // last modified string lastDateMessage = null; while (chars[i] != '|') { lastDateMessage += chars[i]; i++; } i++; forumTheme.LastMessageAdditionTime = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(lastDateMessage)); // is CLosed while (chars[i] != '|') { if (chars[i] == '1') forumTheme.IsClosed = true; i++; } i++; // answers string answers = null; while (chars[i] != '|') { answers += chars[i]; i++; } i++; forumTheme.Answers = int.Parse(answers); // views string views = null; while (chars[i] != '|') { views += chars[i]; i++; } i++; forumTheme.Views = int.Parse(views); // name while (chars[i] != '|') { forumTheme.Name += chars[i]; i++; } i++; // Description while (chars[i] != '|') { forumTheme.Description += chars[i]; i++; } i++; // author string author = null; while (chars[i] != '|') { author += chars[i]; i++; } i++; forumTheme.Author = UserRepository.FindByNameAsync(author).Result; // user reg???? while (chars[i] != '|') { //author += chars[i]; i++; } i++; // author last answer string authorLastMessage = null; while (chars[i] != '|') { authorLastMessage += chars[i]; i++; } i++; forumTheme.LastAnswerUser = UserRepository.FindByNameAsync(authorLastMessage).Result; Themes.Add(forumTheme); // UnitOfWork.ForumThemeRepository.Add(forumTheme); while (chars[i] != 10) { i++; } } // UnitOfWork.Save(); } }
public void Update(ForumTheme entity) { _context.ForumThemes.Attach(entity); _context.Entry(entity).State = EntityState.Modified; }
public async Task DeleteAsync(ForumTheme entity) { await Task.FromResult(_context.ForumThemes.Remove(entity)); }
public async Task<ForumTheme> AddAsync(ForumTheme entity) { var addedEntity = await _context.ForumThemes.AddAsync(entity); return addedEntity.Entity; }
public async Task UpdateAsync(ForumTheme entity) { _context.ForumThemes.Attach(entity); _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(); }
public async Task DeleteAsync(ForumTheme entity) { _context.ForumThemes.Remove(entity); await _context.SaveChangesAsync(); }
public async Task AddForumTheme(ForumTheme ft) { using var context = ContextFactory.CreateDbContext(ConnectionString); context.ForumThemes.Add(ft); await context.SaveChangesAsync(); }