public async Task <HttpResponseMessage> Get(string resource) { string respval = null; if (Request.RequestUri.AbsolutePath.ToLower().EndsWith("metadata")) { respval = SerializeResponse(FhirHelper.GenerateCapabilityStatement()); } else { NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query); string query = FhirParmMapper.Instance.GenerateQuery(storage, resource, nvc); var retVal = await storage.QueryFHIRResource(query, resource); Bundle results = new Bundle(); results.Id = Guid.NewGuid().ToString(); results.Type = Bundle.BundleType.Searchset; results.Total = retVal.Count(); results.Link = new System.Collections.Generic.List <Bundle.LinkComponent>(); results.Link.Add(new Bundle.LinkComponent() { Url = Request.RequestUri.AbsoluteUri, Relation = "self" }); results.Entry = new System.Collections.Generic.List <Bundle.EntryComponent>(); foreach (Resource p in retVal) { results.Entry.Add(new Bundle.EntryComponent() { Resource = p }); } if (retVal != null) { respval = SerializeResponse(results); } } var response = this.Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(respval, Encoding.UTF8, CurrentAcceptType); return(response); }
public async Task <HttpResponseMessage> Get(string resource) { try { string respval = null; if (Request.RequestUri.AbsolutePath.ToLower().EndsWith("metadata")) { respval = SerializeResponse(FhirHelper.GenerateCapabilityStatement(Request.RequestUri.AbsoluteUri)); } else { string validResource = FhirHelper.ValidateResourceType(resource); NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query); string _id = nvc["_id"]; string _nextpage = nvc["_nextpage"]; string _count = nvc["_count"]; if (_count == null) { _count = "100"; } string _querytotal = nvc["_querytotal"]; if (_querytotal == null) { _querytotal = "-1"; } IEnumerable <Resource> retVal = null; ResourceQueryResult searchrslt = null; int iqueryTotal = 0; if (string.IsNullOrEmpty(_id)) { string query = FhirParmMapper.Instance.GenerateQuery(storage, validResource, nvc); searchrslt = await storage.QueryFHIRResource(query, validResource, int.Parse(_count), _nextpage, long.Parse(_querytotal)); retVal = searchrslt.Resources; iqueryTotal = (int)searchrslt.Total; } else { retVal = new List <Resource>(); var r = await storage.LoadFHIRResource(_id, validResource); if (r != null) { ((List <Resource>)retVal).Add(r); } iqueryTotal = retVal.Count(); } var baseurl = Request.RequestUri.Scheme + "://" + Request.RequestUri.Authority + "/" + validResource; Bundle results = new Bundle(); results.Id = Guid.NewGuid().ToString(); results.Type = Bundle.BundleType.Searchset; results.Total = iqueryTotal; results.Link = new System.Collections.Generic.List <Bundle.LinkComponent>(); NameValueCollection qscoll = Request.RequestUri.ParseQueryString(); qscoll.Remove("_count"); qscoll.Remove("_querytotal"); qscoll.Add("_querytotal", searchrslt.Total.ToString()); qscoll.Add("_count", _count); results.Link.Add(new Bundle.LinkComponent() { Url = baseurl + "?" + qscoll.ToString(), Relation = "self" }); if (searchrslt.ContinuationToken != null) { qscoll.Remove("_nextpage"); qscoll.Add("_nextpage", searchrslt.ContinuationToken); results.Link.Add(new Bundle.LinkComponent() { Url = baseurl + "?" + qscoll.ToString(), Relation = "next" }); } results.Entry = new System.Collections.Generic.List <Bundle.EntryComponent>(); Bundle.SearchComponent match = new Bundle.SearchComponent(); match.Mode = Bundle.SearchEntryMode.Match; Bundle.SearchComponent include = new Bundle.SearchComponent(); include.Mode = Bundle.SearchEntryMode.Include; foreach (Resource p in retVal) { results.Entry.Add(new Bundle.EntryComponent() { Resource = p, FullUrl = FhirHelper.GetFullURL(Request, p), Search = match }); var includes = await FhirHelper.ProcessIncludes(p, nvc, storage); foreach (Resource r in includes) { results.Entry.Add(new Bundle.EntryComponent() { Resource = r, FullUrl = FhirHelper.GetFullURL(Request, r), Search = include }); } } if (retVal != null) { respval = SerializeResponse(results); } } var response = this.Request.CreateResponse(HttpStatusCode.OK); response.Headers.TryAddWithoutValidation("Accept", CurrentAcceptType); response.Content = new StringContent(respval, Encoding.UTF8); response.Content.Headers.TryAddWithoutValidation("Content-Type", IsAccceptTypeJSON ? FHIRCONTENTTYPEJSON : FHIRCONTENTTYPEXML); return(response); } catch (Exception e) { OperationOutcome oo = new OperationOutcome(); oo.Issue = new System.Collections.Generic.List <OperationOutcome.IssueComponent>(); OperationOutcome.IssueComponent ic = new OperationOutcome.IssueComponent(); ic.Severity = OperationOutcome.IssueSeverity.Error; ic.Code = OperationOutcome.IssueType.Exception; ic.Diagnostics = e.Message; oo.Issue.Add(ic); var response = this.Request.CreateResponse(HttpStatusCode.BadRequest); response.Headers.TryAddWithoutValidation("Accept", CurrentAcceptType); response.Content = new StringContent(SerializeResponse(oo), Encoding.UTF8); response.Content.Headers.TryAddWithoutValidation("Content-Type", IsAccceptTypeJSON ? FHIRCONTENTTYPEJSON : FHIRCONTENTTYPEXML); response.Content.Headers.LastModified = DateTimeOffset.Now; return(response); } }