Beispiel #1
0
        public async Task AddAsync(string userId, string name, int currencyId)
        {
            var currency = await this.dbContext.Currencies.FirstOrDefaultAsync(c => c.Id == currencyId);

            if (currency == null)
            {
                throw new ArgumentException(GlobalConstants.CurrencyNotExist);
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = "Unnamed Wallet";
            }

            var investmentWallet = new InvestmentWallet
            {
                ApplicationUserId = userId,
                Name       = name,
                CurrencyId = currencyId,
                CreatedOn  = DateTime.UtcNow,
            };

            await this.dbContext.InvestmentWallets.AddAsync(investmentWallet);

            await this.dbContext.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task <InvestmentWalletTradesDto> GetTradesAsync(string userId, int investmentWalletId, int page, int itemsPerPage = 5)
        {
            InvestmentWallet investmentWallet = await this.dbContext.InvestmentWallets
                                                .Include(iw => iw.Trades)
                                                .ThenInclude(t => t.Company)
                                                .Include(iw => iw.Currency)
                                                .FirstOrDefaultAsync(iw => iw.Id == investmentWalletId);

            if (investmentWallet == null)
            {
                throw new ArgumentException(GlobalConstants.InvestmentWalletNotExist);
            }

            if (!await this.IsUserOwnInvestmentWalletAsync(userId, investmentWalletId))
            {
                throw new ArgumentException(GlobalConstants.NotPermissionsToEditInvestmentWallet);
            }

            var walletTrades = new InvestmentWalletTradesDto
            {
                Id                    = investmentWallet.Id,
                Name                  = investmentWallet.Name,
                CreatedOn             = investmentWallet.CreatedOn,
                TotalBuyTradesAmount  = investmentWallet.Trades.Where(t => t.Type == TradeType.Buy && t.Company.IsDeleted == false).Sum(t => t.Price * t.StockQuantity),
                TotalSellTradesAmount = investmentWallet.Trades.Where(t => t.Type == TradeType.Sell && t.Company.IsDeleted == false).Sum(t => t.Price * t.StockQuantity),
                TotalTradesCount      = investmentWallet.Trades.Count(),
                Currency              = new CurrencyInfoDto
                {
                    Name       = investmentWallet.Currency.Name,
                    CurrencyId = investmentWallet.CurrencyId,
                    Code       = investmentWallet.Currency.Code,
                },
                Trades = investmentWallet.Trades
                         .OrderByDescending(x => x.CreatedOn)
                         .Skip((page - 1) * itemsPerPage).Take(itemsPerPage)
                         .Select(t => new TradeDto
                {
                    Id                 = t.Id,
                    CreatedOn          = t.CreatedOn,
                    Price              = t.Price,
                    Type               = t.Type,
                    StockQuantity      = t.StockQuantity,
                    InvestmentWalletId = investmentWallet.Id,
                    Company            = new GetCompanyDto
                    {
                        Name   = t.Company.Name,
                        Id     = t.CompanyId,
                        Ticker = t.Company.Ticker,
                    },
                })
                         .ToList(),
            };

            return(walletTrades);
        }