public async Task <ActionResult <CryptoItem> > PostCryptoItem(CryptoItem item) { _context.CryptoItems.Add(item); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetCryptoItem), new { id = item.Id }, item)); }
public async Task <IActionResult> PutCurrency(int id, Currency currency) { if (id != currency.CurrencyId) { return(BadRequest()); } _context.Entry(currency).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CurrencyExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create([Bind("idCrypto,Name,Symbol,Price,Change24h,Change7d")] Crypto crypto) { if (ModelState.IsValid) { _context.Add(crypto); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(crypto)); }
public async Task UpdateCoinsPrices() { List <Pair> allPairs = await _context.Pairs.Include(p => p.User).Include(p => p.FirstCurrency).Include(p => p.SecondCurrency).ToListAsync(); if (allPairs != null) { string baseUrl = @"https://min-api.cryptocompare.com/data/pricemulti"; List <string> fsymsArray = new List <string>(); string tsymsParam = @"&tsyms=USD"; fsymsArray = ExtractSymbols(allPairs).ToList(); string fsymsParam = @"?fsyms="; string jsonResponse; //API can handle a request with a maximum of 300 currency symbols at a time, so //if you have more than 300 - it will make multiple requests for (int i = 0; i < fsymsArray.Count; i += 300) { fsymsParam += string.Join(',', fsymsArray.Skip(i).Take(300)); string request = baseUrl + fsymsParam + tsymsParam + _apiKey; jsonResponse = await SendRequest(request); fsymsParam = @"?fsyms="; Dictionary <string, CurrencyValueUSD> currenciesPrices = JsonSerializer.Deserialize <Dictionary <string, CurrencyValueUSD> >(jsonResponse); foreach (KeyValuePair <string, DeserializingJSON.CurrencyValueUSD> pair in currenciesPrices) { Currency currencyUpdate = _context.Currencies.FirstOrDefault(c => c.Symbol == pair.Key); if (currencyUpdate != null) { currencyUpdate.ValueUSD = pair.Value.USD; } } await UpdatePairsPrices(allPairs); } CheckPairsPrice(allPairs); } try { await _context.SaveChangesAsync(); } catch (Exception e) { _logger.LogError(e.Message); } }
public async Task AddNewImporter([FromBody] Exchange value) { // Validation var availableExchanges = await GetAvaliableExchanges(); var exchangeMeta = availableExchanges.SingleOrDefault(e => e.ExchangeId == value.ExchangeId); if (exchangeMeta == null) { throw new ArgumentException("No exchange Plugin exists for this exchange"); } if (exchangeMeta.SupportsPrivateKey && string.IsNullOrEmpty(value.PrivateKey)) { throw new ArgumentOutOfRangeException(nameof(value.PrivateKey), exchangeMeta.LabelPrivateKey + " was not provided"); } if (exchangeMeta.SupportsPublicKey && string.IsNullOrEmpty(value.PublicKey)) { throw new ArgumentOutOfRangeException(nameof(value.PublicKey), exchangeMeta.LabelPublicKey + " was not provided"); } if (exchangeMeta.SupportsPassphrase && string.IsNullOrEmpty(value.Passphrase)) { throw new ArgumentOutOfRangeException(nameof(value.Passphrase), exchangeMeta.LabelPassphrase + " was not provided"); } // Triming value.PublicKey = value.PublicKey?.Trim(); value.PrivateKey = value.PrivateKey?.Trim(); value.Passphrase = value.Passphrase?.Trim(); value.Comment = value.Comment?.Trim(); // Add Data await _cryptoContext.Exchanges.AddAsync(value); await _cryptoContext.SaveChangesAsync(); // Schedule import BackgroundJob.Enqueue <Importer>(i => i.Import(value.Id, true)); }
public async Task <bool> SaveChangesAsync() { return(await _context.SaveChangesAsync() > 0); }