Exemplo n.º 1
0
        public async Task <IActionResult> Index()
        {
            var userId = User.GetUserId();

            var user = _userData.GetUserWithStats(userId);

            var coursesAuthor = _data.GetCoursesByAuthor(user);

            var courses = _data.GetCoursesOfUser(user).ToList();

            var coursesForModel = new List <CourseProfileViewModel>();
            var ownCourses      = new List <OwnCourseProfileViewModel>();
            var ownEvents       = new List <PostProfileViewModel>();
            var blogEvents      = new List <PostProfileViewModel>();

            foreach (var course in courses)
            {
                string authorName = "";
                if (course.Author.Name == null || course.Author.Surname == null)
                {
                    authorName = course.Author.UserName;
                }
                else
                {
                    authorName = course.Author.Name + course.Author.Surname;
                }
                coursesForModel.Add(new CourseProfileViewModel
                {
                    Id              = course.Id,
                    Name            = course.Name,
                    Author          = authorName,
                    AuthorImageUrl  = course.Author.PhotoUrl,
                    ImageUrl        = course.ImageUrl,
                    DurationInHours = course.DurationInHours,
                });
            }
            foreach (var eventModel in user.Events)
            {
                ownEvents.Add(new PostProfileViewModel
                {
                    Id       = eventModel.Id,
                    Title    = eventModel.Name,
                    UrlImage = eventModel.ImageUrl
                });
            }
            foreach (var course in coursesAuthor)
            {
                ownCourses.Add(new OwnCourseProfileViewModel
                {
                    Id          = course.Id,
                    Name        = course.Name,
                    Subscribers = course.Users.Count(),
                });
            }
            foreach (var post in user.Blogs)
            {
                blogEvents.Add(new PostProfileViewModel
                {
                    Id       = post.Id,
                    Title    = post.Name,
                    UrlImage = post.ImageUrl
                });
            }
            var userWithRoles = _userData.GetUser(HttpContext.User.GetUserId());

            var model = new ProfileViewModel
            {
                Name              = user.Name,
                AvatarUrl         = user.PhotoUrl,
                Courses           = coursesForModel,
                UserName          = user.UserName,
                Surname           = user.Surname,
                CoursesComplete   = user.CourseStatistics.Count(c => c.TasksCount == c.TasksCompetedCount),
                CoursesInProgress = user.CourseStatistics.Count(c => c.TasksCount != c.TasksCompetedCount),
                CoursesInTotal    = user.CourseStatistics.Count,
                HaveBlogs         = user.Blogs.Count > 0,
                HaveEvents        = user.Events.Count > 0,
                IsCourseCreator   = await _userManager.IsInRoleAsync(_userData.GetUser(HttpContext.User.GetUserId()), Roles.CourseCreator),
                Blogs             = blogEvents,
                Events            = ownEvents,
                MyCourses         = ownCourses,
            };


            return(View(model));
        }