public async Task <ActionResult <ForaViewModel[]> > GetFora() { List <ForaViewModel> foraIndexItemViewModels = new List <ForaViewModel>(); List <Forum> fora = null; try { fora = await _context.Fora.ToListAsync(); } catch (Exception exception) { Console.WriteLine($"Reading fora tabe threw an exception {exception.Message}"); } foreach (Forum forum in fora) { ForaViewModel foraIndexItemViewModel = new ForaViewModel(); foraIndexItemViewModel.InjectFrom(forum); foraIndexItemViewModel.ThreadCount = await ControllerHelpers.GetThreadCountAsync(forum, _context).ConfigureAwait(false); foraIndexItemViewModel.LastThread = "Empty"; if (foraIndexItemViewModel.ThreadCount > 0) { LastForumThread LastThread = await ControllerHelpers.GetLatestThreadDataAsync(forum, _context).ConfigureAwait(false); foraIndexItemViewModel.LastThread = LastThread.Forum.Title + ": " + LastThread.Title + "By - " + LastThread.Author.UserName; } foraIndexItemViewModels.Add(foraIndexItemViewModel); } return(foraIndexItemViewModels.ToArray()); }
public static async Task <LastForumThread> GetLatestThreadDataAsync(Forum forum, JCarrollOnlineV3DbContext data) { LastForumThread lastThreadViewModel = new LastForumThread(); if (data == null) { throw new ArgumentNullException(nameof(data)); } Models.ThreadEntry forumThreadEntry = null; try { forumThreadEntry = await data.ForumThreadEntrys.Where(i => i.Forum.Id == forum.Id) .Include(i => i.Author) .OrderByDescending(i => i.UpdatedAt) .FirstOrDefaultAsync().ConfigureAwait(false); } catch (Exception exception) { Console.WriteLine($"ForumThreadEntrys query threw and exception: {exception.Message}"); } if (forumThreadEntry != null) { lastThreadViewModel.InjectFrom(forumThreadEntry); lastThreadViewModel.Author = new ApplicationUserViewModel(); lastThreadViewModel.Author.InjectFrom(forumThreadEntry.Author); lastThreadViewModel.Forum = new ForaViewModel(); lastThreadViewModel.Forum.InjectFrom(forumThreadEntry.Forum); bool rootNotFound = true; if (forumThreadEntry.Parent?.Id != null) { while (rootNotFound) { forumThreadEntry = await data.ForumThreadEntrys.FindAsync(forumThreadEntry.Parent?.Id).ConfigureAwait(false); if (forumThreadEntry != null) { if (forumThreadEntry.Parent?.Id == null) { rootNotFound = false; } } } } lastThreadViewModel.PostRoot = forumThreadEntry.Id; return(lastThreadViewModel); } return(null); }