public CourseDirectoryHelperTests()
 {
     Helper = new CourseDirectoryHelper(MockFileHelper.Object);
 }
        // GET: CourseDirectory
        // ASB TODO - Should we not be returning OK objects? rather than empty Views if something goes wrong?
        public ActionResult CourseSearchResult([FromQuery]  CourseSearchRequestModel requestModel)
        {
            Telemetry.TrackEvent($"Logging: Started: Controller = {nameof(CourseDirectoryController)}: Action = {nameof(CourseSearchResult)}: {nameof(Environment.MachineName)} = {Environment.MachineName}: {nameof(CorrelationContextAccessor.CorrelationContext.CorrelationId)} = {CorrelationContextAccessor.CorrelationContext.CorrelationId}");

            var dtStart           = DateTime.Now;
            var isPostcodeInvalid = false;

            if (TempData != null)
            {
                isPostcodeInvalid = (TempData["Location_IsInvalid"] != null && (bool)TempData["Location_IsInvalid"] == true);

                if (!string.IsNullOrWhiteSpace(requestModel.Location))
                {
                    var postcodeResult = PostcodeService.IsValidAsync(requestModel.Location).Result;
                    if (postcodeResult.IsFailure)
                    {
                        isPostcodeInvalid = true;
                        TempData["Location_IsInvalid"] = isPostcodeInvalid;
                        TempData["Location_Postcode"]  = requestModel.Location;

                        if (new UriBuilder(Request.Headers["Referer"]).Path != Request.Path)
                        {
                            return(RedirectToAction(nameof(Index)));
                        }
                    }
                }
                else
                {
                    TempData.Remove("Location_IsInvalid");
                    TempData.Remove("Location_Postcode");
                }
            }

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Starting to create course search criteria.");
            var criteria = CourseDirectory.CreateCourseSearchCriteria(requestModel);

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Finished creating course search criteria.");

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Starting call to course directory search from the course directory service.");
            var result = Service.CourseDirectorySearch(criteria, new PagingOptions(CourseDirectoryHelper.GetSortBy(requestModel.SortBy), requestModel.PageNo));

            Telemetry.TrackEvent($"[{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}] Finished calling course directory search from the course directory service.");

            if (!CourseDirectory.IsSuccessfulResult(result, Telemetry, "Course Search", requestModel.SubjectKeyword, dtStart))
            {
                return(View(nameof(Error), new Models.ErrorViewModel()
                {
                    RequestId = "Course Search: " + requestModel.SubjectKeyword.ToString() + ". " + (null != result ? result.Error : string.Empty)
                }));
            }

            //DEBUG_FIX - Add the flush to see if working straightaway
            //ASB TODO Why are we flushing here? We may not end up here due to higher up returns.
            //So that we could test the telemetry, a la the DEBUG_FIX
            Telemetry.Flush();

            int perPage = int.TryParse(Configuration["Tribal:PerPage"], out perPage) ? perPage : 0;

            Telemetry.TrackEvent($"Logging: Ended: Controller = {nameof(CourseDirectoryController)}: Action = {nameof(CourseSearchResult)}: {nameof(Environment.MachineName)} = {Environment.MachineName}: {nameof(CorrelationContextAccessor.CorrelationContext.CorrelationId)} = {CorrelationContextAccessor.CorrelationContext.CorrelationId}");

            return(View(new CourseSearchResultViewModel(result)
            {
                SubjectKeyword = requestModel.SubjectKeyword,
                Location = requestModel.Location,
                LocationHasError = isPostcodeInvalid,
                LocationRadius = (RadiusDistance)requestModel.LocationRadius,
                PerPage = perPage,
                StudyModes = requestModel.StudyModes,
                AttendanceModes = requestModel.AttendanceModes,
                AttendancePatterns = requestModel.AttendancePatterns,
                QualificationLevels = requestModel.QualificationLevels,
                IsDfe1619Funded = requestModel.IsDfe1619Funded,
                SortBy = CourseDirectoryHelper.GetSortBy(requestModel.SortBy),
            }));
        }