コード例 #1
0
        public IActionResult Index()
        {
            var courses    = _courseData.GetThreeRandomCourses();
            var authors    = _userData.GetThreeRandomAuthors();
            var events     = _eventData.GetFiveRandomEvents();
            var news       = _newsData.GetFiveRandomNews();
            var categories = _courseData.GetCategories();

            List <HomeCourseViewModel>  courseViewModel  = new List <HomeCourseViewModel>();
            List <HomeAuthorsViewModel> authorsViewModel = new List <HomeAuthorsViewModel>();
            List <HomeEventViewModel>   eventsViewModel  = new List <HomeEventViewModel>();
            List <HomeNewsViewModel>    newsViewModel    = new List <HomeNewsViewModel>();
            List <string> categoriesNames = new List <string>();

            foreach (var course in courses)
            {
                courseViewModel.Add(new HomeCourseViewModel()
                {
                    Id                = course.Id,
                    CourseName        = course.Name,
                    CourseAuthor      = course.Author.UserName,
                    CourseDescription = course.Description,
                    CourseImage       = course.ImageUrl,
                    UsersCount        = course.Users.Count(),
                });
            }
            foreach (var author in authors)
            {
                authorsViewModel.Add(new HomeAuthorsViewModel()
                {
                    Name     = author.Name,
                    UserName = author.UserName,
                    Position = author.Status,
                    Subname  = author.Surname,
                    PhotoUrl = author.PhotoUrl
                });
            }
            foreach (var eventModel in events)
            {
                eventsViewModel.Add(new HomeEventViewModel()
                {
                    Id         = eventModel.Id,
                    Name       = eventModel.Name,
                    Address    = eventModel.Address,
                    SampleText = StringFormatter.FormatForPreview(eventModel.Text),
                    Time       = eventModel.Time,
                    ImageUrl   = eventModel.ImageUrl
                });
            }
            foreach (var newsModel in news.Skip(1))
            {
                newsViewModel.Add(new HomeNewsViewModel()
                {
                    Id     = newsModel.Id,
                    Author = string.IsNullOrEmpty(newsModel.Author.Name) || string.IsNullOrEmpty(newsModel.Author.Surname) ?
                             newsModel.Author.UserName : $"{newsModel.Author.Name} {newsModel.Author.Surname}",
                    ImageUrl    = newsModel.ImageUrl,
                    ReleaseTime = newsModel.ReleaseTime,
                    TextPreview = StringFormatter.FormatForPreview(newsModel.Text),
                    Title       = newsModel.Name,
                });
            }
            foreach (var category in categories)
            {
                categoriesNames.Add(category.Name);
            }
            HomeNewsViewModel bigNewsModel = null;

            if (news.Any())
            {
                var firstElement = news.First();
                bigNewsModel = new HomeNewsViewModel()
                {
                    Id     = firstElement.Id,
                    Author = string.IsNullOrEmpty(firstElement.Author.Name) || string.IsNullOrEmpty(firstElement.Author.Surname) ?
                             firstElement.Author.UserName : $"{firstElement.Author.Name} {firstElement.Author.Surname}",
                    ImageUrl    = firstElement.ImageUrl,
                    ReleaseTime = firstElement.ReleaseTime,
                    TextPreview = StringFormatter.FormatForBigNews(firstElement.Text),
                    Title       = firstElement.Name,
                };
            }

            HomeViewModel viewModel = new HomeViewModel
            {
                Courses         = courseViewModel,
                Authors         = authorsViewModel,
                Events          = eventsViewModel,
                News            = newsViewModel,
                BigNews         = bigNewsModel,
                CategoriesNames = categoriesNames,
                Stats           = new PlatformStatsViewModel()
                {
                    AuthorsCount = _userData.GetAuthorsCount(),
                    CourseCount  = _courseData.GetCourseCount(),
                    UserCount    = _userData.GetUserCount(),
                },
            };

            return(View(viewModel));
        }