Adding currency exchange rate handling

This commit is contained in:
Rick Falkvinge
2014-12-27 20:21:35 +01:00
parent 4575305ff0
commit 41949bbc1c
11 changed files with 233 additions and 1 deletions
+1
View File
@@ -68,6 +68,7 @@
<Compile Include="Types\BasicElection.cs" />
<Compile Include="Types\BasicExceptionLog.cs" />
<Compile Include="Types\BasicExternalActivity.cs" />
<Compile Include="Types\Financial\BasicExchangeRateSnapshot.cs" />
<Compile Include="Types\Financial\BasicFinancialTransactionTagSet.cs" />
<Compile Include="Types\Financial\BasicFinancialTransactionTagSetType.cs" />
<Compile Include="Types\Financial\BasicFinancialTransactionTagType.cs" />
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Swarmops.Basic.Interfaces;
namespace Swarmops.Basic.Types.Financial
{
[Serializable]
public class BasicExchangeRateSnapshot: IHasIdentity
{
[Obsolete("Do not call the default constructor directly. It is reserved for serialization.", true)]
public BasicExchangeRateSnapshot()
{
// do not call
}
public BasicExchangeRateSnapshot (int identity, DateTime dateTime)
{
this.ExchangeRateSnapshotId = identity;
this.DateTime = dateTime;
}
public BasicExchangeRateSnapshot (BasicExchangeRateSnapshot original) :
this (original.Identity, original.DateTime)
{
// empty copy ctor
}
public int ExchangeRateSnapshotId { get; private set; }
public DateTime DateTime { get; private set; }
public List<BasicExchangeRateSnapshotDatapoint> Datapoints { get; protected set; }
public int Identity { get { return ExchangeRateSnapshotId; }}
}
[Serializable]
public class BasicExchangeRateSnapshotDatapoint
{
public string CurrencyCodeA { get; set; }
public string CurrencyCodeB { get; set; }
public double APerB { get; set; }
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Swarmops.Database
/// </summary>
public static int DbVersionExpected
{
get { return 8; }
get { return 9; }
}
+1
View File
@@ -6,3 +6,4 @@
0006 - Added Memberships.LocalNumber and .FamilyNumber; added CurrencyExchangeRate tables/procs. [Rick Falkvinge]
0007 - NL update; added Lat/Long to PostalCodes and to People, indexed [RF]
0008 - GeographyUpdate updates ahead of deploying: added country, Guid [RF]
0009 - Fixed SalaryTaxLevels and CreateExchangeRateDataPoint [RF]
+26
View File
@@ -0,0 +1,26 @@
ALTER TABLE `SalaryTaxLevels`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`TaxLevelId`, `BracketLow`, `CountryId`, `Year`)
#
DROP PROCEDURE `InsertExchangeRateSnapshotData`
#
CREATE `CreateExchangeRateDatapoint`(
IN currencyExchangeRateSnapshotId INTEGER,
IN currencyAId INTEGER,
IN currencyBId INTEGER,
IN aPerB DOUBLE
)
BEGIN
INSERT INTO CurrencyExchangeRateSnapshotData (CurrencyExchangeRateSnapshotId,CurrencyAId,CurrencyBId,APerB)
VALUES (currencyExchangeRateSnapshotId,currencyAId,currencyBId,aPerB);
END
+82
View File
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using Swarmops.Basic.Types.Financial;
namespace Swarmops.Logic.Financial
{
[Serializable]
public class ExchangeRateSnapshot: BasicExchangeRateSnapshot
{
[Obsolete ("Do not call this ctor directly. Intended for serialization only.", true)]
public ExchangeRateSnapshot()
{
// do not call
}
private ExchangeRateSnapshot (BasicExchangeRateSnapshot basic)
: base (basic)
{
// private ctor
// TODO: Verify that data is in, or load it
}
public static ExchangeRateSnapshot FromBasic (BasicExchangeRateSnapshot basic)
{
return new ExchangeRateSnapshot (basic);
}
public static void Create()
{
using(WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
string rateDataRaw = client.DownloadString("https://bitpay.com/api/rates");
// BitPay doesn't provide valid JSON - the rate field isn't enclosed in quotes - so we can't use JSON Deserialization; we'll
// have to use Regex matching instead.
string regexPattern = @"\{\""code\"":\""([A-Z]+)\"",\""name\"":\""([^\""]+)\"",\""rate\"":([0-9\.]+)\}";
Regex regex = new Regex (regexPattern);
Match match = regex.Match (rateDataRaw);
if (match.Success)
{
// We have at least one match, so prepare a new ExchangeRate snapshot
}
while (match.Success)
{
string currencyCode = match.Groups[0].Value;
string currencyName = match.Groups[1].Value;
double btcRate = Double.Parse (match.Groups[2].Value, NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture);
match = match.NextMatch();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
BitpayRateDatapoint[] rates = (BitpayRateDatapoint[]) serializer.Deserialize<BitpayRateDatapoint[]> (rateDataRaw);
}
}
}
[Serializable]
public class BitpayRateDatapoint
{
string code { get; set; }
string name { get; set; }
string rate { get; set; }
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Swarmops.Basic.Types.Financial;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
{
public class ExchangeRateSnapshots: PluralBase<ExchangeRateSnapshots, ExchangeRateSnapshot, BasicExchangeRateSnapshot>
{
// the templated base takes care of most stuff
}
}
+3
View File
@@ -67,6 +67,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
</ItemGroup>
@@ -119,6 +120,8 @@
<Compile Include="Communications\Transmission\PayloadBase.cs" />
<Compile Include="Financial\BankDataReaders\SEPaymentsBankgiroReader.cs" />
<Compile Include="Financial\Currencies.cs" />
<Compile Include="Financial\ExchangeRateSnapshot.cs" />
<Compile Include="Financial\ExchangeRateSnapshots.cs" />
<Compile Include="Financial\IBankDataAccountStatementReader.cs" />
<Compile Include="Financial\IBankDataPaymentsReader.cs" />
<Compile Include="Financial\BankDataReaders\TabSeparatedValuesReader.cs" />
+44
View File
@@ -0,0 +1,44 @@
<%@ WebService Language="C#" Class="GetFinancialData" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Swarmops.Logic.Financial;
[WebService(Namespace = "http://swarmops.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class GetFinancialData : System.Web.Services.WebService {
/*
[WebMethod]
public ExchangeRateSnapshots GetExhangeRateDataSince(DateTime dateTimeSince)
{
return ExchangeRateSnapshots.GetSince (dateTimeSince);
}
[WebMethod]
public double GetExchangeRate (string currencyCodeA, string currencyCodeB)
{
return ExchangeRateSnapshot.Latest.GetExchangeRate (currencyCodeA, currencyCodeB);
}
[WebMethod]
public double GetExchangeRate (string currencyCodeA, string currencyCodeB, DateTime onDate)
{
return ExchangeRateSnapshot.FromDate (onDate).GetExchangeRate (currencyCodeA, currencyCodeB);
}*/
[WebMethod]
public Currency GetCurrency (string currencyCode)
{
return Currency.FromCode (currencyCode);
}
public Currencies GetCurrencies()
{
return Currencies.GetAll();
}
}
+13
View File
@@ -1,5 +1,6 @@
<%@ WebService Language="C#" Class="GetGeographyData" %>
using System;
using System.Web.Services;
using Swarmops.Logic.Structure;
@@ -44,4 +45,16 @@ public class GetGeographyData : System.Web.Services.WebService
{
return Country.FromCode(countryCode).Geography;
}
[WebMethod]
public GeographyUpdates GetGeographyUpdatesSince (DateTime dateTime)
{
return GeographyUpdates.GetCreatedSince (dateTime);
}
[WebMethod]
public GeographyUpdates GetGeographyUpdates()
{
return GeographyUpdates.GetCreatedSince (new DateTime (1900, 1, 1));
}
}
+3
View File
@@ -1,5 +1,6 @@
using System;
using Resources;
using Swarmops.Logic.Financial;
public partial class Default : PageV5Base
{
@@ -14,5 +15,7 @@ public partial class Default : PageV5Base
InfoBoxLiteral =
"This is a Dashboard placeholder. It will contain a snapshot of the state of things as soon as the basic functions are re-implemented in the new interface.";
ExchangeRateSnapshot.Create();
}
}