Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dryRun and requiredAttributesCanBeNull to UpdateEntityIntance #121

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.11
PACKAGE_VERSION: 1.2.12

jobs:

Expand Down
27 changes: 21 additions & 6 deletions CloudFabric.EAV.Service/EAVService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,8 +2186,13 @@ public JsonDocument SerializeEntityInstanceToJsonSingleLanguage(
return JsonSerializer.SerializeToDocument(entityInstanceViewModel, serializerOptions);
}

public async Task<(EntityInstanceViewModel, ProblemDetails)> UpdateEntityInstance(string partitionKey,
EntityInstanceUpdateRequest updateRequest, CancellationToken cancellationToken)
public async Task<(EntityInstanceViewModel, ProblemDetails)> UpdateEntityInstance(
string partitionKey,
EntityInstanceUpdateRequest updateRequest,
bool dryRun = false,
bool requiredAttributesCanBeNull = false,
CancellationToken cancellationToken = default
)
{
EntityInstance? entityInstance =
await _entityInstanceRepository.LoadAsync(updateRequest.Id, partitionKey, cancellationToken);
Expand Down Expand Up @@ -2227,6 +2232,13 @@ await GetAttributeConfigurationsForEntityConfiguration(
.First(c => c.MachineName == attributeMachineNameToRemove);
updateRequest.AttributesToAddOrUpdate.RemoveAll(a =>
a.ConfigurationAttributeMachineName == attributeMachineNameToRemove);

if (requiredAttributesCanBeNull)
{
entityInstance.RemoveAttributeInstance(attributeMachineNameToRemove);
continue;
}

// validation against null will check if the attribute is required
List<string> errors = attrConfiguration.ValidateInstance(null);

Expand All @@ -2253,7 +2265,7 @@ await GetAttributeConfigurationsForEntityConfiguration(
}

var newAttribute = _mapper.Map<AttributeInstance>(newAttributeRequest);
List<string> errors = attrConfig.ValidateInstance(newAttribute);
List<string> errors = attrConfig.ValidateInstance(newAttribute, requiredAttributesCanBeNull);

if (errors.Count == 0)
{
Expand Down Expand Up @@ -2285,10 +2297,13 @@ await GetAttributeConfigurationsForEntityConfiguration(
return (null, new ValidationErrorResponse(validationErrors))!;
}

var saved = await _entityInstanceRepository.SaveAsync(_userInfo, entityInstance, cancellationToken);
if (!saved)
if (!dryRun)
{
//TODO: Throw a error when ready
var saved = await _entityInstanceRepository.SaveAsync(_userInfo, entityInstance, cancellationToken);
if (!saved)
{
//TODO: Throw a error when ready
}
}

return (_mapper.Map<EntityInstanceViewModel>(entityInstance), null)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
Id = createdInstance.Id,
EntityConfigurationId = createdInstance.EntityConfigurationId,
AttributesToAddOrUpdate = updatedAttributes
},
CancellationToken.None
}
);

updateErrors.Should().BeNull();
Expand Down
35 changes: 7 additions & 28 deletions CloudFabric.EAV.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,10 +2180,7 @@ public async Task UpdateInstance_UpdateAttribute_Success()
};

(EntityInstanceViewModel updatedInstance, _) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),updateRequest);
updatedInstance.Attributes.First(a => a.ConfigurationAttributeMachineName == changedAttributeName)
.As<NumberAttributeInstanceViewModel>().Value.Should().Be(10);
}
Expand Down Expand Up @@ -2222,10 +2219,7 @@ public async Task UpdateInstance_UpdateAttribute_FailValidation()
};

(EntityInstanceViewModel updatedInstance, ProblemDetails validationErrors) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);
updatedInstance.Should().BeNull();
validationErrors.As<ValidationErrorResponse>().Errors.Should().ContainKey(changedAttributeName);
}
Expand Down Expand Up @@ -2264,10 +2258,7 @@ public async Task UpdateInstance_AddAttribute_Success()
};

(EntityInstanceViewModel updatedInstance, _) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);
updatedInstance.Attributes.First(a => a.ConfigurationAttributeMachineName == changedAttributeName)
.As<NumberAttributeInstanceViewModel>().Value.Should().Be(30);
}
Expand Down Expand Up @@ -2305,10 +2296,7 @@ public async Task CreateInstance_NumberOfItemsWithAttributeUpdated_Success()
Id = createdInstance.Id
};

await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);

ProjectionQueryResult<AttributeConfigurationListItemViewModel> attributeConfigurations =
await _eavService.ListAttributes(
Expand Down Expand Up @@ -2403,10 +2391,7 @@ public async Task UpdateInstance_AddAttribute_IgnoreAttributeNotInConfig()
};

(EntityInstanceViewModel updatedInstance, _) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);
updatedInstance.Attributes.FirstOrDefault(a => a.ConfigurationAttributeMachineName == changedAttributeName)
.Should().BeNull();
}
Expand Down Expand Up @@ -2442,10 +2427,7 @@ public async Task UpdateInstance_RemoveAttribute_Success()
};

(EntityInstanceViewModel updatedInstance, _) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);
updatedInstance.Attributes.FirstOrDefault(a => a.ConfigurationAttributeMachineName == changedAttributeName)
.Should().BeNull();
}
Expand Down Expand Up @@ -2483,10 +2465,7 @@ public async Task UpdateInstance_RemoveAttribute_FailValidation()
};

(EntityInstanceViewModel updatedInstance, ProblemDetails errors) =
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(),
updateRequest,
CancellationToken.None
);
await _eavService.UpdateEntityInstance(createdConfiguration.Id.ToString(), updateRequest);
updatedInstance.Should().BeNull();
errors.As<ValidationErrorResponse>().Errors.Should().ContainKey(changedAttributeName);
}
Expand Down