Exemple #1
0
        /// <summary>
        /// Add new KeyValuePair
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public T Add <T>(string keyName, T value) where T : IConvertible
        {
            //if (typeof(IConvertible).IsAssignableFrom(typeof(T)))
            //    throw new ArgumentException("Can't pass types that dont implement the IConvertible interface");

            if (Contains(keyName))
            {
                throw new ArgumentException("Key already exists");
            }

            StoredKeyValuePair storedKeyValuePair = new StoredKeyValuePair()
            {
                Key   = keyName,
                Value = value.ToString(),
                Type  = typeof(T).Name
            };

            try
            {
                using (ETHBotDBContext context = new ETHBotDBContext())
                {
                    context.StoredKeyValuePairs.Add(storedKeyValuePair);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(default(T));
            }

            return(Get <T>(keyName));
        }
Exemple #2
0
        /// <summary>
        /// Add KeyValuePair (From Admin)
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public string Add(string keyName, string value, string type)
        {
            if (Contains(keyName))
            {
                throw new ArgumentException("Key already exists");
            }

            StoredKeyValuePair storedKeyValuePair = new StoredKeyValuePair()
            {
                Key   = keyName,
                Value = value.ToString(),
                Type  = type
            };

            try
            {
                using (ETHBotDBContext context = new ETHBotDBContext())
                {
                    context.StoredKeyValuePairs.Add(storedKeyValuePair);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(null);
            }

            return(Get(keyName).Value);
        }