Exemple #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log,
            [Inject] IApprenticeshipService apprenticeshipService)
        {
            string search    = req.Query["search"];
            string fromUKPRN = req.Query["UKPRN"];
            IEnumerable <IStandardsAndFrameworks> standardsAndFrameworks = null;

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

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

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

            try
            {
                standardsAndFrameworks = await apprenticeshipService.StandardsAndFrameworksSearch(search);

                standardsAndFrameworks = apprenticeshipService.CheckForDuplicateApprenticeships(standardsAndFrameworks, UKPRN);
                if (standardsAndFrameworks == null)
                {
                    return(new NotFoundObjectResult(search));
                }

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