public CompoundDocument Transform(object objectGraph, Context context) { Type innerObjectType = TransformationHelper.GetObjectType(objectGraph); if (objectGraph is HttpError) { return TransformationHelper.HandleHttpError(objectGraph as HttpError); } if (objectGraph is Exception) { return TransformationHelper.HandleException(objectGraph as Exception); } TransformationHelper.VerifyTypeSupport(innerObjectType); TransformationHelper.AssureAllMappingsRegistered(innerObjectType, context.Configuration); var result = new CompoundDocument { Meta = TransformationHelper.GetMetadata(objectGraph) }; var resource = TransformationHelper.UnwrapResourceObject(objectGraph); var resourceMapping = context.Configuration.GetMapping(innerObjectType); var resourceList = TransformationHelper.UnifyObjectsToList(resource); var representationList = resourceList.Select(o => TransformationHelper.CreateResourceRepresentation(o, resourceMapping, context)); var primaryResource = TransformationHelper.ChooseProperResourceRepresentation(resource, representationList); result.Data = primaryResource; if (resourceMapping.Relationships.Any()) { result.Included = TransformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context); } if (resourceMapping.PipelineModule != null) { resourceMapping.PipelineModule.Run(result); } return result; }
private static void HandlePostRequests(HttpActionExecutedContext actionExecutedContext, CompoundDocument transformed) { if (actionExecutedContext.Request.Method != HttpMethod.Post) { return; } var primaryResourceHref = transformed.GetPrimaryResourceHref(); if (String.IsNullOrEmpty(primaryResourceHref)) { return; } actionExecutedContext.Response.Headers.Location = new Uri(primaryResourceHref); actionExecutedContext.Response.StatusCode = HttpStatusCode.Created; }
public CompoundDocument HandleException(Exception exception) { var scfException = exception is UtilJsonApiSerializerBaseException ? exception as UtilJsonApiSerializerBaseException : new UtilJsonApiSerializerBaseException(exception.Message, exception); var compoundDocument = new CompoundDocument { Errors = new Dictionary<string, Error> { { "error", new Error { Id = scfException.Id.ToString(), Title = scfException.Message, Status = ((int)scfException.GetHttpStatusCode()).ToString(CultureInfo.InvariantCulture), }} } }; return compoundDocument; }