Skip to content

Commit

Permalink
Merge pull request #129 from Tech-Fabric/feature/2027/update-ES-nugets
Browse files Browse the repository at this point in the history
Feature/2027/update es nugets
  • Loading branch information
ustims authored Sep 8, 2023
2 parents 83df39c + 64c05a4 commit 42b32ca
Show file tree
Hide file tree
Showing 18 changed files with 739 additions and 249 deletions.
2 changes: 1 addition & 1 deletion CloudFabric.EAV.Domain/CloudFabric.EAV.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down
46 changes: 46 additions & 0 deletions CloudFabric.EAV.Domain/GeneratingValues/Counter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace CloudFabric.EAV.Domain.GeneratedValues;

public class Counter : IGeneratedValueInfo
{
public long NextValue { get; set; }

private int? _lastIncrement;
public int? LastIncrement
{
get { return _lastIncrement; }
init { _lastIncrement = value; }
}

private DateTime _timestamp;
public DateTime Timestamp
{
get { return _timestamp; }
init { _timestamp = value; }
}

public Guid AttributeConfidurationId { get; init; }

// Used for Json deserialization
public Counter()
{
}

public Counter(long nextValue, DateTime timestamp, Guid attributeConfigurationId, int? lastIncrement = null)
{
NextValue = nextValue;
Timestamp = timestamp;
AttributeConfidurationId = attributeConfigurationId;
LastIncrement = lastIncrement;
}

public void SetTimestamp(DateTime timestamp)
{
_timestamp = timestamp;
}

public long Step(int increment)
{
_lastIncrement = increment;
return NextValue += increment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CloudFabric.EAV.Domain.GeneratedValues;

public interface IGeneratedValueInfo
{
public Guid AttributeConfidurationId { get; init; }

public DateTime Timestamp { get; init; }
}
10 changes: 10 additions & 0 deletions CloudFabric.EAV.Enums/GeneratedValueActionStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CloudFabric.EAV.Enums;

public enum GeneratedValueActionStatus
{

NoAction,
Saved,
Conflict,
Failed
}
20 changes: 20 additions & 0 deletions CloudFabric.EAV.Models/GeneratedValueActionResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using CloudFabric.EAV.Enums;

namespace CloudFabric.EAV.Models;

public class GeneratedValueActionResponse
{
public GeneratedValueActionResponse(Guid entityConfiguration, Guid attributeConfigurationId)
{
EntityConfigurationId = entityConfiguration;
AttributeConfigurationId = attributeConfigurationId;
}

public Type? GeneratedValueType { get; set; }

public GeneratedValueActionStatus Status { get; set; }

public Guid EntityConfigurationId { get; }

public Guid AttributeConfigurationId { get; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CloudFabric.EAV.Enums;
using CloudFabric.EAV.Enums;

namespace CloudFabric.EAV.Models.RequestModels.Attributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class CategoryInstanceCreateRequest
public Guid CategoryTreeId { get; set; }

public string MachineName { get; set; }

public List<AttributeInstanceCreateUpdateRequest> Attributes { get; set; }

public Guid? ParentId { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions CloudFabric.EAV.Options/ValueAttributeServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CloudFabric.EAV.Options;

public class ValueAttributeServiceOptions
{
public int ActionMaxCountAttempts { get; set; } = 4;
}
14 changes: 14 additions & 0 deletions CloudFabric.EAV.Service/Abstractions/IGeneratedValueService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CloudFabric.EAV.Domain.GeneratedValues;
using CloudFabric.EAV.Models;
using CloudFabric.EAV.Models.ViewModels.Attributes;

namespace CloudFabric.EAV.Service.Abstractions;

public interface IGeneratedValueService<T> where T : IGeneratedValueInfo
{
Task<T?> Create(Guid entityConfigurationId, AttributeConfigurationViewModel attributeConfiguration);

Task<T?> Load(Guid entityConfigurationId, Guid attributeConfigurationId);

Task<GeneratedValueActionResponse> Save(Guid entityConfigurationId, Guid attributeConfigurationId, T generatedValueInfo);
}
6 changes: 4 additions & 2 deletions CloudFabric.EAV.Service/EAVCategoryService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

using AutoMapper;
Expand Down Expand Up @@ -34,13 +34,15 @@ public EAVCategoryService(ILogger<EAVService<CategoryUpdateRequest, Category, Ca
AggregateRepositoryFactory aggregateRepositoryFactory,
ProjectionRepositoryFactory projectionRepositoryFactory,
EventUserInfo userInfo,
ValueAttributeService valueAttributeService,
IOptions<ElasticSearchQueryOptions>? elasticSearchQueryOptions = null) : base(logger,
new CategoryFromDictionaryDeserializer(mapper),
mapper,
jsonSerializerOptions,
aggregateRepositoryFactory,
projectionRepositoryFactory,
userInfo)
userInfo,
valueAttributeService)
{

_elasticSearchQueryOptions = elasticSearchQueryOptions != null
Expand Down
42 changes: 22 additions & 20 deletions CloudFabric.EAV.Service/EAVEntityInstanceService.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
using System.Text.Json;
using System.Text.Json;

using AutoMapper;

using CloudFabric.EAV.Domain.GeneratedValues;
using CloudFabric.EAV.Domain.Models;
using CloudFabric.EAV.Domain.Models.Attributes;
using CloudFabric.EAV.Enums;
using CloudFabric.EAV.Models.RequestModels;
using CloudFabric.EAV.Models.ViewModels;
using CloudFabric.EAV.Options;
using CloudFabric.EAV.Service.Serialization;
using CloudFabric.EventSourcing.Domain;
using CloudFabric.EventSourcing.EventStore;
using CloudFabric.EventSourcing.EventStore.Persistence;
using CloudFabric.Projections;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using ProjectionDocumentSchemaFactory =
CloudFabric.EAV.Domain.Projections.EntityInstanceProjection.ProjectionDocumentSchemaFactory;
namespace CloudFabric.EAV.Service;

public class EAVEntityInstanceService: EAVService<EntityInstanceUpdateRequest, EntityInstance, EntityInstanceViewModel>
public class EAVEntityInstanceService : EAVService<EntityInstanceUpdateRequest, EntityInstance, EntityInstanceViewModel>
{

public EAVEntityInstanceService(ILogger<EAVService<EntityInstanceUpdateRequest, EntityInstance, EntityInstanceViewModel>> logger,
IMapper mapper,
JsonSerializerOptions jsonSerializerOptions,
AggregateRepositoryFactory aggregateRepositoryFactory,
ProjectionRepositoryFactory projectionRepositoryFactory,
EventUserInfo userInfo) : base(logger,
EventUserInfo userInfo,
ValueAttributeService valueAttributeService) : base(logger,
new EntityInstanceFromDictionaryDeserializer(mapper),
mapper,
jsonSerializerOptions,
aggregateRepositoryFactory,
projectionRepositoryFactory,
userInfo)
userInfo,
valueAttributeService)
{
}

Expand Down Expand Up @@ -390,9 +389,11 @@ await GetAttributeConfigurationsForEntityConfiguration(
}



public async Task<(EntityInstanceViewModel?, ProblemDetails?)> CreateEntityInstance(
EntityInstanceCreateRequest entity, bool dryRun = false, bool requiredAttributesCanBeNull = false, CancellationToken cancellationToken = default
public async Task<(EntityInstanceViewModel?, ProblemDetails?)> CreateEntityInstance(
EntityInstanceCreateRequest entity,
bool dryRun = false,
bool requiredAttributesCanBeNull = false,
CancellationToken cancellationToken = default
)
{
EntityConfiguration? entityConfiguration = await _entityConfigurationRepository.LoadAsync(
Expand Down Expand Up @@ -421,6 +422,8 @@ await GetAttributeConfigurationsForEntityConfiguration(
);

var validationErrors = new Dictionary<string, string[]>();
List<IGeneratedValueInfo?> generatedValues = new();

foreach (AttributeConfiguration a in attributeConfigurations)
{
AttributeInstance? attributeValue = entityInstance.Attributes
Expand All @@ -432,10 +435,7 @@ await GetAttributeConfigurationsForEntityConfiguration(
validationErrors.Add(a.MachineName, attrValidationErrors.ToArray());
}

// Note that this method updates entityConfiguration state (for serial attribute it increments the number
// stored in externalvalues) but does not save entity configuration, we need to do that manually outside of
// the loop
InitializeAttributeInstanceWithExternalValuesFromEntity(entityConfiguration, a, attributeValue);
generatedValues.Add(await _valueAttributeService.GenerateAttributeInstanceValue(entityConfiguration, a, attributeValue));
}

if (validationErrors.Count > 0)
Expand All @@ -445,13 +445,15 @@ await GetAttributeConfigurationsForEntityConfiguration(

if (!dryRun)
{
var entityConfigurationSaved = await _entityConfigurationRepository
.SaveAsync(_userInfo, entityConfiguration, cancellationToken)
.ConfigureAwait(false);
var response = await _valueAttributeService.SaveValues(entityConfiguration.Id, generatedValues);

if (!entityConfigurationSaved)
foreach (var actionResponse in response.Where(x => x.Status == GeneratedValueActionStatus.Failed))
{
throw new Exception("Entity was not saved");
var attributeMachineName = attributeConfigurations.First(x => x.Id == actionResponse.AttributeConfigurationId).MachineName;

validationErrors.Add(
attributeMachineName,
new string[] { $"Failed to generate value: {actionResponse.GeneratedValueType?.Name}" });
}

ProjectionDocumentSchema schema = ProjectionDocumentSchemaFactory
Expand Down
Loading

0 comments on commit 42b32ca

Please sign in to comment.