public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
                                                     ILogger log,
                                                     [Inject] IApprenticeshipService apprenticeshipService)
        {
            List <Apprenticeship> persisted = null;


            try
            {
                persisted = (List <Apprenticeship>) await apprenticeshipService.GetUpdatedApprenticeships();

                if (persisted == null)
                {
                    return(new EmptyResult());
                }
                var listOfProviderUKPRN = persisted.Select(x => x.ProviderUKPRN.ToString())
                                          .Distinct()
                                          .ToList();
                List <Apprenticeship> totalList = new List <Apprenticeship>();
                foreach (var ukprn in listOfProviderUKPRN)
                {
                    var results = apprenticeshipService.GetApprenticeshipByUKPRN(int.Parse(ukprn)).Result;
                    if (results.Any())
                    {
                        totalList.AddRange((List <Apprenticeship>)results);
                    }
                }
                var providers = apprenticeshipService.ApprenticeshipsToTribalProviders(totalList);
                return(new OkObjectResult(providers));
            }
            catch (Exception e)
            {
                return(new InternalServerErrorObjectResult(e));
            }
        }
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
                                                     ILogger log,
                                                     [Inject] IApprenticeshipService apprenticeshipService)
        {
            string fromQuery = req.Query["UKPRN"];
            List <Apprenticeship> persisted = null;

            if (string.IsNullOrWhiteSpace(fromQuery))
            {
                return(new BadRequestObjectResult($"Empty or missing UKPRN value."));
            }

            if (!int.TryParse(fromQuery, out int UKPRN))
            {
                return(new BadRequestObjectResult($"Invalid UKPRN value, expected a valid integer"));
            }

            try
            {
                persisted = (List <Apprenticeship>) await apprenticeshipService.GetUpdatedApprenticeships();

                if (persisted == null)
                {
                    return(new NotFoundObjectResult(UKPRN));
                }

                var providers = apprenticeshipService.ApprenticeshipsToTribalProviders(persisted.Where(x => x.ProviderUKPRN == UKPRN).ToList());
                return(new OkObjectResult(providers));
            }
            catch (Exception e)
            {
                return(new InternalServerErrorObjectResult(e));
            }
        }
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
                                                     ILogger log,
                                                     [Inject] IApprenticeshipService apprenticeshipService)
        {
            List <Apprenticeship> persisted = null;

            try
            {
                persisted = (List <Apprenticeship>) await apprenticeshipService.GetUpdatedApprenticeships();

                if (persisted == null)
                {
                    return(new EmptyResult());
                }

                return(new OkObjectResult(persisted));
            }
            catch (Exception e)
            {
                return(new InternalServerErrorObjectResult(e));
            }
        }