示例#1
0
        public Task StoreAsync <T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }
            var contents = NewtonsoftJsonSerializer.Instance.Serialize((object)value);

            if (_context.Tokens.Any(t => t.Key == key))
            {
                var token = _context.Tokens.Single(t => t.Key == key);
                token.Data = contents;
            }
            else
            {
                _context.Tokens.Add(new  TokenModel
                {
                    Key  = key,
                    Data = contents
                });
            }
            _context.SaveChanges();
            return(CompletedTask);
        }
示例#2
0
        public bool UpdateCurrency(Currency currency)
        {
            if (currency == null)
            {
                return(false);
            }
            var res = false;

            try
            {
                var cur = _context.Currencies.FirstOrDefault(c => c.Code == currency.Code);
                if (cur == null)
                {
                    currency.LastUpdate = DateTime.UtcNow;
                    _context.Currencies.Add(currency);
                    _logger.Write(Severities.Information,
                                  $"Currency {currency.Code} added with rate {currency.Rate}");
                }
                else
                {
                    cur.IsBaseCurrency = currency.IsBaseCurrency;
                    cur.Rate           = currency.Rate;
                    cur.LastUpdate     = DateTime.UtcNow;
                    _logger.Write(Severities.Information,
                                  $"Currency {currency.Code} updated with rate {currency.Rate}");
                }
                _context.SaveChanges();

                if (currency.IsBaseCurrency)//Change base currency
                {
                    var oldBase = _context.Currencies.Where(c => c.IsBaseCurrency && c.Code != currency.Code);
                    if (oldBase.Any())
                    {
                        foreach (var cur0 in oldBase)
                        {
                            cur0.IsBaseCurrency = false;
                        }
                        _context.SaveChanges();
                    }
                }
                res = true;
            }
            catch (Exception e)
            {
                _logger.Write(Severities.Error, "Error update currency", e);
                res = false;
            }

            return(res);
        }
示例#3
0
        public void WriteSettings(SettingsNames name, string value)
        {
            var val = _context.SettingItems.SingleOrDefault(z => z.Name == name.ToString());

            if (val == null)
            {
                val = new SettingItem
                {
                    Name        = name.ToString(),
                    Value       = value,
                    LastChanged = DateTime.UtcNow
                };
                _context.SettingItems.Add(val);
            }
            else
            {
                val.Value       = value;
                val.LastChanged = DateTime.UtcNow;
            }
            _context.SaveChanges();
        }