private async Task <HttpResponseMessage> Upsert(string resourceType, string headerid = null) { try { var raw = await Request.Content.ReadAsStringAsync().ConfigureAwait(false); BaseFhirParser parser; if (IsContentTypeJson) { parser = jsonparser; } else if (IsContentTypeXml) { parser = xmlparser; } else { throw new Exception("Invalid Content-Type must be application/fhir+json or application/fhir+xml"); } var reader = IsContentTypeJson ? FhirJsonParser.CreateFhirReader(raw) : FhirXmlParser.CreateFhirReader(raw, false); var p = (Resource)parser.Parse(reader, FhirHelper.ResourceTypeFromString(resourceType)); Enum.TryParse(resourceType, out ResourceType rt); if (p.ResourceType != rt) { var oo = new OperationOutcome { Issue = new List <OperationOutcome.IssueComponent>() }; var ic = new OperationOutcome.IssueComponent { Severity = OperationOutcome.IssueSeverity.Error, Code = OperationOutcome.IssueType.Exception, Diagnostics = "Resource provide is not of type " + resourceType }; oo.Issue.Add(ic); var respconf = Request.CreateResponse(HttpStatusCode.BadRequest); respconf.Content = new StringContent(SerializeResponse(oo), Encoding.UTF8); respconf.Content.Headers.LastModified = DateTimeOffset.Now; respconf.Headers.TryAddWithoutValidation("Accept", CurrentAcceptType); respconf.Content.Headers.TryAddWithoutValidation("Content-Type", IsAccceptTypeJson ? Fhircontenttypejson : Fhircontenttypexml); return(respconf); } if (string.IsNullOrEmpty(p.Id) && headerid != null) { p.Id = headerid; } //Store resource regardless of type var dbresp = await ProcessSingleResource(p, resourceType, IsMatchVersionId).ConfigureAwait(false); p = dbresp.Resource; var response = Request.CreateResponse(dbresp.Response == 1 ? HttpStatusCode.Created : HttpStatusCode.OK); response.Content = new StringContent("", Encoding.UTF8); response.Content.Headers.LastModified = p.Meta.LastUpdated; response.Headers.Add("Location", Request.RequestUri.AbsoluteUri + (headerid == null ? "/" + p.Id : "")); response.Headers.Add("ETag", "W/\"" + p.Meta.VersionId + "\""); //Extract and Save each Resource in bundle if it's a batch type if (p.ResourceType != ResourceType.Bundle || ((Bundle)p).Type != Bundle.BundleType.Batch) { return(response); } var source = (Bundle)p; /*Bundle results = new Bundle(); * results.Id = Guid.NewGuid().ToString(); * results.Type = Bundle.BundleType.Searchset; * results.Total = source.Entry.Count(); * results.Link = new System.Collections.Generic.List<Bundle.LinkComponent>(); * results.Link.Add(new Bundle.LinkComponent() { Url = Request.RequestUri.AbsoluteUri, Relation = "original" }); * results.Entry = new System.Collections.Generic.List<Bundle.EntryComponent>();*/ foreach (var ec in source.Entry) { var resourceResponse = await ProcessSingleResource(ec.Resource, Enum.GetName(typeof(ResourceType), ec.Resource.ResourceType)). ConfigureAwait(false); //results.Entry.Add(new Bundle.EntryComponent() { Resource = rslt.Resource, FullUrl = FhirHelper.GetFullURL(Request, rslt.Resource) }); } return(response); } catch (Exception e) { var oo = new OperationOutcome { Issue = new List <OperationOutcome.IssueComponent>() }; var ic = new OperationOutcome.IssueComponent { Severity = OperationOutcome.IssueSeverity.Error, Code = OperationOutcome.IssueType.Exception, Diagnostics = e.Message }; oo.Issue.Add(ic); var response = 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); } }
private async Task <HttpResponseMessage> Upsert(string resourceType, HttpStatusCode statusCode) { try { string raw = await Request.Content.ReadAsStringAsync(); BaseFhirParser parser = null; if (IsContentTypeJSON) { parser = jsonparser; } else if (IsContentTypeXML) { parser = xmlparser; } else { throw new Exception("Invalid Content-Type must be application/fhir+json or application/fhir+xml"); } var reader = IsContentTypeJSON ? FhirJsonParser.CreateFhirReader(raw) : FhirXmlParser.CreateFhirReader(raw, false); var p = (Resource)parser.Parse(reader, FhirHelper.ResourceTypeFromString(resourceType)); ResourceType rt; Enum.TryParse(resourceType, out rt); if (p.ResourceType != rt) { 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 = "Resource provide is not of type " + resourceType; oo.Issue.Add(ic); var respconf = this.Request.CreateResponse(HttpStatusCode.BadRequest); respconf.Content = new StringContent(SerializeResponse(oo), Encoding.UTF8, CurrentAcceptType); return(respconf); } //Store resource regardless of type p = await ProcessSingleResource(p, resourceType, IsMatchVersionId); var response = this.Request.CreateResponse(statusCode); response.Headers.Add("Location", Request.RequestUri.AbsoluteUri + "/" + p.Id); response.Headers.Add("ETag", "W/\"" + p.Meta.VersionId + "\""); //Extract and Save each Resource in bundle if it's a batch type if (p.ResourceType == ResourceType.Bundle && ((Bundle)p).Type == Bundle.BundleType.Batch) { Bundle source = (Bundle)p; Bundle results = new Bundle(); results.Id = Guid.NewGuid().ToString(); results.Type = Bundle.BundleType.Searchset; results.Total = source.Entry.Count(); results.Link = new System.Collections.Generic.List <Bundle.LinkComponent>(); results.Link.Add(new Bundle.LinkComponent() { Url = Request.RequestUri.AbsoluteUri, Relation = "original" }); results.Entry = new System.Collections.Generic.List <Bundle.EntryComponent>(); foreach (Bundle.EntryComponent ec in source.Entry) { var rslt = await ProcessSingleResource(ec.Resource, Enum.GetName(typeof(Hl7.Fhir.Model.ResourceType), ec.Resource.ResourceType)); results.Entry.Add(new Bundle.EntryComponent() { Resource = rslt }); } } 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.Content = new StringContent(SerializeResponse(oo), Encoding.UTF8, CurrentAcceptType); return(response); } }