public async Task <IActionResult> CreateCashWithdrawal( [FromBody] CashWithdrawalCreateIn cashWithdrawal, [FromHeader(Name = "Authorization")] string token) { var userId = User.Claims.GetUserId(); await mediator.Send(new CreateCashWithdrawal.Command(cashWithdrawal, userId)).ConfigureAwait(false); return(Created( new Uri(Request.Path, UriKind.Relative), null)); }
protected async override Task Handle(Command request, CancellationToken cancellationToken) { var reg = new UserRegistration { Username = "******", Email = request.RandomEmail, Password = "******" }; var user = await mediator.Send(new CreateUser.Command(reg)).ConfigureAwait(false); var userId = context.Users.Single(u => u.Email == user.Email).Id; // create categories var catNames = new [] { "Transportation", "Dining", "Groceries", "Entertainment", "Accommodations", "Utilities", "Medical", "Fees", "Deposit", "Non-trip", "Loss/Gain" }; var categories = await mediator.Send(new CreateCategory.Query( catNames.Select(cn => new CategoryIn { CategoryName = cn, UserId = userId }).ToArray() )).ConfigureAwait(false); var keywordNames = new [] { "Expensive", "Cheap", "Coffee", "Alcohol", "Lunch", "Dinner", "Breakfast", "Uber", "Doctor", "Dentist" }; var keywords = await mediator.Send(new CreateKeyword.Query(keywordNames, userId)).ConfigureAwait(false); var countries = context.Countries .Where(c => new [] { "Thailand", "Mexico", "Viet Nam", "United States", "Japan" }.Contains(c.CountryName)).ToArray(); LocationOut[] locations = new LocationOut[0]; foreach (var country in countries) { switch (country.CountryName) { case "Thailand": locations = await CreateLocation(country.Id, "Chiang Mai").ConfigureAwait(false); locations = await CreateLocation(country.Id, "Bangkok").ConfigureAwait(false); locations = await CreateLocation(country.Id, "Koh Tao").ConfigureAwait(false); break; case "Mexico": locations = await CreateLocation(country.Id, "Guadalajara").ConfigureAwait(false); locations = await CreateLocation(country.Id, "Merida").ConfigureAwait(false); locations = await CreateLocation(country.Id, "Playa del Carmen").ConfigureAwait(false); break; case "Viet Nam": locations = await CreateLocation(country.Id, "Hanoi").ConfigureAwait(false); locations = await CreateLocation(country.Id, "Ho Chi Minh City").ConfigureAwait(false); break; case "United States": locations = await CreateLocation(country.Id, "California").ConfigureAwait(false); locations = await CreateLocation(country.Id, "New York").ConfigureAwait(false); break; case "Japan": locations = await CreateLocation(country.Id, "Tokyo").ConfigureAwait(false); break; } } var currencyIds = context.Currencies.Select(c => c.Id).ToArray(); var titles = context.Transactions .OrderBy(t => Guid.NewGuid()) .Select(t => t.Title) .Take(100) .ToArray(); var transactionCount = 200; var dayCounter = 0; var rand = new Random(); for (int i = 0; i < transactionCount; i++) { dayCounter = OneIn(3) ? dayCounter - rand.Next(1, 4) : dayCounter; var trans = new TransactionCreateIn { TransDate = DateTime.Today.AddDays(dayCounter).ToString("yyyy-MM-dd"), Amount = rand.Next(5, 500), LocationId = locations.OrderBy(l => Guid.NewGuid()).First().Id, CurrencyId = currencyIds.OrderBy(c => Guid.NewGuid()).First(), CategoryId = categories.OrderBy(c => Guid.NewGuid()).First().Id, Title = titles.OrderBy(t => Guid.NewGuid()).First(), Memo = OneIn(4) ? ipsum.Substring(0, (int)(ipsum.Length * rand.NextDouble())) : null, PaidWithCash = OneIn(6) ? false : true, UserId = userId, KeywordIds = OneIn(3) ? keywords.OrderBy(k => Guid.NewGuid()) .Take(rand.Next(1, 4)) .Select(k => k.Id) .ToArray() : null }; await mediator.Send(new CreateTransaction.Command(trans, userId)).ConfigureAwait(false); } var withdrawalTitles = context.CashWithdrawals .OrderBy(t => Guid.NewGuid()) .Select(t => t.Title) .Take(100) .ToArray(); var withdrawalCount = 20; dayCounter = 0; for (int i = 0; i < withdrawalCount; i++) { dayCounter = OneIn(3) ? dayCounter - rand.Next(1, 4) : dayCounter; var withdrawal = new CashWithdrawalCreateIn { Title = withdrawalTitles.OrderBy(t => Guid.NewGuid()).First(), TransDate = DateTime.Today.AddDays(dayCounter).ToString("yyyy-MM-dd"), Amount = rand.Next(1, 10) * 100, CurrencyId = currencyIds.OrderBy(c => Guid.NewGuid()).First(), Memo = OneIn(4) ? ipsum.Substring(0, (int)(ipsum.Length * rand.NextDouble())) : null, UserId = userId }; await mediator.Send(new CreateCashWithdrawal.Command(withdrawal, userId)).ConfigureAwait(false); } bool OneIn(int x) { return(rand.Next() % x == 0); } Task <LocationOut[]> CreateLocation(int countryId, string locationName) { var location = new Location { CountryId = countryId, LocationName = locationName, UserId = userId }; return(mediator.Send(new CreateLocation.Query(location))); } Log.Information("Finished creating user {User}", reg); }
protected async override Task Handle(Command request, CancellationToken response) { var import = request.Import; //ensure that the user exists and that it's empty await DeleteUserData(request.UserId); //get currency map var dbCurrencies = await mediator.Send(new GetCurrencies.Query()).ConfigureAwait(false); var ccyMap = new Dictionary <int, int>(); foreach (var ccy in import.Currencies) { var dbMatch = dbCurrencies.First(d => d.IsoCode == ccy.IsoCode); ccyMap.Add(ccy.CurrencyId, dbMatch.Id); } //get country map var dbCountries = await mediator.Send(new GetCountries.Query()).ConfigureAwait(false); var countryMap = new Dictionary <int, int>(); foreach (var country in import.Countries) { var dbMatch = dbCountries.First(d => d.CountryName == country.CountryName); countryMap.Add(country.CountryId, dbMatch.Id); } //enter keywords var keywords = import.Keywords.Select(k => k.Keyword).ToArray(); var newKeywords = await mediator.Send(new CreateKeyword.Query(keywords, request.UserId)).ConfigureAwait(false); var keywordMap = new Dictionary <int, int>(); foreach (var kw in import.Keywords) { var dbMatch = newKeywords.First(nk => nk.KeywordName == kw.Keyword); keywordMap.Add(kw.Id, dbMatch.Id); } //enter categories var categories = import.Categories.Select(c => new CategoryIn { CategoryName = c.CategoryName, UserId = request.UserId }).ToArray(); var newCategories = await mediator.Send(new CreateCategory.Query(categories)).ConfigureAwait(false); var categoryMap = new Dictionary <int, int>(); foreach (var cat in import.Categories) { var dbMatch = newCategories.First(nc => nc.CategoryName == cat.CategoryName); categoryMap.Add(cat.CategoryId, dbMatch.Id); } //enter locations LocationOut[] newLocations = null; foreach (var location in import.Locations) { var newLocation = new Location { LocationName = location.LocationName, CountryId = countryMap[location.CountryId], UserId = request.UserId }; newLocations = await mediator.Send(new CreateLocation.Query(newLocation)).ConfigureAwait(false); } var locationMap = new Dictionary <int, int>(); if (newLocations.Any()) { foreach (var loc in import.Locations) { var dbMatch = newLocations.First(nl => nl.LocationName == loc.LocationName); locationMap.Add(loc.LocationId, dbMatch.Id); } } //enter transactions int transCounter = 0; foreach (var t in import.Transactions) { transCounter++; if (transCounter % 50 == 0) { Log.Information($"ImportUser: on transaction {transCounter}"); } var foo = import.TransactionKeywords.Where(tk => tk.TransactionId == t.Id); var newTransKeywords = foo.Select(f => keywordMap[f.KeywordId]).ToArray(); var newTrans = new TransactionCreateIn { TransDate = t.TransDate.Substring(0, 10), Amount = t.Amount, LocationId = locationMap[t.LocationId], CurrencyId = ccyMap[t.CurrencyId], CategoryId = categoryMap[t.CategoryId], Title = t.Title, Memo = t.Memo, PaidWithCash = t.PaidWithCash, UserId = request.UserId, KeywordIds = newTransKeywords }; await mediator.Send(new CreateTransaction.Command(newTrans, request.UserId)).ConfigureAwait(false); } //enter cash withdrawals foreach (var cw in import.CashWithdrawals) { var newCashWithdrawal = new CashWithdrawalCreateIn { Title = cw.Title, TransDate = cw.TransDate, Amount = cw.Amount, CurrencyId = ccyMap[cw.CurrencyId], Memo = cw.Memo, UserId = request.UserId }; await mediator.Send(new CreateCashWithdrawal.Command(newCashWithdrawal, request.UserId)).ConfigureAwait(false); } }
public Command(CashWithdrawalCreateIn cashWithdrawalIn, int tokenUserId) { CashWithdrawalIn = cashWithdrawalIn; TokenUserId = tokenUserId; }