Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

82 lines
2.7 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using Squidex.Domain.Apps.Events;
using Squidex.Infrastructure.EventSourcing;
namespace Squidex.Domain.Apps.Entities
{
public static class EntityMapper
{
public static T Update<T>(this T entity, Envelope<IEvent> envelope, Action<SquidexEvent, T> updater = null) where T : IEntity
{
var @event = (SquidexEvent)envelope.Payload;
var headers = envelope.Headers;
SetId(entity, headers);
SetCreated(entity, headers);
SetCreatedBy(entity, @event);
SetLastModified(entity, headers);
SetLastModifiedBy(entity, @event);
SetVersion(entity, headers);
updater?.Invoke(@event, entity);
return entity;
}
private static void SetId(IEntity entity, EnvelopeHeaders headers)
{
if (entity is IUpdateableEntity updateable && updateable.Id == Guid.Empty)
{
updateable.Id = headers.AggregateId();
}
}
private static void SetVersion(IEntity entity, EnvelopeHeaders headers)
{
if (entity is IUpdateableEntityWithVersion updateable)
{
updateable.Version = headers.EventStreamNumber();
}
}
private static void SetCreated(IEntity entity, EnvelopeHeaders headers)
{
if (entity is IUpdateableEntity updateable && updateable.Created == default)
{
updateable.Created = headers.Timestamp();
}
}
private static void SetCreatedBy(IEntity entity, SquidexEvent @event)
{
if (entity is IUpdateableEntityWithCreatedBy withCreatedBy && withCreatedBy.CreatedBy == null)
{
withCreatedBy.CreatedBy = @event.Actor;
}
}
private static void SetLastModified(IEntity entity, EnvelopeHeaders headers)
{
if (entity is IUpdateableEntity updateable)
{
updateable.LastModified = headers.Timestamp();
}
}
private static void SetLastModifiedBy(IEntity entity, SquidexEvent @event)
{
if (entity is IUpdateableEntityWithLastModifiedBy withModifiedBy)
{
withModifiedBy.LastModifiedBy = @event.Actor;
}
}
}
}