public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log, [Inject] IApprenticeshipService apprenticeshipService) { string fromQueryId = req.Query["id"]; string fromQueryType = req.Query["type"]; StandardsAndFrameworks persisted = null; if (string.IsNullOrWhiteSpace(fromQueryId)) { return(new BadRequestObjectResult($"Empty or missing id value.")); } if (!int.TryParse(fromQueryType, out int apprenticeshipType)) { return(new BadRequestObjectResult($"Invalid Type value, expected a valid integer")); } if (!Guid.TryParse(fromQueryId, out Guid id)) { return(new BadRequestObjectResult($"Invalid id value. Expected a non-empty valid {nameof(Guid)}")); } try { persisted = (StandardsAndFrameworks)await apprenticeshipService.GetStandardsAndFrameworksById(id, apprenticeshipType); if (persisted == null) { return(new NotFoundObjectResult(id)); } return(new OkObjectResult(persisted)); } catch (Exception e) { return(new InternalServerErrorObjectResult(e)); } }
public async Task <IStandardsAndFrameworks> GetStandardsAndFrameworksById(Guid id, int type) { if (id == Guid.Empty) { throw new ArgumentException($"Cannot be an empty {nameof(Guid)}", nameof(id)); } StandardsAndFrameworks persisted = null; var client = _cosmosDbHelper.GetClient(); await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client); await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client, _settings.StandardsCollectionId); await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client, _settings.FrameworksCollectionId); Document doc = null; switch ((ApprenticeshipType)type) { case ApprenticeshipType.StandardCode: { doc = _cosmosDbHelper.GetDocumentById(client, _settings.StandardsCollectionId, id); persisted = _cosmosDbHelper.DocumentTo <StandardsAndFrameworks>(doc); break; } case ApprenticeshipType.FrameworkCode: { doc = _cosmosDbHelper.GetDocumentById(client, _settings.FrameworksCollectionId, id); List <StandardsAndFrameworks> docs = new List <StandardsAndFrameworks>(); docs.Add(_cosmosDbHelper.DocumentTo <StandardsAndFrameworks>(doc)); docs = _cosmosDbHelper.GetProgTypesForFramework(client, _settings.ProgTypesCollectionId, docs); persisted = docs[0]; break; } } return(persisted); }