|
|
@ -10,7 +10,9 @@ using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
using MongoDB.Bson; |
|
|
using MongoDB.Driver; |
|
|
using MongoDB.Driver; |
|
|
|
|
|
using Squidex.Core.Schemas; |
|
|
using Squidex.Events; |
|
|
using Squidex.Events; |
|
|
using Squidex.Events.Contents; |
|
|
using Squidex.Events.Contents; |
|
|
using Squidex.Events.Schemas; |
|
|
using Squidex.Events.Schemas; |
|
|
@ -22,6 +24,7 @@ using Squidex.Infrastructure.Dispatching; |
|
|
using Squidex.Infrastructure.Reflection; |
|
|
using Squidex.Infrastructure.Reflection; |
|
|
using Squidex.Read.Contents; |
|
|
using Squidex.Read.Contents; |
|
|
using Squidex.Read.Contents.Repositories; |
|
|
using Squidex.Read.Contents.Repositories; |
|
|
|
|
|
using Squidex.Read.Schemas.Services; |
|
|
using Squidex.Store.MongoDb.Utils; |
|
|
using Squidex.Store.MongoDb.Utils; |
|
|
|
|
|
|
|
|
namespace Squidex.Store.MongoDb.Contents |
|
|
namespace Squidex.Store.MongoDb.Contents |
|
|
@ -30,6 +33,7 @@ namespace Squidex.Store.MongoDb.Contents |
|
|
{ |
|
|
{ |
|
|
private const string Prefix = "Projections_Content_"; |
|
|
private const string Prefix = "Projections_Content_"; |
|
|
private readonly IMongoDatabase database; |
|
|
private readonly IMongoDatabase database; |
|
|
|
|
|
private readonly ISchemaProvider schemaProvider; |
|
|
|
|
|
|
|
|
protected ProjectionDefinitionBuilder<MongoContentEntity> Projection |
|
|
protected ProjectionDefinitionBuilder<MongoContentEntity> Projection |
|
|
{ |
|
|
{ |
|
|
@ -71,11 +75,14 @@ namespace Squidex.Store.MongoDb.Contents |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public MongoContentRepository(IMongoDatabase database) |
|
|
public MongoContentRepository(IMongoDatabase database, ISchemaProvider schemaProvider) |
|
|
{ |
|
|
{ |
|
|
Guard.NotNull(database, nameof(database)); |
|
|
Guard.NotNull(database, nameof(database)); |
|
|
|
|
|
Guard.NotNull(schemaProvider, nameof(schemaProvider)); |
|
|
|
|
|
|
|
|
this.database = database; |
|
|
this.database = database; |
|
|
|
|
|
|
|
|
|
|
|
this.schemaProvider = schemaProvider; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task ClearAsync() |
|
|
public async Task ClearAsync() |
|
|
@ -113,8 +120,19 @@ namespace Squidex.Store.MongoDb.Contents |
|
|
|
|
|
|
|
|
cursor.SortByDescending(x => x.LastModified); |
|
|
cursor.SortByDescending(x => x.LastModified); |
|
|
|
|
|
|
|
|
var entities = |
|
|
var schemaEntity = await schemaProvider.FindSchemaByIdAsync(schemaId); |
|
|
await cursor.ToListAsync(); |
|
|
|
|
|
|
|
|
if (schemaEntity == null) |
|
|
|
|
|
{ |
|
|
|
|
|
return new List<IContentEntity>(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var entities = await cursor.ToListAsync(); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var entity in entities) |
|
|
|
|
|
{ |
|
|
|
|
|
entity.ParseData(schemaEntity.Schema); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return entities.OfType<IContentEntity>().ToList(); |
|
|
return entities.OfType<IContentEntity>().ToList(); |
|
|
} |
|
|
} |
|
|
@ -154,64 +172,89 @@ namespace Squidex.Store.MongoDb.Contents |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(schemaId); |
|
|
var collection = GetCollection(schemaId); |
|
|
|
|
|
|
|
|
var entity = |
|
|
var entity = await collection.Find(x => x.Id == id).FirstOrDefaultAsync(); |
|
|
await collection.Find(x => x.Id == id).FirstOrDefaultAsync(); |
|
|
|
|
|
|
|
|
if (entity == null) |
|
|
|
|
|
{ |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var schemaEntity = await schemaProvider.FindSchemaByIdAsync(schemaId); |
|
|
|
|
|
|
|
|
|
|
|
if (schemaEntity == null) |
|
|
|
|
|
{ |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
entity.ParseData(schemaEntity.Schema); |
|
|
|
|
|
|
|
|
return entity; |
|
|
return entity; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(ContentCreated @event, EnvelopeHeaders headers) |
|
|
protected Task On(ContentCreated @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
return ForSchemaAsync(headers, (collection, schema) => |
|
|
|
|
|
|
|
|
return collection.CreateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
{ |
|
|
SimpleMapper.Map(@event, x); |
|
|
return collection.CreateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
|
|
|
SimpleMapper.Map(@event, x); |
|
|
|
|
|
|
|
|
x.SetData(@event.Data); |
|
|
x.SetData(schema, @event.Data); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(ContentUpdated @event, EnvelopeHeaders headers) |
|
|
protected Task On(ContentUpdated @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
return ForSchemaAsync(headers, (collection, schema) => |
|
|
|
|
|
|
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
{ |
|
|
x.SetData(@event.Data); |
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
|
|
|
x.SetData(schema, @event.Data); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(ContentPublished @event, EnvelopeHeaders headers) |
|
|
protected Task On(ContentPublished @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
return ForSchemaAsync(headers, collection => |
|
|
|
|
|
|
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
{ |
|
|
x.IsPublished = true; |
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
|
|
|
x.IsPublished = true; |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(ContentUnpublished @event, EnvelopeHeaders headers) |
|
|
protected Task On(ContentUnpublished @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
return ForSchemaAsync(headers, collection => |
|
|
|
|
|
|
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
{ |
|
|
x.IsPublished = false; |
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
|
|
|
x.IsPublished = false; |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(ContentDeleted @event, EnvelopeHeaders headers) |
|
|
protected Task On(ContentDeleted @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
return ForSchemaAsync(headers, collection => |
|
|
|
|
|
|
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
{ |
|
|
x.IsDeleted = true; |
|
|
return collection.UpdateAsync(headers, x => |
|
|
|
|
|
{ |
|
|
|
|
|
x.IsDeleted = true; |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected Task On(FieldDeleted @event, EnvelopeHeaders headers) |
|
|
|
|
|
{ |
|
|
|
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
|
|
|
|
|
|
|
|
|
return collection.UpdateManyAsync(new BsonDocument(), Update.Unset(new StringFieldDefinition<MongoContentEntity>($"Data.{@event.FieldId}"))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
protected Task On(SchemaCreated @event, EnvelopeHeaders headers) |
|
|
protected Task On(SchemaCreated @event, EnvelopeHeaders headers) |
|
|
{ |
|
|
{ |
|
|
var collection = GetCollection(headers.AggregateId()); |
|
|
var collection = GetCollection(headers.AggregateId()); |
|
|
@ -224,6 +267,27 @@ namespace Squidex.Store.MongoDb.Contents |
|
|
return this.DispatchActionAsync(@event.Payload, @event.Headers); |
|
|
return this.DispatchActionAsync(@event.Payload, @event.Headers); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task ForSchemaAsync(EnvelopeHeaders headers, Func<IMongoCollection<MongoContentEntity>, Schema, Task> action) |
|
|
|
|
|
{ |
|
|
|
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
|
|
|
|
|
|
|
|
|
var schemaEntity = await schemaProvider.FindSchemaByIdAsync(headers.SchemaId()); |
|
|
|
|
|
|
|
|
|
|
|
if (schemaEntity == null) |
|
|
|
|
|
{ |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await action(collection, schemaEntity.Schema); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task ForSchemaAsync(EnvelopeHeaders headers, Func<IMongoCollection<MongoContentEntity>, Task> action) |
|
|
|
|
|
{ |
|
|
|
|
|
var collection = GetCollection(headers.SchemaId()); |
|
|
|
|
|
|
|
|
|
|
|
await action(collection); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private IMongoCollection<MongoContentEntity> GetCollection(Guid schemaId) |
|
|
private IMongoCollection<MongoContentEntity> GetCollection(Guid schemaId) |
|
|
{ |
|
|
{ |
|
|
var name = $"{Prefix}{schemaId}"; |
|
|
var name = $"{Prefix}{schemaId}"; |
|
|
|