|
|
|
@ -45,6 +45,35 @@ namespace Squidex.Domain.Apps.Read.MongoDb.Utils |
|
|
|
throw new DomainObjectNotFoundException(headers.AggregateId().ToString(), typeof(T)); |
|
|
|
} |
|
|
|
|
|
|
|
await collection.UpdateAsync(@event, headers, entity, updater); |
|
|
|
} |
|
|
|
|
|
|
|
public static async Task<bool> TryUpdateAsync<T>(this IMongoCollection<T> collection, SquidexEvent @event, EnvelopeHeaders headers, Action<T> updater) where T : class, IMongoEntity, 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; |
|
|
|
} |
|
|
|
|
|
|
|
private static async Task UpdateAsync<T>(this IMongoCollection<T> collection, SquidexEvent @event, EnvelopeHeaders headers, T entity, Action<T> updater) where T : class, IMongoEntity, new() |
|
|
|
{ |
|
|
|
EntityMapper.Update(@event, headers, entity); |
|
|
|
|
|
|
|
updater(entity); |
|
|
|
|