private async Task <IHttpActionResult> Update( EdmEntityObject edmEntityObject, bool isFullReplaceUpdate, CancellationToken cancellationToken) { ODataPath path = this.GetPath(); IEdmEntitySet entitySet = path.NavigationSource as IEdmEntitySet; if (entitySet == null) { throw new NotImplementedException(Resources.UpdateOnlySupportedOnEntitySet); } var propertiesInEtag = await this.GetOriginalValues(entitySet); if (propertiesInEtag == null) { throw new PreconditionRequiredException(Resources.PreconditionRequired); } // In case of type inheritance, the actual type will be different from entity type // This is only needed for put case, and does not for patch case var expectedEntityType = path.EdmType; var actualEntityType = path.EdmType; if (edmEntityObject.ActualEdmType != null) { expectedEntityType = edmEntityObject.ExpectedEdmType; actualEntityType = edmEntityObject.ActualEdmType; } DataModificationItem updateItem = new DataModificationItem( entitySet.Name, expectedEntityType.GetClrType(Api), actualEntityType.GetClrType(Api), DataModificationItemAction.Update, RestierQueryBuilder.GetPathKeyValues(path), propertiesInEtag, edmEntityObject.CreatePropertyDictionary()); updateItem.IsFullReplaceUpdateRequest = isFullReplaceUpdate; RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet(); if (changeSetProperty == null) { ChangeSet changeSet = new ChangeSet(); changeSet.Entries.Add(updateItem); SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken); } else { changeSetProperty.ChangeSet.Entries.Add(updateItem); await changeSetProperty.OnChangeSetCompleted(); } return(this.CreateUpdatedODataResult(updateItem.Entity)); }
#pragma warning restore CA1062 // Validate public arguments /// <summary> /// Handles a DELETE request to delete an entity. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task object that contains the deletion result.</returns> public async Task <IActionResult> Delete(CancellationToken cancellationToken) { EnsureInitialized(); var path = GetPath(); if (!(path.NavigationSource is IEdmEntitySet entitySet)) { throw new NotImplementedException(Resources.DeleteOnlySupportedOnEntitySet); } var propertiesInEtag = GetOriginalValues(entitySet); if (propertiesInEtag == null) { throw new StatusCodeException((HttpStatusCode)428, Resources.PreconditionRequired); } var model = api.GetModel(); var deleteItem = new DataModificationItem( entitySet.Name, path.EdmType.GetClrType(model), null, RestierEntitySetOperation.Delete, RestierQueryBuilder.GetPathKeyValues(path), propertiesInEtag, null); var changeSetProperty = HttpContext.GetChangeSet(); if (changeSetProperty == null) { var changeSet = new ChangeSet(); changeSet.Entries.Add(deleteItem); var result = await api.SubmitAsync(changeSet, cancellationToken).ConfigureAwait(false); } else { changeSetProperty.ChangeSet.Entries.Add(deleteItem); await changeSetProperty.OnChangeSetCompleted().ConfigureAwait(false); } return(StatusCode((int)HttpStatusCode.NoContent)); }
/// <summary> /// Handles a DELETE request to delete an entity. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task object that contains the deletion result.</returns> public async Task <IHttpActionResult> Delete(CancellationToken cancellationToken) { ODataPath path = this.GetPath(); IEdmEntitySet entitySet = path.NavigationSource as IEdmEntitySet; if (entitySet == null) { throw new NotImplementedException(Resources.DeleteOnlySupportedOnEntitySet); } var propertiesInEtag = await this.GetOriginalValues(entitySet); if (propertiesInEtag == null) { throw new PreconditionRequiredException(Resources.PreconditionRequired); } DataModificationItem deleteItem = new DataModificationItem( entitySet.Name, path.EdmType.GetClrType(Api.ServiceProvider), null, DataModificationItemAction.Remove, RestierQueryBuilder.GetPathKeyValues(path), propertiesInEtag, null); RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet(); if (changeSetProperty == null) { ChangeSet changeSet = new ChangeSet(); changeSet.Entries.Add(deleteItem); SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken); } else { changeSetProperty.ChangeSet.Entries.Add(deleteItem); await changeSetProperty.OnChangeSetCompleted(this.Request); } return(this.StatusCode(HttpStatusCode.NoContent)); }
private async Task <IHttpActionResult> Update( EdmEntityObject edmEntityObject, bool isFullReplaceUpdate, CancellationToken cancellationToken) { CheckModelState(); var path = GetPath(); var entitySet = path.NavigationSource as IEdmEntitySet; if (entitySet == null) { throw new NotImplementedException(Resources.UpdateOnlySupportedOnEntitySet); } var propertiesInEtag = await GetOriginalValues(entitySet).ConfigureAwait(false); if (propertiesInEtag == null) { throw new PreconditionRequiredException(Resources.PreconditionRequired); } // In case of type inheritance, the actual type will be different from entity type // This is only needed for put case, and does not need for patch case // For put request, it will create a new, blank instance of the entity. // copy over the key values and set any updated values from the client on the new instance. // Then apply all the properties of the new instance to the instance to be updated. // This will set any unspecified properties to their default value. var expectedEntityType = path.EdmType; var actualEntityType = path.EdmType as IEdmStructuredType; if (edmEntityObject.ActualEdmType != null) { expectedEntityType = edmEntityObject.ExpectedEdmType; actualEntityType = edmEntityObject.ActualEdmType; } var updateItem = new DataModificationItem( entitySet.Name, expectedEntityType.GetClrType(Api.ServiceProvider), actualEntityType.GetClrType(Api.ServiceProvider), RestierEntitySetOperation.Update, RestierQueryBuilder.GetPathKeyValues(path), propertiesInEtag, edmEntityObject.CreatePropertyDictionary(actualEntityType, api, false)) { IsFullReplaceUpdateRequest = isFullReplaceUpdate }; var changeSetProperty = Request.GetChangeSet(); if (changeSetProperty == null) { var changeSet = new ChangeSet(); changeSet.Entries.Add(updateItem); var result = await Api.SubmitAsync(changeSet, cancellationToken).ConfigureAwait(false); } else { changeSetProperty.ChangeSet.Entries.Add(updateItem); await changeSetProperty.OnChangeSetCompleted(Request).ConfigureAwait(false); } return(CreateUpdatedODataResult(updateItem.Resource)); }