-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented field type replacement & update. (#33)
* Implemented field type replacement. * Controller. * Implemented field type update. * Replace with delta.
- Loading branch information
Showing
30 changed files
with
910 additions
and
15 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
backend/src/Logitar.Cms.Contracts/FieldTypes/ReplaceFieldTypePayload.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Logitar.Cms.Contracts.FieldTypes.Properties; | ||
|
||
namespace Logitar.Cms.Contracts.FieldTypes; | ||
|
||
public record ReplaceFieldTypePayload | ||
{ | ||
public string UniqueName { get; set; } | ||
public string? DisplayName { get; set; } | ||
public string? Description { get; set; } | ||
|
||
public BooleanProperties? BooleanProperties { get; set; } | ||
public DateTimeProperties? DateTimeProperties { get; set; } | ||
public NumberProperties? NumberProperties { get; set; } | ||
public StringProperties? StringProperties { get; set; } | ||
public TextProperties? TextProperties { get; set; } | ||
|
||
public ReplaceFieldTypePayload() : this(string.Empty) | ||
{ | ||
} | ||
|
||
public ReplaceFieldTypePayload(string uniqueName) | ||
{ | ||
UniqueName = uniqueName; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/Logitar.Cms.Contracts/FieldTypes/UpdateFieldTypePayload.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Logitar.Cms.Contracts.FieldTypes.Properties; | ||
|
||
namespace Logitar.Cms.Contracts.FieldTypes; | ||
|
||
public record UpdateFieldTypePayload | ||
{ | ||
public string? UniqueName { get; set; } | ||
public Change<string>? DisplayName { get; set; } | ||
public Change<string>? Description { get; set; } | ||
|
||
public BooleanProperties? BooleanProperties { get; set; } | ||
public DateTimeProperties? DateTimeProperties { get; set; } | ||
public NumberProperties? NumberProperties { get; set; } | ||
public StringProperties? StringProperties { get; set; } | ||
public TextProperties? TextProperties { get; set; } | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/Logitar.Cms.Core/FieldTypes/Commands/ReplaceFieldTypeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Commands; | ||
|
||
public record ReplaceFieldTypeCommand(Guid Id, ReplaceFieldTypePayload Payload, long? Version) : Activity, IRequest<FieldType?>; |
106 changes: 106 additions & 0 deletions
106
backend/src/Logitar.Cms.Core/FieldTypes/Commands/ReplaceFieldTypeCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using FluentValidation; | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using Logitar.Cms.Core.FieldTypes.Properties; | ||
using Logitar.Cms.Core.FieldTypes.Validators; | ||
using Logitar.Identity.Contracts.Settings; | ||
using Logitar.Identity.Domain.Shared; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Commands; | ||
|
||
internal class ReplaceFieldTypeCommandHandler : IRequestHandler<ReplaceFieldTypeCommand, FieldType?> | ||
{ | ||
private readonly IFieldTypeQuerier _fieldTypeQuerier; | ||
private readonly IFieldTypeRepository _fieldTypeRepository; | ||
private readonly ISender _sender; | ||
|
||
public ReplaceFieldTypeCommandHandler(IFieldTypeQuerier fieldTypeQuerier, IFieldTypeRepository fieldTypeRepository, ISender sender) | ||
{ | ||
_fieldTypeQuerier = fieldTypeQuerier; | ||
_fieldTypeRepository = fieldTypeRepository; | ||
_sender = sender; | ||
} | ||
|
||
public async Task<FieldType?> Handle(ReplaceFieldTypeCommand command, CancellationToken cancellationToken) | ||
{ | ||
FieldTypeId id = new(command.Id); | ||
FieldTypeAggregate? fieldType = await _fieldTypeRepository.LoadAsync(id, cancellationToken); | ||
if (fieldType == null) | ||
{ | ||
return null; | ||
} | ||
|
||
IUniqueNameSettings uniqueNameSettings = FieldTypeAggregate.UniqueNameSettings; | ||
|
||
ReplaceFieldTypePayload payload = command.Payload; | ||
new ReplaceFieldTypeValidator(uniqueNameSettings, fieldType.DataType).ValidateAndThrow(payload); | ||
|
||
FieldTypeAggregate? reference = null; | ||
if (command.Version.HasValue) | ||
{ | ||
reference = await _fieldTypeRepository.LoadAsync(id, command.Version.Value, cancellationToken); | ||
} | ||
|
||
UniqueNameUnit uniqueName = new(uniqueNameSettings, payload.UniqueName); | ||
DisplayNameUnit? displayName = DisplayNameUnit.TryCreate(payload.DisplayName); | ||
DescriptionUnit? description = DescriptionUnit.TryCreate(payload.Description); | ||
if (reference == null || uniqueName != reference.UniqueName) | ||
{ | ||
fieldType.UniqueName = uniqueName; | ||
} | ||
if (reference == null || displayName != reference.DisplayName) | ||
{ | ||
fieldType.DisplayName = displayName; | ||
} | ||
if (reference == null || description != reference.Description) | ||
{ | ||
fieldType.Description = description; | ||
} | ||
fieldType.Update(command.ActorId); | ||
|
||
if (payload.BooleanProperties != null) | ||
{ | ||
ReadOnlyBooleanProperties properties = new(payload.BooleanProperties); | ||
if (reference == null || properties != reference.Properties) | ||
{ | ||
fieldType.SetProperties(properties, command.ActorId); | ||
} | ||
} | ||
if (payload.DateTimeProperties != null) | ||
{ | ||
ReadOnlyDateTimeProperties properties = new(payload.DateTimeProperties); | ||
if (reference == null || properties != reference.Properties) | ||
{ | ||
fieldType.SetProperties(properties, command.ActorId); | ||
} | ||
} | ||
if (payload.NumberProperties != null) | ||
{ | ||
ReadOnlyNumberProperties properties = new(payload.NumberProperties); | ||
if (reference == null || properties != reference.Properties) | ||
{ | ||
fieldType.SetProperties(properties, command.ActorId); | ||
} | ||
} | ||
if (payload.StringProperties != null) | ||
{ | ||
ReadOnlyStringProperties properties = new(payload.StringProperties); | ||
if (reference == null || properties != reference.Properties) | ||
{ | ||
fieldType.SetProperties(properties, command.ActorId); | ||
} | ||
} | ||
if (payload.TextProperties != null) | ||
{ | ||
ReadOnlyTextProperties properties = new(payload.TextProperties); | ||
if (reference == null || properties != reference.Properties) | ||
{ | ||
fieldType.SetProperties(properties, command.ActorId); | ||
} | ||
} | ||
|
||
await _sender.Send(new SaveFieldTypeCommand(fieldType), cancellationToken); | ||
|
||
return await _fieldTypeQuerier.ReadAsync(fieldType, cancellationToken); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/Logitar.Cms.Core/FieldTypes/Commands/UpdateFieldTypeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Commands; | ||
|
||
public record UpdateFieldTypeCommand(Guid Id, UpdateFieldTypePayload Payload) : Activity, IRequest<FieldType?>; |
77 changes: 77 additions & 0 deletions
77
backend/src/Logitar.Cms.Core/FieldTypes/Commands/UpdateFieldTypeCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using FluentValidation; | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using Logitar.Cms.Core.FieldTypes.Properties; | ||
using Logitar.Cms.Core.FieldTypes.Validators; | ||
using Logitar.Identity.Contracts.Settings; | ||
using Logitar.Identity.Domain.Shared; | ||
using MediatR; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Commands; | ||
|
||
internal class UpdateFieldTypeCommandHandler : IRequestHandler<UpdateFieldTypeCommand, FieldType?> | ||
{ | ||
private readonly IFieldTypeQuerier _fieldTypeQuerier; | ||
private readonly IFieldTypeRepository _fieldTypeRepository; | ||
private readonly ISender _sender; | ||
|
||
public UpdateFieldTypeCommandHandler(IFieldTypeQuerier fieldTypeQuerier, IFieldTypeRepository fieldTypeRepository, ISender sender) | ||
{ | ||
_fieldTypeQuerier = fieldTypeQuerier; | ||
_fieldTypeRepository = fieldTypeRepository; | ||
_sender = sender; | ||
} | ||
|
||
public async Task<FieldType?> Handle(UpdateFieldTypeCommand command, CancellationToken cancellationToken) | ||
{ | ||
FieldTypeId id = new(command.Id); | ||
FieldTypeAggregate? fieldType = await _fieldTypeRepository.LoadAsync(id, cancellationToken); | ||
if (fieldType == null) | ||
{ | ||
return null; | ||
} | ||
|
||
IUniqueNameSettings uniqueNameSettings = FieldTypeAggregate.UniqueNameSettings; | ||
|
||
UpdateFieldTypePayload payload = command.Payload; | ||
new UpdateFieldTypeValidator(uniqueNameSettings, fieldType.DataType).ValidateAndThrow(payload); | ||
|
||
if (!string.IsNullOrWhiteSpace(payload.UniqueName)) | ||
{ | ||
fieldType.UniqueName = new UniqueNameUnit(uniqueNameSettings, payload.UniqueName); | ||
} | ||
if (payload.DisplayName != null) | ||
{ | ||
fieldType.DisplayName = DisplayNameUnit.TryCreate(payload.DisplayName.Value); | ||
} | ||
if (payload.Description != null) | ||
{ | ||
fieldType.Description = DescriptionUnit.TryCreate(payload.Description.Value); | ||
} | ||
fieldType.Update(command.ActorId); | ||
|
||
if (payload.BooleanProperties != null) | ||
{ | ||
fieldType.SetProperties(new ReadOnlyBooleanProperties(payload.BooleanProperties), command.ActorId); | ||
} | ||
if (payload.DateTimeProperties != null) | ||
{ | ||
fieldType.SetProperties(new ReadOnlyDateTimeProperties(payload.DateTimeProperties), command.ActorId); | ||
} | ||
if (payload.NumberProperties != null) | ||
{ | ||
fieldType.SetProperties(new ReadOnlyNumberProperties(payload.NumberProperties), command.ActorId); | ||
} | ||
if (payload.StringProperties != null) | ||
{ | ||
fieldType.SetProperties(new ReadOnlyStringProperties(payload.StringProperties), command.ActorId); | ||
} | ||
if (payload.TextProperties != null) | ||
{ | ||
fieldType.SetProperties(new ReadOnlyTextProperties(payload.TextProperties), command.ActorId); | ||
} | ||
|
||
await _sender.Send(new SaveFieldTypeCommand(fieldType), cancellationToken); | ||
|
||
return await _fieldTypeQuerier.ReadAsync(fieldType, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
backend/src/Logitar.Cms.Core/FieldTypes/Validators/ReplaceFieldTypeValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using FluentValidation; | ||
using Logitar.Cms.Contracts.FieldTypes; | ||
using Logitar.Identity.Contracts.Settings; | ||
using Logitar.Identity.Domain.Shared; | ||
|
||
namespace Logitar.Cms.Core.FieldTypes.Validators; | ||
|
||
public class ReplaceFieldTypeValidator : AbstractValidator<ReplaceFieldTypePayload> | ||
{ | ||
public ReplaceFieldTypeValidator(IUniqueNameSettings uniqueNameSettings, DataType dataType) | ||
{ | ||
RuleFor(x => x.UniqueName).SetValidator(new UniqueNameValidator(uniqueNameSettings)); | ||
When(x => !string.IsNullOrWhiteSpace(x.DisplayName), () => RuleFor(x => x.DisplayName!).SetValidator(new DisplayNameValidator())); | ||
When(x => !string.IsNullOrWhiteSpace(x.Description), () => RuleFor(x => x.Description!).SetValidator(new DescriptionValidator())); | ||
|
||
switch (dataType) | ||
{ | ||
case DataType.Boolean: | ||
When(x => x.BooleanProperties != null, () => RuleFor(x => x.BooleanProperties!).SetValidator(new BooleanPropertiesValidator())) | ||
.Otherwise(() => RuleFor(x => x.BooleanProperties).NotNull()); | ||
RuleFor(x => x.DateTimeProperties).Null(); | ||
RuleFor(x => x.NumberProperties).Null(); | ||
RuleFor(x => x.StringProperties).Null(); | ||
RuleFor(x => x.TextProperties).Null(); | ||
break; | ||
case DataType.DateTime: | ||
RuleFor(x => x.BooleanProperties).Null(); | ||
When(x => x.DateTimeProperties != null, () => RuleFor(x => x.DateTimeProperties!).SetValidator(new DateTimePropertiesValidator())) | ||
.Otherwise(() => RuleFor(x => x.DateTimeProperties).NotNull()); | ||
RuleFor(x => x.NumberProperties).Null(); | ||
RuleFor(x => x.StringProperties).Null(); | ||
RuleFor(x => x.TextProperties).Null(); | ||
break; | ||
case DataType.Number: | ||
RuleFor(x => x.BooleanProperties).Null(); | ||
RuleFor(x => x.DateTimeProperties).Null(); | ||
When(x => x.NumberProperties != null, () => RuleFor(x => x.NumberProperties!).SetValidator(new NumberPropertiesValidator())) | ||
.Otherwise(() => RuleFor(x => x.NumberProperties).NotNull()); | ||
RuleFor(x => x.StringProperties).Null(); | ||
RuleFor(x => x.TextProperties).Null(); | ||
break; | ||
case DataType.String: | ||
RuleFor(x => x.BooleanProperties).Null(); | ||
RuleFor(x => x.DateTimeProperties).Null(); | ||
RuleFor(x => x.NumberProperties).Null(); | ||
When(x => x.StringProperties != null, () => RuleFor(x => x.StringProperties!).SetValidator(new StringPropertiesValidator())) | ||
.Otherwise(() => RuleFor(x => x.StringProperties).NotNull()); | ||
RuleFor(x => x.TextProperties).Null(); | ||
break; | ||
case DataType.Text: | ||
RuleFor(x => x.BooleanProperties).Null(); | ||
RuleFor(x => x.DateTimeProperties).Null(); | ||
RuleFor(x => x.NumberProperties).Null(); | ||
RuleFor(x => x.StringProperties).Null(); | ||
When(x => x.TextProperties != null, () => RuleFor(x => x.TextProperties!).SetValidator(new TextPropertiesValidator())) | ||
.Otherwise(() => RuleFor(x => x.TextProperties).NotNull()); | ||
break; | ||
default: | ||
throw new DataTypeNotSupportedException(dataType); | ||
} | ||
} | ||
} |
Oops, something went wrong.