Exemplo n.º 1
0
    public async Task <IDictionary <string, VariableValue> > GetBestMatchingValuesAsync(
        IEnumerable <string> variableNames,
        Guid applicationId,
        Guid applicationPartId,
        Guid environmentId,
        CancellationToken cancellationToken)
    {
        ISet <string> distinctNames = variableNames.ToHashSet();

        IReadOnlyList <Variable> variables =
            await _variableStore.GetByNamesAsync(distinctNames, cancellationToken);

        IDictionary <string, VariableValue> values = new Dictionary <string, VariableValue>();

        IDictionary <Guid, Variable> ids = variables.ToDictionary(x => x.Id);

        IEnumerable <VariableValue> partValues = await _variableStore
                                                 .GetByApplicationPartIdAsync(applicationPartId, ids.Keys, cancellationToken);

        foreach (VariableValue value in partValues)
        {
            if (ids.TryGetValue(value.Key.VariableId, out Variable? variable) &&
                value.Key.EnvironmentId == environmentId)
            {
                values[variable.Name] = value;
                ids.Remove(value.Key.VariableId);
            }
        }

        IEnumerable <VariableValue> appValues =
            await _variableStore.GetByApplicationIdAsync(applicationId,
                                                         ids.Keys,
                                                         cancellationToken);

        foreach (VariableValue value in appValues)
        {
            if (ids.TryGetValue(value.Key.VariableId, out Variable? variable) &&
                value.Key.EnvironmentId == environmentId)
            {
                values[variable.Name] = value;
                ids.Remove(value.Key.VariableId);
            }
        }

        IEnumerable <VariableValue> globalValues =
            await _variableStore.GetGlobalVariableValue(ids.Keys, cancellationToken);

        foreach (VariableValue value in globalValues)
        {
            if (ids.TryGetValue(value.Key.VariableId, out Variable? variable) &&
                value.Key.EnvironmentId == environmentId)
            {
                values[variable.Name] = value;
                ids.Remove(value.Key.VariableId);
            }
        }

        return(values);
    }