コード例 #1
0
        // Course topics list.
        public async Task <ActionResult> CourseTopics(int?id)
        {
            try
            {
                // I.Checks.
                // Check id.
                if (!int.TryParse(id.ToString(), out int intId))
                {
                    return(RedirectToAction("Index"));
                }
                // Get courseDTO object.
                CourseDTO courseDTO = await CourseService.GetAsync(intId);

                if (courseDTO == null)
                {
                    return(RedirectToAction("Index"));
                }

                // II. AutoMapper Setup.
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <TestResultDTO, TestResultViewModel>()
                    .ForMember("Id", opt => opt.MapFrom(tr => tr.TestResultId))
                    .ForMember("TestTitle", opt => opt.MapFrom(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.TopicTitle));
                    cfg.CreateMap <TestResultDetailDTO, TestResultDetailViewModel>()
                    .ForMember("Question", opt => opt.MapFrom(trd => trd.Question.QuestionText))
                    .ForMember("Topic", opt => opt.MapFrom(trd => trd.Question.Topic.TopicTitle));
                    cfg.CreateMap <TopicDTO, TopicViewModel>()
                    .ForMember("ID", opt => opt.MapFrom(obj => obj.TopicId))
                    .ForMember("Name", opt => opt.MapFrom(obj => obj.TopicTitle));
                });
                IMapper iMapper = config.CreateMapper();

                // III. Set ViewBag properties.
                // Get a role name for the current user.
                string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                if (currentUserId != null)
                {
                    ViewBag.RoleName = UserService.FindUserRoleById(currentUserId);
                }
                else
                {
                    ViewBag.RoleName = null;
                }
                // Check: is this user subscribed to this course?
                SubscriptionDTO activeSubscription = SubscriptionService.Find(obj =>
                                                                              obj.UserProfileId == currentUserId &&
                                                                              obj.CourseId == intId &&
                                                                              (DateTime.Now - obj.StartDate < TimeSpan.FromDays(obj.SubscriptionPeriod)))
                                                     .FirstOrDefault();
                ViewBag.IsInApproving = false;
                ViewBag.IsSubscribed  = false;
                if (activeSubscription != null)
                {
                    if (activeSubscription.IsApproved)
                    {
                        ViewBag.IsSubscribed = true;
                    }
                    else
                    {
                        ViewBag.IsInApproving = true;
                    }
                }
                ViewBag.ParentId       = intId;
                ViewBag.CourseName     = courseDTO.CourseTitle;
                ViewBag.ParentParentId = courseDTO.SpecialityId;
                IEnumerable <TestResultDTO> testResultDTOs = TestResultService.Find(tr => tr.UserProfileId == currentUserId &&
                                                                                    tr.IsTopicTest);
                IEnumerable <TestResultViewModel> userTestResults = iMapper.Map <IEnumerable <TestResultDTO>, IEnumerable <TestResultViewModel> >(testResultDTOs);
                ViewBag.UserTestResults = userTestResults;
                ViewBag.AttemptsNumber  = courseDTO.AttemptsNumber;

                // IV. Get data for a view.
                List <TopicDTO> source = TopicService.Find(dto => dto.CourseId == intId).OrderBy(o => o.TopicNumber).ToList();
                IEnumerable <TopicViewModel> topicOrderedList = iMapper.Map <IEnumerable <TopicDTO>, IEnumerable <TopicViewModel> >(source);

                // VI.
                return(View(topicOrderedList));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }