/// /// This method retrieves Key Information for Students data from the KIS Data Service. /// It uses the Simple.OData.Client library to connect to the service - this /// library can also support strongly typed connections if desired. /// /// A Key Information for Students for the qualification requested private KeyInformationData GetKeyInformationData() { //Set up the client to connect to the web service url var client = new ODataClient(" https://info4learners.tec.govt.nz/"); //Pass an OData query to the service and get the result (the library we //are using requires this to be an asynchronous call) //This query asks for Key Information for Students where the provider number and //qualification code match the ones we are using var oDataResult = Task.Run(async () => await client.FindEntriesAsync($"KeyInformation?$filter=ProviderCode eq '{_edumisNumber}' and QualificationCode eq '{_qualificationCode}'")) .GetAwaiter().GetResult().ToList(); //We know that there should only be one Key Information for Students record for a //qualification, so any other result means something isn't right if(oDataResult.Count != 1) { throw new Exception($"OData call for a Key Information for Students for edumis {_edumisNumber} qualification {_qualificationCode} returned {oDataResult.Count} records."); } //Pull the values we are interested in out of the result and put them //into a model class the view can use //We can safely use First() because we checked previously there was only //one record returned var kis = new KeyInformationData { CourseCompletion = oDataResult.First()["CourseCompletion"].ToString(), Duration = oDataResult.First()["Duration"].ToString(), MinimumEntryRequirements = oDataResult.First()["MinimumEntryRequirements"].ToString(), GovernmentSubsidy = oDataResult.First()["GovernmentSubsidy"].ToString(), AnnualStudentFees = oDataResult.First()["AnnualStudentFees"].ToString(), AnnualGovernmentTuitionSubsidy = oDataResult.First()["AnnualGovernmentTuitionSubsidy"].ToString(), Total_Annual = oDataResult.First()["Total_Annual"].ToString(), Total_TotalQualification = oDataResult.First()["Total_TotalQualification"].ToString(), GraduatesInEmployment = oDataResult.First()["GraduatesInEmployment"].ToString(), GraduatesInStudy = oDataResult.First()["GraduatesInStudy"].ToString(), GraduatesOnBenefit = oDataResult.First()["GraduatesOnBenefit"].ToString(), MedianEarnings = oDataResult.First()["MedianEarnings"].ToString(), UpperQuartileEarnings=oDataResult.First()["UpperQuartileEarnings"].ToString(), LowerQuartileEarnings= oDataResult.First()["LowerQuartileEarnings"].ToString(), QualificationCode = oDataResult.First()["QualificationCode"].ToString(), StudentFees = oDataResult.First()["StudentFees"].ToString(), }; return kis; } /// /// Retrieves the default page for the site. /// In this case, a simple page showing a Key Information for Students for the /// qualification specified by the _qualificationCode. /// /// The Home/Index view along with its data model public ActionResult Index() { //Get the appropriate Key Information for Students from the cache var kis = System.Web.HttpRuntime.Cache[_qualificationCode] as KeyInformationData; //If the Key Information for Students was not found in the cache if (kis == null) { try { //Get the Key Information for Students from TEC kis = GetKeyInformationData(); //Save the result into the cache ready for next time //Set the cached value to expire in 24 hours System.Web.HttpRuntime.Cache.Insert(_qualificationCode, kis, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration); } catch { //Catch any errors arising from calling the web service //so they don't affect our view //In a real implementation, we would log the errors here } } //Return the view with the Key Information for Students data to display return View(kis); }