示例#1
0
        public async Task <IActionResult> Index(int?category, int page = 1)
        {
            if (!AuthUser.Identity.IsAuthenticated)
            {
                return(RedirectToSignIn());
            }

            if (!UserHasPermission(Permission.AccessMissionControl))
            {
                // not authorized for Mission Control, redirect to authorization code

                return(RedirectToAction(nameof(AuthorizationCode)));
            }

            // change user to default language
            if (Culture.DefaultName != _userContextProvider.GetCurrentCulture().Name)
            {
                AlertInfo = $"Language changed to <strong>{Culture.DefaultCulture.DisplayName}</strong> for <strong>Mission Control</strong> <span class=\"fas fa-rocket\"></span>.";
                return(RedirectToAction(nameof(Index), new { culture = Culture.DefaultName }));
            }

            Site site = await GetCurrentSiteAsync();

            var viewModel = new AtAGlanceViewModel
            {
                AtAGlanceReport = await GetAtAGlanceAsync(),
                ShowPosts       = await _newsService.AnyPublishedPostsAsync()
            };

            if (viewModel.ShowPosts)
            {
                var filter = new NewsFilter(page, PostsPerPage)
                {
                    IsActive = true
                };

                if (category.HasValue)
                {
                    filter.CategoryIds = new List <int> {
                        category.Value
                    };
                }
                else
                {
                    filter.DefaultCategory = true;
                }

                var postList = await _newsService.GetPaginatedPostListAsync(filter);

                var paginateModel = new PaginateViewModel
                {
                    ItemCount    = postList.Count,
                    CurrentPage  = page,
                    ItemsPerPage = filter.Take.Value
                };
                if (paginateModel.PastMaxPage)
                {
                    return(RedirectToRoute(
                               new
                    {
                        page = paginateModel.LastPage ?? 1
                    }));
                }

                foreach (var post in postList.Data)
                {
                    post.Content       = CommonMark.CommonMarkConverter.Convert(post.Content);
                    post.CreatedByName = await _userService.GetUsersNameByIdAsync(post.CreatedBy);
                }

                var user = await _userService.GetDetails(GetId(ClaimType.UserId));

                viewModel.IsNewsSubscribed = user.IsNewsSubscribed;
                viewModel.NewsPosts        = postList.Data;
                viewModel.NewsCategories   = await _newsService.GetAllCategoriesAsync();

                foreach (var item in viewModel.NewsCategories)
                {
                    item.IsNew = _newsService.WithinTimeFrame(item.LastPostDate, 7);
                }
                viewModel.PaginateModel          = paginateModel;
                viewModel.SiteAdministratorEmail = site.FromEmailAddress;
            }

            PageTitle = $"Mission Control: {site.Name}";

            return(View(viewModel));
        }