public ActionResult GetSearchAndCompareCourse(string providerCode, string courseCode)
        {
            var name = this.User.Identity.Name;

            if (string.IsNullOrWhiteSpace(providerCode) || string.IsNullOrWhiteSpace(courseCode))
            {
                return(BadRequest());
            }

            var courseMapper = new CourseMapper();

            var providerData         = _dataService.GetProviderForUser(name, providerCode);
            var orgEnrichmentData    = _enrichmentservice.GetProviderEnrichment(providerCode, name);
            var courseData           = _dataService.GetCourseForUser(name, providerCode, courseCode);
            var courseEnrichmentData = _enrichmentservice.GetCourseEnrichment(providerCode, courseCode, name);

            if (providerData == null || courseData == null)
            {
                return(NotFound());
            }

            var course = courseMapper.MapToSearchAndCompareCourse(
                providerData,
                courseData,
                orgEnrichmentData?.EnrichmentModel,
                courseEnrichmentData?.EnrichmentModel);

            return(Ok(course));
        }
예제 #2
0
        public List <SearchCourse> ReadAllCourseData(IManageCoursesDbContext context)
        {
            _logger.Information("Retrieving courses");
            var courses = context.Courses
                          .Where(x => x.Provider.RecruitmentCycle.Year == RecruitmentCycle.CurrentYear)
                          .Include(x => x.Provider)
                          .Include(x => x.CourseSubjects).ThenInclude(x => x.Subject)
                          .Include(x => x.AccreditingProvider)
                          .Include(x => x.CourseSites).ThenInclude(x => x.Site)
                          .Include(x => x.CourseEnrichments).ThenInclude(x => x.CreatedByUser)
                          .Include(x => x.CourseEnrichments).ThenInclude(x => x.UpdatedByUser)
                          .ToList();

            foreach (var course in courses)
            {
                course.CourseSites = new Collection <CourseSite>(course.CourseSites.Where(x => x.Publish == "Y").ToList());
            }

            _logger.Information($" - {courses.Count()} courses");

            _logger.Information("Retrieving enrichments");

            var orgEnrichments = context.ProviderEnrichments
                                 .Include(x => x.CreatedByUser)
                                 .Include(x => x.UpdatedByUser)
                                 .Where(x => x.Status == EnumStatus.Published && x.Provider.RecruitmentCycle.Year == RecruitmentCycle.CurrentYear)
                                 .ToLookup(x => x.ProviderCode)
                                 .ToDictionary(x => x.Key, x => x.OrderByDescending(y => y.UpdatedAt).First());

            _logger.Information($" - {orgEnrichments.Count()} orgEnrichments");

            var courseMapper = new CourseMapper();
            var converter    = new EnrichmentConverter();

            var mappedCourses = new List <SearchAndCompare.Domain.Models.Course>();

            _logger.Information("Combine courses with provider and enrichment data");

            foreach (var c in courses)
            {
                var courseEnrichment = c.CourseEnrichments
                                       .OrderByDescending(y => y.UpdatedAt)
                                       .FirstOrDefault(x => x.Status == EnumStatus.Published);

                var mappedCourse = courseMapper.MapToSearchAndCompareCourse(
                    c.Provider,
                    c,
                    converter.Convert(orgEnrichments.GetValueOrDefault(c.Provider.ProviderCode))?.EnrichmentModel,
                    converter.Convert(courseEnrichment)?.EnrichmentModel
                    );

                if (!mappedCourse.Campuses.Any())
                {
                    // only publish running courses
                    continue;
                }

                if (!mappedCourse.CourseSubjects.Any())
                {
                    _logger.Warning(
                        $"failed to assign subject to [{c.Provider.ProviderCode}]/[{c.CourseCode}] {c.Name}. UCAS tags: {c.Subjects}");
                    // only publish courses we could map to one or more subjects.
                    continue;
                }

                // hacks - remove when coursemapper refactor has completed
                mappedCourse.ProviderLocation = new Location {
                    Address = mappedCourse.ContactDetails.Address
                };
                mappedCourse.Fees   = mappedCourse.Fees ?? new Fees();
                mappedCourse.Salary = mappedCourse.Salary ?? new Salary();

                mappedCourses.Add(mappedCourse);
            }

            return(mappedCourses);
        }