/// <summary> /// Creates a new resource with attributes, relationships or both. /// Example: POST /articles HTTP/1.1 /// </summary> public virtual async Task <IActionResult> PostAsync([FromBody] TResource resource, CancellationToken cancellationToken) { _traceWriter.LogMethodStart(new { resource }); ArgumentGuard.NotNull(resource, nameof(resource)); if (_create == null) { throw new RequestMethodNotAllowedException(HttpMethod.Post); } if (!_options.AllowClientGeneratedIds && resource.StringId != null) { throw new ResourceIdInCreateResourceNotAllowedException(); } if (_options.ValidateModelState && !ModelState.IsValid) { throw new InvalidModelStateException(ModelState, typeof(TResource), _options.IncludeExceptionStackTraceInErrors, _options.SerializerNamingStrategy); } var newResource = await _create.CreateAsync(resource, cancellationToken); var resourceId = (newResource ?? resource).StringId; string locationUrl = $"{HttpContext.Request.Path}/{resourceId}"; if (newResource == null) { HttpContext.Response.Headers["Location"] = locationUrl; return(NoContent()); } return(Created(locationUrl, newResource)); }
public virtual async Task <IActionResult> PostAsync([FromBody] T resource) { _logger.LogTrace($"Entering {nameof(PostAsync)}({(resource == null ? "null" : "object")})."); if (_create == null) { throw new RequestMethodNotAllowedException(HttpMethod.Post); } if (resource == null) { throw new InvalidRequestBodyException(null, null, null); } if (!_jsonApiOptions.AllowClientGeneratedIds && !string.IsNullOrEmpty(resource.StringId)) { throw new ResourceIdInPostRequestNotAllowedException(); } if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid) { var namingStrategy = _jsonApiOptions.SerializerContractResolver.NamingStrategy; throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors, namingStrategy); } resource = await _create.CreateAsync(resource); return(Created($"{HttpContext.Request.Path}/{resource.StringId}", resource)); }
public virtual async Task <IActionResult> PostAsync([FromBody] T entity) { if (_create == null) { throw Exceptions.UnSupportedRequestMethod; } if (entity == null) { return(UnprocessableEntity()); } if (!_jsonApiOptions.AllowClientGeneratedIds && !string.IsNullOrEmpty(entity.StringId)) { return(Forbidden()); } if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid) { return(UnprocessableEntity(ModelState.ConvertToErrorCollection <T>())); } entity = await _create.CreateAsync(entity); return(Created($"{HttpContext.Request.Path}/{entity.Id}", entity)); }
/// <inheritdoc /> public virtual async Task <OperationContainer> ProcessAsync(OperationContainer operation, CancellationToken cancellationToken) { ArgumentGuard.NotNull(operation, nameof(operation)); TResource newResource = await _service.CreateAsync((TResource)operation.Resource, cancellationToken); if (operation.Resource.LocalId != null) { string serverId = newResource != null ? newResource.StringId : operation.Resource.StringId; ResourceContext resourceContext = _resourceContextProvider.GetResourceContext <TResource>(); _localIdTracker.Assign(operation.Resource.LocalId, resourceContext.PublicName, serverId); } return(newResource == null ? null : operation.WithResource(newResource)); }
public async Task <IActionResult> Create([FromServices] ICreateService createService, ToDoDto toDoDto) { var toDo = await createService.CreateAsync(toDoDto); return(CreatedAtAction(nameof(Get), new { id = toDo.Id }, toDo)); }