private static async Task<object> FindEntity( SubmitContext context, DataModificationEntry entry, CancellationToken cancellationToken) { IQueryable query = context.ApiContext.Source(entry.EntitySetName); query = entry.ApplyTo(query); QueryResult result = await context.ApiContext.QueryAsync(new QueryRequest(query), cancellationToken); object entity = result.Results.SingleOrDefault(); if (entity == null) { // TODO GitHubIssue#38 : Handle the case when entity is resolved // there are 2 cases where the entity is not found: // 1) it doesn't exist // 2) concurrency checks have failed // we should account for both - I can see 3 options: // a. always return "PreConditionFailed" result // - this is the canonical behavior of WebAPI OData, see the following post: // "Getting started with ASP.NET Web API 2.2 for OData v4.0" on http://blogs.msdn.com/b/webdev/. // - this makes sense because if someone deleted the record, then you still have a concurrency error // b. possibly doing a 2nd query with just the keys to see if the record still exists // c. only query with the keys, and then set the DbEntityEntry's OriginalValues to the ETag values, // letting the save fail if there are concurrency errors ////throw new EntityNotFoundException throw new InvalidOperationException(Resources.ResourceNotFound); } return entity; }
public async Task TestEntityFilterReturnsTask() { TestEntityFilterReturnsTaskApi api = new TestEntityFilterReturnsTaskApi(); DataModificationEntry<Customer> createCustomer = new DataModificationEntry<Customer>( "Customers", "Customer", null, null, new Dictionary<string, object>() { {"CustomerID", "NEW01"}, {"CompanyName", "New Cust"}, }); await api.SubmitAsync(new ChangeSet(new ChangeSetEntry[] { createCustomer })); NorthwindContext ctx = new NorthwindContext(); #if EF7 Customer newCustomer = await ctx.Customers.FirstOrDefaultAsync(e => e.CustomerID == "NEW01"); #else Customer newCustomer = await ctx.Customers.FindAsync("NEW01"); #endif // The "OnInserting" should have been appended by the OnInsertingCustomers filter Assert.Equal("New CustOnInserting", newCustomer.CompanyName); ctx.Customers.Remove(newCustomer); await ctx.SaveChangesAsync(); }
public async Task ComplexTypeUpdate() { // Arrange var libraryDomain = new LibraryDomain(); var entry = new DataModificationEntry( "Readers", "Person", new Dictionary<string, object> { { "Id", new Guid("53162782-EA1B-4712-AF26-8AA1D2AC0461") } }, new Dictionary<string, object>(), new Dictionary<string, object> { { "Addr", new Dictionary<string, object> { { "Zip", "332" } } } }); var changeSet = new ChangeSet(new[] { entry }); var sc = new SubmitContext(libraryDomain.Context, changeSet); // Act await ChangeSetPreparer.Instance.PrepareAsync(sc, CancellationToken.None); var person = entry.Entity as Person; // Assert Assert.NotNull(person); Assert.Equal("332", person.Addr.Zip); }
private static string GetAuthorizeFailedMessage(ChangeSetEntry entry) { switch (entry.Type) { case ChangeSetEntryType.DataModification: DataModificationEntry dataModification = (DataModificationEntry)entry; string message = null; if (dataModification.IsNew) { message = Resources.NoPermissionToInsertEntity; } else if (dataModification.IsUpdate) { message = Resources.NoPermissionToUpdateEntity; } else if (dataModification.IsDelete) { message = Resources.NoPermissionToDeleteEntity; } else { throw new NotSupportedException(Resources.DataModificationMustBeCUD); } return(string.Format(CultureInfo.InvariantCulture, message, dataModification.EntitySetName)); case ChangeSetEntryType.ActionInvocation: ActionInvocationEntry actionInvocation = (ActionInvocationEntry)entry; return(string.Format( CultureInfo.InvariantCulture, Resources.NoPermissionToInvokeAction, actionInvocation.ActionName)); default: throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, Resources.InvalidChangeSetEntryType, entry.Type)); } }
private static async Task PerformPersist( SubmitContext context, IEnumerable <ChangeSetEntry> changeSetItems, CancellationToken cancellationToken) { // Once the change is persisted, the EntityState is lost. // In order to invoke the correct post-CUD event, remember which action was performed on the entity. foreach (ChangeSetEntry item in changeSetItems) { if (item.Type == ChangeSetEntryType.DataModification) { DataModificationEntry dataModification = (DataModificationEntry)item; if (dataModification.IsNew) { dataModification.AddAction = AddAction.Inserting; } else if (dataModification.IsUpdate) { dataModification.AddAction = AddAction.Updating; } else if (dataModification.IsDelete) { dataModification.AddAction = AddAction.Removing; } } } var executor = context.GetHookHandler <ISubmitExecutor>(); if (executor == null) { throw new NotSupportedException(Resources.SubmitExecutorMissing); } context.Result = await executor.ExecuteSubmitAsync(context, cancellationToken); }
private static void SetValues(DbEntityEntry dbEntry, DataModificationEntry entry, Type entityType) { if (entry.IsFullReplaceUpdate) { // The algorithm for a "FullReplaceUpdate" is taken from ObjectContextServiceProvider.ResetResource // in WCF DS, and works as follows: // - 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. object newInstance = Activator.CreateInstance(entityType); SetValues(newInstance, entityType, entry.EntityKey); SetValues(newInstance, entityType, entry.LocalValues); dbEntry.CurrentValues.SetValues(newInstance); } else { foreach (KeyValuePair<string, object> propertyPair in entry.LocalValues) { DbPropertyEntry propertyEntry = dbEntry.Property(propertyPair.Key); object value = propertyPair.Value; if (propertyEntry is DbComplexPropertyEntry) { var dic = value as IReadOnlyDictionary<string, object>; if (dic == null) { // TODO GitHubIssue#103 : Choose property error message for unknown type throw new NotSupportedException("Unsupported type for property:" + propertyPair.Key); } var type = propertyEntry.CurrentValue.GetType(); value = Activator.CreateInstance(type); SetValues(value, type, dic); } propertyEntry.CurrentValue = value; } } }
/// <summary> /// Handles a POST request to create an entity. /// </summary> /// <param name="edmEntityObject">The entity object to create.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task object that contains the creation result.</returns> public async Task<IHttpActionResult> Post(EdmEntityObject edmEntityObject, CancellationToken cancellationToken) { if (!this.ModelState.IsValid) { return BadRequest(this.ModelState); } ODataPath path = this.GetPath(); IEdmEntitySet entitySet = path.NavigationSource as IEdmEntitySet; if (entitySet == null) { throw new NotImplementedException(Resources.InsertOnlySupportedOnEntitySet); } DataModificationEntry postEntry = new DataModificationEntry( entitySet.Name, path.EdmType.FullTypeName(), null, null, edmEntityObject.CreatePropertyDictionary()); RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet(); if (changeSetProperty == null) { ChangeSet changeSet = new ChangeSet(); changeSet.Entries.Add(postEntry); SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken); } else { changeSetProperty.ChangeSet.Entries.Add(postEntry); await changeSetProperty.OnChangeSetCompleted(); } return this.CreateCreatedODataResult(postEntry.Entity); }
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); } DataModificationEntry updateEntry = new DataModificationEntry( entitySet.Name, path.EdmType.FullTypeName(), RestierQueryBuilder.GetPathKeyValues(path), this.GetOriginalValues(), edmEntityObject.CreatePropertyDictionary()); updateEntry.IsFullReplaceUpdate = isFullReplaceUpdate; RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet(); if (changeSetProperty == null) { ChangeSet changeSet = new ChangeSet(); changeSet.Entries.Add(updateEntry); SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken); } else { changeSetProperty.ChangeSet.Entries.Add(updateEntry); await changeSetProperty.OnChangeSetCompleted(); } return this.CreateUpdatedODataResult(updateEntry.Entity); }
/// <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); } DataModificationEntry deleteEntry = new DataModificationEntry( entitySet.Name, path.EdmType.FullTypeName(), RestierQueryBuilder.GetPathKeyValues(path), this.GetOriginalValues(), null); RestierChangeSetProperty changeSetProperty = this.Request.GetChangeSet(); if (changeSetProperty == null) { ChangeSet changeSet = new ChangeSet(); changeSet.Entries.Add(deleteEntry); SubmitResult result = await Api.SubmitAsync(changeSet, cancellationToken); } else { changeSetProperty.ChangeSet.Entries.Add(deleteEntry); await changeSetProperty.OnChangeSetCompleted(); } return this.StatusCode(HttpStatusCode.NoContent); }
private static void SetValues(DbEntityEntry dbEntry, DataModificationEntry entry, Type entityType) { if (entry.IsFullReplaceUpdate) { // The algorithm for a "FullReplaceUpdate" is taken from ObjectContextServiceProvider.ResetResource // in WCF DS, and works as follows: // - 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. object newInstance = Activator.CreateInstance(entityType); SetValues(newInstance, entityType, entry.EntityKey); SetValues(newInstance, entityType, entry.LocalValues); dbEntry.CurrentValues.SetValues(newInstance); } else { foreach (KeyValuePair<string, object> propertyPair in entry.LocalValues) { DbPropertyEntry propertyEntry = dbEntry.Property(propertyPair.Key); object value = propertyPair.Value; if (value == null) { // If the property value is null, we set null in the entry too. propertyEntry.CurrentValue = null; continue; } Type type = propertyEntry.CurrentValue.GetType(); if (propertyEntry is DbComplexPropertyEntry) { var dic = value as IReadOnlyDictionary<string, object>; if (dic == null) { throw new NotSupportedException(string.Format( CultureInfo.InvariantCulture, Resources.UnsupportedPropertyType, propertyPair.Key)); } value = Activator.CreateInstance(type); SetValues(value, type, dic); } propertyEntry.CurrentValue = ConvertToEfValue(type, value); } } }