mirror of https://github.com/Squidex/squidex.git
80 changed files with 427 additions and 507 deletions
@ -1,41 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// EventExtensions.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Globalization; |
|
||||
using Squidex.Infrastructure.CQRS; |
|
||||
|
|
||||
namespace Squidex.Events |
|
||||
{ |
|
||||
public static class EventExtensions |
|
||||
{ |
|
||||
public static Guid AppId(this EnvelopeHeaders headers) |
|
||||
{ |
|
||||
return headers["AppId"].ToGuid(CultureInfo.InvariantCulture); |
|
||||
} |
|
||||
|
|
||||
public static Envelope<T> SetAppId<T>(this Envelope<T> envelope, Guid value) where T : class |
|
||||
{ |
|
||||
envelope.Headers.Set("AppId", value); |
|
||||
|
|
||||
return envelope; |
|
||||
} |
|
||||
|
|
||||
public static Guid SchemaId(this EnvelopeHeaders headers) |
|
||||
{ |
|
||||
return headers["SchemaId"].ToGuid(CultureInfo.InvariantCulture); |
|
||||
} |
|
||||
|
|
||||
public static Envelope<T> SetSchemaId<T>(this Envelope<T> envelope, Guid value) where T : class |
|
||||
{ |
|
||||
envelope.Headers.Set("SchemaId", value); |
|
||||
|
|
||||
return envelope; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,18 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SquidexEvent.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.CQRS.Events; |
||||
|
|
||||
|
namespace Squidex.Events |
||||
|
{ |
||||
|
public abstract class SquidexEvent : IEvent |
||||
|
{ |
||||
|
public RefToken Actor { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,29 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// EnrichWithActorProcessor.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Threading.Tasks; |
|
||||
using Squidex.Infrastructure.CQRS.Commands; |
|
||||
using Squidex.Infrastructure.Tasks; |
|
||||
|
|
||||
namespace Squidex.Infrastructure.CQRS.Events |
|
||||
{ |
|
||||
public sealed class EnrichWithActorProcessor : IEventProcessor |
|
||||
{ |
|
||||
public Task ProcessEventAsync(Envelope<IEvent> @event, IAggregate aggregate, ICommand command) |
|
||||
{ |
|
||||
var actorCommand = command as IActorCommand; |
|
||||
|
|
||||
if (actorCommand != null) |
|
||||
{ |
|
||||
@event.SetActor(actorCommand.Actor); |
|
||||
} |
|
||||
|
|
||||
return TaskHelper.Done; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,53 @@ |
|||||
|
// ==========================================================================
|
||||
|
// NamedLongIdConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using Newtonsoft.Json; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Json |
||||
|
{ |
||||
|
public sealed class NamedLongIdConverter : JsonConverter |
||||
|
{ |
||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
|
{ |
||||
|
var namedId = (NamedId<long>)value; |
||||
|
|
||||
|
writer.WriteValue($"{namedId.Id},{namedId.Name}"); |
||||
|
} |
||||
|
|
||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
|
{ |
||||
|
if (reader.TokenType == JsonToken.Null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var parts = ((string)reader.Value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); |
||||
|
|
||||
|
if (parts.Length < 2) |
||||
|
{ |
||||
|
throw new JsonException("Named id must have more than 2 parts divided by commata"); |
||||
|
} |
||||
|
|
||||
|
long id; |
||||
|
|
||||
|
if (!long.TryParse(parts[0], out id)) |
||||
|
{ |
||||
|
throw new JsonException("Named id must be a valid long"); |
||||
|
} |
||||
|
|
||||
|
return new NamedId<long>(id, string.Join(",", parts.Skip(1))); |
||||
|
} |
||||
|
|
||||
|
public override bool CanConvert(Type objectType) |
||||
|
{ |
||||
|
return objectType == typeof(NamedId<long>); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
// ==========================================================================
|
||||
|
// CollectionExtensions.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using MongoDB.Driver; |
||||
|
using Squidex.Events; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.CQRS; |
||||
|
using Squidex.Infrastructure.MongoDb; |
||||
|
|
||||
|
namespace Squidex.Read.MongoDb.Utils |
||||
|
{ |
||||
|
public static class CollectionExtensions |
||||
|
{ |
||||
|
public static Task CreateAsync<T>(this IMongoCollection<T> collection, SquidexEvent @event, EnvelopeHeaders headers, Action<T> updater) where T : MongoEntity, new() |
||||
|
{ |
||||
|
var entity = EntityMapper.Create<T>(@event, headers); |
||||
|
|
||||
|
updater(entity); |
||||
|
|
||||
|
return collection.InsertOneIfNotExistsAsync(entity); |
||||
|
} |
||||
|
|
||||
|
public static async Task CreateAsync<T>(this IMongoCollection<T> collection, SquidexEvent @event, EnvelopeHeaders headers, Func<T, Task> updater) where T : MongoEntity, new() |
||||
|
{ |
||||
|
var entity = EntityMapper.Create<T>(@event, headers); |
||||
|
|
||||
|
await updater(entity); |
||||
|
|
||||
|
await collection.InsertOneIfNotExistsAsync(entity); |
||||
|
} |
||||
|
|
||||
|
public static async Task UpdateAsync<T>(this IMongoCollection<T> collection, SquidexEvent @event, EnvelopeHeaders headers, Action<T> updater) where T : MongoEntity, new() |
||||
|
{ |
||||
|
var entity = await collection.Find(t => t.Id == headers.AggregateId()).FirstOrDefaultAsync(); |
||||
|
|
||||
|
if (entity == null) |
||||
|
{ |
||||
|
throw new DomainObjectNotFoundException(headers.AggregateId().ToString(), typeof(T)); |
||||
|
} |
||||
|
|
||||
|
EntityMapper.Update(@event, headers, entity); |
||||
|
|
||||
|
updater(entity); |
||||
|
|
||||
|
await collection.ReplaceOneAsync(t => t.Id == entity.Id, entity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ContentCommand.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Squidex.Infrastructure.CQRS.Commands; |
||||
|
|
||||
|
namespace Squidex.Write.Contents.Commands |
||||
|
{ |
||||
|
public abstract class ContentCommand : SchemaCommand, IAggregateCommand |
||||
|
{ |
||||
|
public Guid ContentId { get; set; } |
||||
|
|
||||
|
Guid IAggregateCommand.AggregateId |
||||
|
{ |
||||
|
get { return ContentId; } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,42 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// EnrichWithAppIdProcessor.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Threading.Tasks; |
|
||||
using Squidex.Events; |
|
||||
using Squidex.Infrastructure.CQRS; |
|
||||
using Squidex.Infrastructure.CQRS.Commands; |
|
||||
using Squidex.Infrastructure.CQRS.Events; |
|
||||
using Squidex.Infrastructure.Tasks; |
|
||||
using Squidex.Write.Apps; |
|
||||
|
|
||||
namespace Squidex.Write |
|
||||
{ |
|
||||
public sealed class EnrichWithAppIdProcessor : IEventProcessor |
|
||||
{ |
|
||||
public Task ProcessEventAsync(Envelope<IEvent> @event, IAggregate aggregate, ICommand command) |
|
||||
{ |
|
||||
var appDomainObject = aggregate as AppDomainObject; |
|
||||
|
|
||||
if (appDomainObject != null) |
|
||||
{ |
|
||||
@event.SetAppId(aggregate.Id); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
var appCommand = command as IAppCommand; |
|
||||
|
|
||||
if (appCommand != null) |
|
||||
{ |
|
||||
@event.SetAppId(appCommand.AppId); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return TaskHelper.Done; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,42 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// EnrichWithSchemaIdProcessor.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Threading.Tasks; |
|
||||
using Squidex.Events; |
|
||||
using Squidex.Infrastructure.CQRS; |
|
||||
using Squidex.Infrastructure.CQRS.Commands; |
|
||||
using Squidex.Infrastructure.CQRS.Events; |
|
||||
using Squidex.Infrastructure.Tasks; |
|
||||
using Squidex.Write.Schemas; |
|
||||
|
|
||||
namespace Squidex.Write |
|
||||
{ |
|
||||
public sealed class EnrichWithSchemaIdProcessor : IEventProcessor |
|
||||
{ |
|
||||
public Task ProcessEventAsync(Envelope<IEvent> @event, IAggregate aggregate, ICommand command) |
|
||||
{ |
|
||||
var schemaDomainObject = aggregate as SchemaDomainObject; |
|
||||
|
|
||||
if (schemaDomainObject != null) |
|
||||
{ |
|
||||
@event.SetSchemaId(aggregate.Id); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
var schemaCommand = command as ISchemaCommand; |
|
||||
|
|
||||
if (schemaCommand != null) |
|
||||
{ |
|
||||
@event.SetSchemaId(schemaCommand.SchemaId); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return TaskHelper.Done; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +1,15 @@ |
|||||
// ==========================================================================
|
// ==========================================================================
|
||||
// IActorCommand.cs
|
// FieldCommand.cs
|
||||
// Squidex Headless CMS
|
// Squidex Headless CMS
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
// Copyright (c) Squidex Group
|
// Copyright (c) Squidex Group
|
||||
// All rights reserved.
|
// All rights reserved.
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
|
|
||||
namespace Squidex.Infrastructure.CQRS.Commands |
namespace Squidex.Write.Schemas.Commands |
||||
{ |
{ |
||||
public interface IActorCommand : ICommand |
public class FieldCommand : SchemaAggregateCommand |
||||
{ |
{ |
||||
RefToken Actor { get; set; } |
public long FieldId { get; set; } |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue