public async Task <CommandResponse> Handle(Command request, CancellationToken cancellationToken) { var(records, err) = _parser.Parse <CoinbaseProRecord>(request.Content); if (err != null) { return(CommandResponse.Failed(err)); } try { var coinbaseProContainer = new CoinbaseProContainer(); coinbaseProContainer.AddRecords(records); foreach (var b in coinbaseProContainer.GetBuys()) { var cmd = new core.Cryptos.Handlers.Buy.Command { Date = b.Date, DollarAmount = b.DollarAmount, Quantity = b.Quantity, Token = b.Token }; cmd.WithUserId(request.UserId); await _mediator.Send(cmd); } foreach (var b in coinbaseProContainer.GetSells()) { var cmd = new core.Cryptos.Handlers.Sell.Command { Date = b.Date, DollarAmount = b.DollarAmount, Quantity = b.Quantity, Token = b.Token }; cmd.WithUserId(request.UserId); await _mediator.Send(cmd); } return(CommandResponse.Success()); } catch (Exception ex) { return(CommandResponse.Failed( $"Entry Failed: {ex.Message}" )); } }
private async Task <CommandResponse> ProcessLine(CryptoRecord record, Guid userId) { RequestWithUserId <CommandResponse> CreateBuy(CryptoRecord r) { var b = new core.Cryptos.Handlers.Buy.Command { Date = record.Timestamp, DollarAmount = record.USDSubtotal.Value, Quantity = record.QuantityTransacted.Value, Token = record.Asset }; b.WithUserId(userId); return(b); } RequestWithUserId <CommandResponse> CreateSell(CryptoRecord r) { var s = new core.Cryptos.Handlers.Sell.Command { Date = record.Timestamp, DollarAmount = record.USDSubtotal.Value, Quantity = record.QuantityTransacted.Value, Token = record.Asset }; s.WithUserId(userId); return(s); } RequestWithUserId <CommandResponse> CreateAward(CryptoRecord r) { var s = new core.Cryptos.Handlers.Reward.Command { Date = record.Timestamp, DollarAmount = record.USDSubtotal.Value, Quantity = record.QuantityTransacted.Value, Notes = record.Notes, Token = record.Asset }; s.WithUserId(userId); return(s); } RequestWithUserId <CommandResponse> CreateYield(CryptoRecord r) { var s = new core.Cryptos.Handlers.Yield.Command { Date = record.Timestamp, DollarAmount = record.USDSubtotal.Value, Quantity = record.QuantityTransacted.Value, Notes = record.Notes, Token = record.Asset }; s.WithUserId(userId); return(s); } RequestWithUserId <CommandResponse> cmd = null; switch (record.TransactionType.ToLower()) { case "buy": cmd = CreateBuy(record); break; case "sell": cmd = CreateSell(record); break; case "coinbase earn": cmd = CreateAward(record); break; case "rewards income": cmd = CreateYield(record); break; } try { if (cmd != null) { return(await _mediator.Send(cmd)); } else { return(CommandResponse.Success()); } } catch (Exception ex) { return(CommandResponse.Failed( $"Entry for {record.Asset}/{record.TransactionType}/{record.Timestamp.ToString("yyyy-MM-dd")} failed: {ex.Message}" )); } }