public static async Task UpdateAsync <T>(this IMongoCollection <T> collection, SquidexEvent @event, EnvelopeHeaders headers, Action <T> updater) where T : class, IEntity, new() { var entity = await collection.Find(t => t.Id == headers.AggregateId()) .FirstOrDefaultAsync(); if (entity == null) { throw new DomainObjectNotFoundException(headers.AggregateId().ToString(), typeof(T)); } await collection.UpdateAsync(@event, headers, entity, updater); }
private static void SetId(IEntity entity, EnvelopeHeaders headers) { if (entity is IUpdateableEntity updateable && updateable.Id == Guid.Empty) { updateable.Id = headers.AggregateId(); } }
protected Task On(ContentDeleted @event, EnvelopeHeaders headers) { return(ForSchemaIdAsync(@event.SchemaId.Id, collection => { return collection.DeleteOneAsync(x => x.Id == headers.AggregateId()); })); }
public static async Task <bool> TryUpdateAsync <T>(this IMongoCollection <T> collection, SquidexEvent @event, EnvelopeHeaders headers, Action <T> updater) where T : class, IEntity, new() { var entity = await collection.Find(t => t.Id == headers.AggregateId()) .FirstOrDefaultAsync(); if (entity != null) { if (entity is IEntityWithVersion withVersion) { var eventVersion = headers.EventStreamNumber(); if (eventVersion <= withVersion.Version) { return(false); } } await collection.UpdateAsync(@event, headers, entity, updater); return(true); } return(false); }
protected Task On(ContentDeleted @event, EnvelopeHeaders headers) { return(ForAppIdAsync(@event.AppId.Id, async collection => { await collection.UpdateManyAsync( Filter.And( Filter.AnyEq(x => x.ReferencedIds, @event.ContentId), Filter.AnyNe(x => x.ReferencedIdsDeleted, @event.ContentId)), Update.AddToSet(x => x.ReferencedIdsDeleted, @event.ContentId)); await collection.DeleteOneAsync(x => x.Id == headers.AggregateId()); })); }
private static void SetId(EnvelopeHeaders headers, IMongoEntity entity) { entity.Id = headers.AggregateId(); }