Skip to content

Commit

Permalink
Merge pull request #124 from Tech-Fabric/feature/2027-machineName-in-…
Browse files Browse the repository at this point in the history
…categorypath

Feature/2027 machine name in categorypath
  • Loading branch information
ustims authored Sep 6, 2023
2 parents 7bbff18 + 437d2ac commit a655a0c
Show file tree
Hide file tree
Showing 41 changed files with 2,315 additions and 1,724 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ "main" ]

env:
PACKAGE_VERSION: 1.2.13
PACKAGE_VERSION: 1.3.0

jobs:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions CloudFabric.EAV.Domain/CloudFabric.EAV.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CloudFabric.EventSourcing.Domain" Version="0.1.2" />
<PackageReference Include="CloudFabric.Projections" Version="0.1.2" />
<PackageReference Include="CloudFabric.EventSourcing.Domain" Version="0.2.0" />
<PackageReference Include="CloudFabric.Projections" Version="0.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
Expand Down
30 changes: 30 additions & 0 deletions CloudFabric.EAV.Domain/Events/Instance/CategoryCreated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CloudFabric.EventSourcing.EventStore;

namespace CloudFabric.EAV.Domain.Models;

public record CategoryCreated : Event
{
public CategoryCreated()
{

}

public CategoryCreated(Guid id,
string machineName,
Guid entityConfigurationId,
List<AttributeInstance> attributes,
Guid? tenantId)
{
TenantId = tenantId;
Attributes = attributes;
EntityConfigurationId = entityConfigurationId;
AggregateId = id;
MachineName = machineName;
}

public Guid EntityConfigurationId { get; set; }
public List<AttributeInstance> Attributes { get; set; }
public Guid? TenantId { get; set; }
public string MachineName { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ public EntityCategoryPathChanged()
{
}

public EntityCategoryPathChanged(Guid id, Guid entityConfigurationId, Guid categoryTreeId, string categoryPath)
public EntityCategoryPathChanged(Guid id,
Guid entityConfigurationId,
Guid categoryTreeId,
string categoryPath,
Guid? parentId)
{
AggregateId = id;
EntityConfigurationId = entityConfigurationId;
CategoryPath = categoryPath;
CategoryTreeId = categoryTreeId;
ParentId = parentId;
ParentMachineName = string.IsNullOrEmpty(categoryPath) ? "" : categoryPath.Split('/').Last(x => !string.IsNullOrEmpty(x));
}

public string CategoryPath { get; set; }
public Guid EntityConfigurationId { get; set; }

public Guid CategoryTreeId { get; set; }
public Guid? ParentId { get; set; }
public string ParentMachineName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.RegularExpressions;

using CloudFabric.EAV.Domain.Utilities.Extensions;

namespace CloudFabric.EAV.Domain.Models.Attributes;

public class ValueFromListOptionConfiguration
Expand All @@ -11,9 +13,7 @@ public ValueFromListOptionConfiguration(string name, string? machineName)

if (string.IsNullOrEmpty(machineName))
{
machineName = name.Replace(" ", "_");
var specSymbolsRegex = new Regex("[^\\d\\w_]*", RegexOptions.None, TimeSpan.FromMilliseconds(100));
machineName = specSymbolsRegex.Replace(machineName, "").ToLower();
machineName = name.SanitizeForMachineName();
}

MachineName = machineName;
Expand Down
26 changes: 22 additions & 4 deletions CloudFabric.EAV.Domain/Models/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ namespace CloudFabric.EAV.Domain.Models;

public class Category : EntityInstanceBase
{
public string MachineName { get; set; }
public Category(IEnumerable<IEvent> events) : base(events)
{
}

public Category(Guid id, Guid entityConfigurationId, List<AttributeInstance> attributes, Guid? tenantId)
: base(id, entityConfigurationId, attributes, tenantId)
public Category(Guid id,
string machineName,
Guid entityConfigurationId,
List<AttributeInstance> attributes,
Guid? tenantId)
{
Apply(new CategoryCreated(id, machineName, entityConfigurationId, attributes, tenantId));

}

public Category(
Guid id,
string machineName,
Guid entityConfigurationId,
List<AttributeInstance> attributes,
Guid? tenantId,
string categoryPath,
Guid? parentId,
Guid categoryTreeId
) : base(id, entityConfigurationId, attributes, tenantId)
) : this(id, machineName, entityConfigurationId, attributes, tenantId)
{
Apply(new EntityCategoryPathChanged(id, EntityConfigurationId, categoryTreeId, categoryPath, parentId));
}

public void On(CategoryCreated @event)
{
Apply(new EntityCategoryPathChanged(id, EntityConfigurationId, categoryTreeId, categoryPath));
Id = @event.AggregateId;
EntityConfigurationId = @event.EntityConfigurationId;
Attributes = new List<AttributeInstance>(@event.Attributes).AsReadOnly();
TenantId = @event.TenantId;
CategoryPaths = new List<CategoryPath>();
MachineName = @event.MachineName;
}
}
2 changes: 2 additions & 0 deletions CloudFabric.EAV.Domain/Models/CategoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public class CategoryPath
{
public Guid TreeId { get; set; }
public string Path { get; set; }
public Guid? ParentId { get; set; }
public string ParentMachineName { get; set; }
}
18 changes: 14 additions & 4 deletions CloudFabric.EAV.Domain/Models/EntityInstanceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CloudFabric.EAV.Domain.Models;

public class EntityInstanceBase : AggregateBase
{
public EntityInstanceBase(IEnumerable<IEvent> events) : base(events)
public EntityInstanceBase(IEnumerable<IEvent> events) : base(events)
{
}

Expand All @@ -19,6 +19,10 @@ public EntityInstanceBase(Guid id, Guid entityConfigurationId, List<AttributeIns
Apply(new EntityInstanceCreated(id, entityConfigurationId, attributes, tenantId));
}

protected EntityInstanceBase()
{
}

public override string PartitionKey => EntityConfigurationId.ToString();
public List<CategoryPath> CategoryPaths { get; protected set; }

Expand Down Expand Up @@ -54,21 +58,27 @@ public void On(EntityInstanceCreated @event)
CategoryPaths = new List<CategoryPath>();
}

public void ChangeCategoryPath(Guid treeId, string categoryPath)
public void ChangeCategoryPath(Guid treeId, string categoryPath, Guid parentId)
{
Apply(new EntityCategoryPathChanged(Id, EntityConfigurationId, treeId, categoryPath));
Apply(new EntityCategoryPathChanged(Id, EntityConfigurationId, treeId, categoryPath, parentId));
}

public void On(EntityCategoryPathChanged @event)
{
CategoryPath? categoryPath = CategoryPaths.FirstOrDefault(x => x.TreeId == @event.CategoryTreeId);
if (categoryPath == null)
{
CategoryPaths.Add(new CategoryPath { TreeId = @event.CategoryTreeId, Path = @event.CategoryPath });
CategoryPaths.Add(new CategoryPath { TreeId = @event.CategoryTreeId,
Path = @event.CategoryPath,
ParentId = @event.ParentId,
ParentMachineName = @event.ParentMachineName
});
}
else
{
categoryPath.Path = @event.CategoryPath;
categoryPath.ParentMachineName = @event.ParentMachineName;
categoryPath.ParentId = @event.ParentId;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class AttributeConfigurationProjectionBuilder : ProjectionBuilder<Attribu
IHandleEvent<EntityConfigurationAttributeRemoved>
{
public AttributeConfigurationProjectionBuilder(
ProjectionRepositoryFactory projectionRepositoryFactory, AggregateRepositoryFactory _
) : base(projectionRepositoryFactory)
ProjectionRepositoryFactory projectionRepositoryFactory,
ProjectionOperationIndexSelector indexSelector
) : base(projectionRepositoryFactory, indexSelector)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class EntityConfigurationProjectionBuilder : ProjectionBuilder<EntityConf
IHandleEvent<AggregateUpdatedEvent<EntityConfiguration>>
{
public EntityConfigurationProjectionBuilder(
ProjectionRepositoryFactory projectionRepositoryFactory, AggregateRepositoryFactory _
) : base(projectionRepositoryFactory)
ProjectionRepositoryFactory projectionRepositoryFactory,
ProjectionOperationIndexSelector indexSelector
) : base(projectionRepositoryFactory, indexSelector)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace CloudFabric.EAV.Domain.Projections.EntityInstanceProjection;
/// </summary>
public class EntityInstanceProjectionBuilder : ProjectionBuilder,
IHandleEvent<EntityInstanceCreated>,
// IHandleEvent<AttributeInstanceAdded>,
IHandleEvent<CategoryCreated>,
// IHandleEvent<AttributeInstanceAdded>,
IHandleEvent<AttributeInstanceUpdated>,
// IHandleEvent<AttributeInstanceRemoved>,
IHandleEvent<EntityCategoryPathChanged>,
Expand All @@ -41,8 +42,9 @@ public class EntityInstanceProjectionBuilder : ProjectionBuilder,

public EntityInstanceProjectionBuilder(
ProjectionRepositoryFactory projectionRepositoryFactory,
AggregateRepositoryFactory aggregateRepositoryFactory
) : base(projectionRepositoryFactory)
AggregateRepositoryFactory aggregateRepositoryFactory,
ProjectionOperationIndexSelector indexSelector
) : base(projectionRepositoryFactory, indexSelector)
{
_aggregateRepositoryFactory = aggregateRepositoryFactory;
}
Expand Down Expand Up @@ -134,14 +136,20 @@ await UpdateDocument(
List<CategoryPath> categoryPaths =
categoryPathsObj as List<CategoryPath> ?? new List<CategoryPath>();
CategoryPath? categoryPath = categoryPaths.FirstOrDefault(x => x.TreeId == @event.CategoryTreeId);
if (categoryPath == null)
{
categoryPaths.Add(new CategoryPath { Path = @event.CategoryPath, TreeId = @event.CategoryTreeId }
);
categoryPaths.Add(new CategoryPath { TreeId = @event.CategoryTreeId,
Path = @event.CategoryPath,
ParentId = @event.ParentId,
ParentMachineName = @event.ParentMachineName
});
}
else
{
categoryPath.Path = @event.CategoryPath;
categoryPath.ParentMachineName = @event.ParentMachineName;
categoryPath.ParentId = @event.ParentId;
}
document["CategoryPaths"] = categoryPaths;
Expand Down Expand Up @@ -177,6 +185,35 @@ await UpsertDocument(
);
}

public async Task On(CategoryCreated @event)
{
ProjectionDocumentSchema projectionDocumentSchema =
await BuildProjectionDocumentSchemaForEntityConfigurationIdAsync(
@event.EntityConfigurationId
).ConfigureAwait(false);

var document = new Dictionary<string, object?>
{
{ "Id", @event.AggregateId },
{ "EntityConfigurationId", @event.EntityConfigurationId },
{ "TenantId", @event.TenantId },
{ "CategoryPaths", new List<CategoryPath>() },
{ "MachineName", @event.MachineName},
};

foreach (AttributeInstance attribute in @event.Attributes)
{
document.Add(attribute.ConfigurationAttributeMachineName, attribute.GetValue());
}

await UpsertDocument(
projectionDocumentSchema,
document,
@event.PartitionKey,
@event.Timestamp
);
}

private async Task<ProjectionDocumentSchema> BuildProjectionDocumentSchemaForEntityConfigurationIdAsync(
Guid entityConfigurationId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public static ProjectionDocumentPropertySchema GetArrayAttributeSchema(
EavAttributeType.HtmlText => null,
EavAttributeType.EntityReference => null,
EavAttributeType.ValueFromList => null,
EavAttributeType.Money => null,
EavAttributeType.LocalizedText => GetLocalizedTextAttributeNestedProperties(),
EavAttributeType.DateRange => GetDateAttributeNestedProperties(),
EavAttributeType.Image => GetImageAttributeNestedProperties(),
Expand Down Expand Up @@ -441,6 +442,22 @@ private static List<ProjectionDocumentPropertySchema> GetCategoryPathsNestedProp
IsRetrievable = true,
IsFilterable = true,
IsSortable = true
},
new ()
{
PropertyName = "ParentMachineName",
PropertyType = TypeCode.String,
IsRetrievable = true,
IsFilterable = true,
IsSortable = true
},
new ()
{
PropertyName = "ParentId",
PropertyType = TypeCode.Object,
IsRetrievable = true,
IsFilterable = true,
IsSortable = true
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,25 @@ List<AttributeConfiguration> attributeConfigurations
}
);

schema.Properties.Add(
new ProjectionDocumentPropertySchema
{
PropertyName = "MachineName",
PropertyType = TypeCode.String,
IsKey = false,
IsSearchable = true,
IsRetrievable = true,
IsFilterable = true,
IsSortable = false,
IsFacetable = false
}
);

schema.Properties.Add(
ProjectionAttributesSchemaFactory.GetCategoryPathsAttributeSchema()
);


schema.Properties.Add(
new ProjectionDocumentPropertySchema
{
Expand Down
12 changes: 12 additions & 0 deletions CloudFabric.EAV.Domain/Utilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.RegularExpressions;

namespace CloudFabric.EAV.Domain.Utilities.Extensions;

public static class StringExtensions
{
public static string SanitizeForMachineName(this string str)
{
var specSymbolsRegex = new Regex("[^\\d\\w_]*", RegexOptions.None, TimeSpan.FromMilliseconds(100));
return specSymbolsRegex.Replace(str.Replace(" ", "_"), "").ToLower();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
Expand Down
Loading

0 comments on commit a655a0c

Please sign in to comment.