Ejemplo n.º 1
0
        public async Task <ActionResult <ThreadViewModel[]> > GetForum(int forumId)
        {
            List <ThreadViewModel> forumThreadEntries = new List <ThreadViewModel>();

            Forum currentForum = await _context.Fora.Include(i => i.ForumThreadEntries).ThenInclude(i => i.Author).FirstOrDefaultAsync(cf => cf.Id == forumId);

            // Create the view model
            foreach (ThreadEntry threadEntry in currentForum.ForumThreadEntries)
            {
                ThreadViewModel forumThreadEntryViewModel = new ThreadViewModel();

                forumThreadEntryViewModel.InjectFrom(threadEntry);

                forumThreadEntryViewModel.Author = new ApplicationUserViewModel();
                forumThreadEntryViewModel.Author.InjectFrom(threadEntry);

                forumThreadEntryViewModel.Replies = currentForum.ForumThreadEntries.Where(forumThreadEntry => forumThreadEntry.Root?.Id == threadEntry.Id && forumThreadEntry.Parent != null).Count();
                IOrderedEnumerable <ThreadEntry> interim = currentForum.ForumThreadEntries.Where(m => m.Root?.Id == threadEntry.Id).OrderBy(m => m.UpdatedAt.ToFileTime());
                if (interim.Count() > 0)
                {
                    forumThreadEntryViewModel.LastReply = interim.FirstOrDefault().UpdatedAt.ToString();
                }
                else
                {
                    forumThreadEntryViewModel.LastReply = DateTime.Now.ToString();
                }

                forumThreadEntries.Add(forumThreadEntryViewModel);
            }

            return(forumThreadEntries.ToArray());
        }
        public async Task <ActionResult <ThreadViewModel[]> > GetThread(int threadId)
        {
            List <ThreadViewModel> forumThreadEntries = new List <ThreadViewModel>();

            List <ThreadEntry> threadEntries = new List <ThreadEntry>();

            threadEntries.Add(await _context.ForumThreadEntrys.Where(te => te.Id == threadId).Include(te => te.Author).Include(te => te.Children).Include(te => te.Forum).FirstOrDefaultAsync());

            // Create the view model
            foreach (ThreadEntry threadEntry in threadEntries)
            {
                ThreadViewModel threadViewModel = new ThreadViewModel();

                threadViewModel.InjectFrom(threadEntry);
                threadViewModel.Author = new ApplicationUserViewModel();
                threadViewModel.Author.InjectFrom(threadEntry.Author);

                threadViewModel.CreatedAt = threadEntry.CreatedAt.ToString();
                threadViewModel.UpdatedAt = threadEntry.UpdatedAt.ToString();

                threadViewModel.Forum = new ForumViewModel();
                threadViewModel.Forum.InjectFrom(threadEntry.Forum);

                threadViewModel.Replies = threadEntries.Where(forumThreadEntry => forumThreadEntry.Root != null && forumThreadEntry.Root.Id == threadEntry.Id && forumThreadEntry.Parent != null).Count();
                IOrderedEnumerable <ThreadEntry> children = threadEntries.Where(m => m.Root != null && m.Id == threadEntry.Id).OrderBy(m => m.UpdatedAt.ToFileTime());

                if (threadEntry.Children?.Count() > 0)
                {
                    threadViewModel.LastReply = threadEntry.Children.FirstOrDefault().UpdatedAt.ToString();
                    List <ThreadViewModel> childViewModels = new List <ThreadViewModel>();

                    foreach (ThreadEntry child in threadEntry.Children)
                    {
                        ThreadViewModel childViewModel = new ThreadViewModel();

                        childViewModel.InjectFrom(child);
                        childViewModels.Add(childViewModel);
                    }

                    threadViewModel.Children = childViewModels.ToArray();
                }
                else
                {
                    threadViewModel.LastReply = DateTime.Now.ToString();
                    threadViewModel.Children  = new ThreadViewModel[] { };
                }

                forumThreadEntries.Add(threadViewModel);
            }

            return(forumThreadEntries.ToArray());
        }