public void UpdateUnit(string currencyName, double price) { try { using var connection = new SqlConnection(_connectionString.AddCatalog(CatalogName)); CryptoCurrencyDbModel currencyRecord = GetCryptoCurrency(currencyName, false); if (currencyRecord != null) { //Set value currencyRecord.UnitPrice = price; _ = connection.Update(TableName, currencyRecord); } else { _ = connection.Insert(TableName, new CryptoCurrencyDbModel { Id = GetLatestId() + 1 ?? 1, CurrencyName = currencyName, UnitPrice = price }); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public int?GetLatestId() { using var connection = new SqlConnection(_connectionString.AddCatalog(CatalogName)); CryptoCurrencyDbModel currencyRecord = connection.QueryAll <CryptoCurrencyDbModel>(TableName).OrderByDescending(c => c.Id).FirstOrDefault(); return(currencyRecord?.Id); }
public void Can_Update_UnitInDollar_Bitcoin() { string currencyName = "Bitcoin"; var cryptoCurrencyDb = new CryptoCurrencyDb(DatabaseConnectionString); cryptoCurrencyDb.UpdateUnit(currencyName, 100); CryptoCurrencyDbModel result = cryptoCurrencyDb.GetCryptoCurrency(currencyName, true); Assert.Equal(100, result.UnitPrice); }
/// <summary> /// Gets the crypto currency from database /// </summary> /// <param name="currencyName"></param> /// <param name="throwException">Indicates if it should throw <see cref="ArgumentException"/> if null,</param> /// <exception cref="ArgumentException">Is thrown if no records is found and <see cref="throwException"/> is set to true.</exception> /// <returns>A crypto currency record.</returns> public CryptoCurrencyDbModel GetCryptoCurrency(string currencyName, bool throwException) { using var connection = new SqlConnection(_connectionString.AddCatalog(CatalogName)); CryptoCurrencyDbModel currencyRecord = connection.Query <CryptoCurrencyDbModel>(TableName, c => c.CurrencyName == currencyName).FirstOrDefault(); if (currencyRecord == null && throwException) { throw new ArgumentException("The given currency: {currencyName} is not found", currencyName); } return(currencyRecord); }
/// <summary> /// Konverterer fra en kryptovaluta til en anden. /// Hvis en af de angivne valutaer ikke findes, kaster funktionen en ArgumentException /// /// </summary> /// <param name="fromCurrencyName">Navnet på den valuta, der konverterers fra</param> /// <param name="toCurrencyName">Navnet på den valuta, der konverteres til</param> /// <param name="amount">Beløbet angivet i valutaen angivet i fromCurrencyName</param> /// <returns>Værdien af beløbet i toCurrencyName</returns> public double Convert(String fromCurrencyName, String toCurrencyName, double amount) { //The validation happens inside 'GetCryptoCurrency' CryptoCurrencyDbModel fromCurrency = _cryptoCurrencyDb.GetCryptoCurrency(fromCurrencyName, true); //We use the unit price from the database to get it in dollar double fromCurrencyDollar = amount * fromCurrency.UnitPrice; //The validation happens inside 'GetCryptoCurrency' CryptoCurrencyDbModel toCurrency = _cryptoCurrencyDb.GetCryptoCurrency(toCurrencyName, true); //Convert to dollars var toCurrencyDollar = fromCurrencyDollar / toCurrency.UnitPrice; return(toCurrencyDollar); }