Esempio n. 1
0
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='entity'>
 /// </param>
 public static ApiOverrideModel ApiApiOverridePut(this ISettingsServiceV2API operations, KeyValueToUpdate entity = default(KeyValueToUpdate))
 {
     return(operations.ApiApiOverridePutAsync(entity).GetAwaiter().GetResult());
 }
Esempio n. 2
0
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='entity'>
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ApiOverrideModel> ApiApiOverridePutAsync(this ISettingsServiceV2API operations, KeyValueToUpdate entity = default(KeyValueToUpdate), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.ApiApiOverridePutWithHttpMessagesAsync(entity, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Esempio n. 3
0
        public async Task <ApiOverrideModel> Put([FromBody] KeyValueToUpdate entity)
        {
            try
            {
                var keyValues = await _keyValuesRepository.GetKeyValuesAsync();

                var keyValue = keyValues.FirstOrDefault(x => x.RowKey == entity.RowKey);
                if (keyValue == null)
                {
                    return(new ApiOverrideModel
                    {
                        Status = UpdateSettingsStatus.NotFound
                    });
                }

                var duplicatedKeys = keyValues.Where(x => x.RowKey != keyValue.RowKey && x.Value == entity.Value).ToList();
                if (entity.Forced == false && IS_PRODUCTION)
                {
                    if (duplicatedKeys.Count > 0)
                    {
                        return(new ApiOverrideModel
                        {
                            Status = UpdateSettingsStatus.HasDuplicated,
                            DuplicatedKeys = duplicatedKeys.Select(x => x.RowKey)
                        });
                    }
                }

                var keyValueEntity = new KeyValueEntity
                {
                    RowKey          = keyValue.RowKey,
                    Value           = entity.Value,
                    IsDuplicated    = duplicatedKeys.Count > 0,
                    Override        = keyValue.Override,
                    Types           = keyValue.Types,
                    RepositoryNames = keyValue.RepositoryNames,
                    EmptyValueType  = keyValue.EmptyValueType
                };
                var entitiesToUpload = new List <IKeyValueEntity>()
                {
                    keyValueEntity
                };

                if (duplicatedKeys.Any())
                {
                    var duplicationsToUpload = duplicatedKeys.Where(x => !x.IsDuplicated.HasValue || !x.IsDuplicated.Value);
                    duplicationsToUpload.ToList().ForEach(item =>
                    {
                        item.IsDuplicated = true;
                        entitiesToUpload.Add(item);
                    });
                }

                var oldDuplications = keyValues.Where(x => x.RowKey != keyValue.RowKey && x.Value == keyValue.Value);
                if (oldDuplications.Count() == 1)
                {
                    var oldDuplication = oldDuplications.First();
                    oldDuplication.IsDuplicated = false;
                    entitiesToUpload.Add(oldDuplication);
                }

                var result = await _keyValuesRepository.UpdateKeyValueAsync(entitiesToUpload);

                string strObj = JsonConvert.SerializeObject(keyValues);
                await _keyValueHistoryRepository.SaveKeyValueHistoryAsync(
                    keyValueEntity?.RowKey,
                    keyValueEntity?.Value,
                    strObj,
                    UserInfo.UserEmail,
                    UserInfo.Ip);

                var updatedKeyValues = await _keyValuesRepository.GetKeyValuesAsync();

                return(new ApiOverrideModel
                {
                    Status = result ? UpdateSettingsStatus.Ok : UpdateSettingsStatus.InternalError,
                    KeyValues = updatedKeyValues
                });
            }
            catch (Exception ex)
            {
                _log.Error(ex, context: entity);
                return(new ApiOverrideModel {
                    Status = UpdateSettingsStatus.InternalError
                });
            }
        }