public ActionResult <AccountModel> Get(int userId, string currency, decimal amount) { //https://localhost:44398/api/refill/1/HUF/100 var user = ctx.Users.FirstOrDefault(x => x.Id == userId); if (user == null) { return(NotFound("User not found.")); } user.Purse = ctx.Purses.Include(p => p.Accounts).FirstOrDefault(p => p.UserId == userId); user.Purse.Accounts.ToList().ForEach(a => a.Currency = ctx.Currencies.FirstOrDefault(c => c.Id == a.CurrencyId)); var account = user.Purse.Accounts.FirstOrDefault(a => a.Currency.Title == currency.ToUpper()); if (account == null) { return(NotFound("Account not found.")); } account.Amount += amount; ctx.SaveChanges(); var result = new AccountModel() { Title = account.Currency.Title, Amount = Currency.ConvertToStr(account.Amount) }; return(new JsonResult(result)); }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new PurseContext( serviceProvider.GetRequiredService < DbContextOptions <PurseContext> >())) { if (context.Users.Any()) { return; } string defaultSource = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; var currencies = Currency.Import(defaultSource); if (currencies != null) { currencies.ToList().ForEach(x => context.Currencies.Add(x)); var config = new Configuration() { CurrencySource = defaultSource, Updated = DateTime.Now.Date }; context.Configuration.Add(config); context.SaveChanges(); } var user1 = new User { Name = "Tom", Purse = new Purse() }; var user2 = new User { Name = "Ray", Purse = new Purse() }; context.Users.AddRange(user1, user2); context.SaveChanges(); FillPurse(user1.Purse, currencies.ToList(), context); FillPurse(user2.Purse, currencies.ToList(), context); context.SaveChanges(); } }
public ActionResult <List <AccountModel> > Get(int userId, string currencyFrom, string currencyTo, decimal amount, string ratesSource = null) { //https://localhost:44398/api/transfer/1/CHF/SEK/150 & optional /ratesSource var ratesActual = CheckRatesUpdateDate(); if (!ratesActual || ratesSource != null) { var currencies = Currency.Import(ratesSource == null ? ctx.Configuration.First().CurrencySource : ratesSource); if (currencies == null) { return(NoContent()); } RefillRates(currencies.ToList()); } var user = ctx.Users.FirstOrDefault(x => x.Id == userId); if (user == null) { return(NotFound("User not found.")); } user.Purse = ctx.Purses.Include(p => p.Accounts).FirstOrDefault(p => p.UserId == userId); user.Purse.Accounts.ToList().ForEach(a => a.Currency = ctx.Currencies.FirstOrDefault(c => c.Id == a.CurrencyId)); var accountFrom = user.Purse.Accounts.FirstOrDefault(a => a.Currency.Title.ToUpper() == currencyFrom.ToUpper()); var accountTo = user.Purse.Accounts.FirstOrDefault(a => a.Currency.Title.ToUpper() == currencyTo.ToUpper()); if (accountFrom == null || accountTo == null) { return(NotFound("Account not found.")); } if (accountFrom.Amount >= amount) { var baseCurrecySum = amount / accountFrom.Currency.Rate; accountFrom.Amount -= amount; var targetAccountAmount = baseCurrecySum * accountTo.Currency.Rate; accountTo.Amount += targetAccountAmount; ctx.SaveChanges(); } else { return(NotFound("Not enough money.")); } var result = new List <AccountModel>(); user.Purse.Accounts .ToList() .ForEach(a => result.Add(new AccountModel() { Title = a.Currency.Title, Amount = Currency.ConvertToStr(a.Amount) })); return(new JsonResult(result)); }
public void Save() { db.SaveChanges(); }