mirror of https://github.com/Squidex/squidex.git
9 changed files with 278 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// AssetCreated.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Events.Assets |
|||
{ |
|||
[TypeName("AssetCreatedEvent")] |
|||
public class AssetCreated : AssetEvent |
|||
{ |
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
// ==========================================================================
|
|||
// AssetDeleted.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Events.Assets |
|||
{ |
|||
[TypeName("AssetDeletedEvent")] |
|||
public sealed class AssetDeleted : AssetEvent |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// AssertEvent.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.CQRS.Events; |
|||
|
|||
namespace Squidex.Events.Assets |
|||
{ |
|||
public abstract class AssetEvent : IEvent |
|||
{ |
|||
public Guid AssetId { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// AssetRenamed.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Events.Assets |
|||
{ |
|||
[TypeName("AssetRenamedEvent")] |
|||
public class AssetRenamed : AssetEvent |
|||
{ |
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,118 @@ |
|||
// ==========================================================================
|
|||
// AssetDomainObject.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Events.Assets; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.CQRS; |
|||
using Squidex.Infrastructure.CQRS.Events; |
|||
using Squidex.Infrastructure.Dispatching; |
|||
using Squidex.Infrastructure.Reflection; |
|||
using Squidex.Write.Assets.Commands; |
|||
|
|||
namespace Squidex.Write.Assets |
|||
{ |
|||
public class AssetDomainObject : DomainObject |
|||
{ |
|||
private bool isDeleted; |
|||
private string name; |
|||
|
|||
public bool IsDeleted |
|||
{ |
|||
get { return isDeleted; } |
|||
} |
|||
|
|||
public string Name |
|||
{ |
|||
get { return name; } |
|||
} |
|||
|
|||
public AssetDomainObject(Guid id, int version) |
|||
: base(id, version) |
|||
{ |
|||
} |
|||
|
|||
protected void On(AssetCreated @event) |
|||
{ |
|||
name = @event.Name; |
|||
} |
|||
|
|||
protected void On(AssetRenamed @event) |
|||
{ |
|||
name = @event.Name; |
|||
} |
|||
|
|||
protected void On(AssetDeleted @event) |
|||
{ |
|||
isDeleted = true; |
|||
} |
|||
|
|||
public AssetDomainObject Create(CreateAsset command) |
|||
{ |
|||
Guard.Valid(command, nameof(command), () => "Cannot create content"); |
|||
|
|||
VerifyNotCreated(); |
|||
|
|||
RaiseEvent(SimpleMapper.Map(command, new AssetCreated())); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
public AssetDomainObject Delete(DeleteAsset command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
VerifyCreatedAndNotDeleted(); |
|||
|
|||
RaiseEvent(SimpleMapper.Map(command, new AssetDeleted())); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
public AssetDomainObject Rename(RenameAsset command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
VerifyCreatedAndNotDeleted(); |
|||
VerifyDifferentNames(command.Name, () => "Cannot rename asset."); |
|||
|
|||
RaiseEvent(SimpleMapper.Map(command, new AssetRenamed())); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
private void VerifyDifferentNames(string newName, Func<string> message) |
|||
{ |
|||
if (string.Equals(name, newName)) |
|||
{ |
|||
throw new ValidationException(message(), new ValidationError("The asset already has this name.", "Name")); |
|||
} |
|||
} |
|||
|
|||
private void VerifyNotCreated() |
|||
{ |
|||
if (!string.IsNullOrWhiteSpace(name)) |
|||
{ |
|||
throw new DomainException("Asset has already been created."); |
|||
} |
|||
} |
|||
|
|||
private void VerifyCreatedAndNotDeleted() |
|||
{ |
|||
if (isDeleted || !string.IsNullOrWhiteSpace(name)) |
|||
{ |
|||
throw new DomainException("Asset has already been deleted or not created yet."); |
|||
} |
|||
} |
|||
|
|||
protected override void DispatchEvent(Envelope<IEvent> @event) |
|||
{ |
|||
this.DispatchAction(@event.Payload); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
// ==========================================================================
|
|||
// AssetAggregateCommand.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.CQRS.Commands; |
|||
|
|||
namespace Squidex.Write.Assets.Commands |
|||
{ |
|||
public abstract class AssetAggregateCommand : AppCommand, IAggregateCommand |
|||
{ |
|||
public Guid AssetId { get; set; } |
|||
|
|||
Guid IAggregateCommand.AggregateId |
|||
{ |
|||
get { return AssetId; } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// ==========================================================================
|
|||
// CreateAsset.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Write.Assets.Commands |
|||
{ |
|||
public sealed class CreateAsset : AssetAggregateCommand, IValidatable |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public void Validate(IList<ValidationError> errors) |
|||
{ |
|||
if (!Name.IsSlug()) |
|||
{ |
|||
errors.Add(new ValidationError("Name must be a valid slug", nameof(Name))); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
// ==========================================================================
|
|||
// DeleteAsset.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Write.Assets.Commands |
|||
{ |
|||
public sealed class DeleteAsset : AssetAggregateCommand |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// ==========================================================================
|
|||
// RenameAsset.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Write.Assets.Commands |
|||
{ |
|||
public sealed class RenameAsset : AssetAggregateCommand, IValidatable |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public void Validate(IList<ValidationError> errors) |
|||
{ |
|||
if (!string.IsNullOrWhiteSpace(Name)) |
|||
{ |
|||
errors.Add(new ValidationError("Name must not be null or empty.", nameof(Name))); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue