private async Task <ResponseBase> InitializeUpdate(UpdateRangeCommand command, KeyValuePair <Type, string> property, HttpInformation context, SapphireDbContext db) { bool updateRejected; List <ValidatedResponseBase> updateResults; do { updateRejected = false; updateResults = command.Entries.Select <UpdateEntry, ValidatedResponseBase>((updateEntry) => { object[] primaryKeys = property.Key.GetPrimaryKeyValuesFromJson(db, updateEntry.Value); object dbValue = db.Find(property.Key, primaryKeys); if (dbValue == null) { if (property.Key.JsonContainsData(db, updateEntry.Value)) { updateEntry.Value.Merge(updateEntry.UpdatedProperties); object completeValue = updateEntry.Value.ToObject(property.Key); return(CreateRangeCommandHandler.SetPropertiesAndValidate <UpdateEventAttribute>(db, property, completeValue, context, serviceProvider)); } throw new ValueNotFoundException(command.ContextName, command.CollectionName, primaryKeys); } if (!property.Key.CanUpdate(context, dbValue, serviceProvider)) { throw new UnauthorizedException("The user is not authorized for this action."); } return(ApplyChangesToDb(command, property, dbValue, updateEntry.Value, updateEntry.UpdatedProperties, db, context)); }).ToList(); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { foreach (EntityEntry entityEntry in db.ChangeTracker.Entries()) { await entityEntry.ReloadAsync(); } updateRejected = true; } } while (updateRejected); foreach (ValidatedResponseBase response in updateResults) { if (response.Value != null) { property.Key.ExecuteHookMethods <UpdateEventAttribute>(ModelStoreEventAttributeBase.EventType.After, response.Value, null, context, serviceProvider); } } return(new UpdateRangeResponse { ReferenceId = command.ReferenceId, Results = updateResults }); }
private async Task <ResponseBase> InitializeUpdate(UpdateRangeCommand command, KeyValuePair <Type, string> property, HttpInformation context, SapphireDbContext db) { List <object> updateValues = command.Entries.Select(e => e.Value) .Select(newValue => newValue.ToObject(property.Key)) .ToList(); UpdateRangeResponse response; bool updateRejected; do { updateRejected = false; response = new UpdateRangeResponse { ReferenceId = command.ReferenceId, Results = updateValues.Select((updateValue, index) => { if (!property.Key.CanUpdate(context, updateValue, serviceProvider)) { return((UpdateResponse)command.CreateExceptionResponse <UpdateResponse>( "The user is not authorized for this action.")); } object[] primaryKeys = property.Key.GetPrimaryKeyValues(db, updateValue); object value = db.Find(property.Key, primaryKeys); if (value != null) { object previousValue = command.Entries[index].Previous?.ToObject(property.Key); return(ApplyChangesToDb(property, value, updateValue, previousValue, db, context)); } return((ValidatedResponseBase)CreateRangeCommandHandler .SetPropertiesAndValidate <UpdateEventAttribute>(db, property, updateValue, context, serviceProvider)); }).ToList() }; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { foreach (EntityEntry entityEntry in db.ChangeTracker.Entries()) { await entityEntry.ReloadAsync(); } updateRejected = true; } } while (updateRejected); foreach (object value in updateValues) { property.Key.ExecuteHookMethods <UpdateEventAttribute>(ModelStoreEventAttributeBase.EventType.After, value, context, serviceProvider); } return(response); }