Beispiel #1
0
        public async Task <PortfolioVm> Handle(GetPortfolioRequest request, CancellationToken cancellationToken)
        {
            var portfolio      = new PortfolioVm();
            var holdingsResult = new List <HoldingVm>();

            var holdings = _context.Holdings
                           .Where(x => x.UserId == _identityService.UserId)
                           .OrderByDescending(x => x.Created)
                           .ToList();

            foreach (var holding in holdings)
            {
                holding.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));
                holding.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
                holdingsResult.Add(_mapper.Map <HoldingVm>(holding));
            }

            portfolio.TotalShares          = holdings.Sum(h => h.Shares);
            portfolio.TotalAverageCost     = holdings.Average(h => h.PurchasePrice);
            portfolio.TotalCostBasis       = holdings.Sum(h => h.CostBasis);
            portfolio.TotalMarketValue     = holdings.Sum(h => h.MarketValue);
            portfolio.TotalGainLoss        = holdings.Sum(h => h.GainLoss);
            portfolio.TotalGainLossPercent = portfolio.TotalGainLoss / portfolio.TotalCostBasis;

            foreach (var holdingResult in holdingsResult)
            {
                holdingResult.Weight = holdingResult.CostBasis / portfolio.TotalMarketValue;
            }

            portfolio.Holdings = holdingsResult;

            return(portfolio);
        }
Beispiel #2
0
        public async Task <Unit> Handle(UpdateHoldingRequest request, CancellationToken cancellationToken)
        {
            var entity = await _context.Holdings.FindAsync(request.Id);

            var account = await _context.Accounts
                          .AsNoTracking()
                          .FirstAsync(x => x.Id == request.AccountId, cancellationToken: cancellationToken);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Holding), request.Id);
            }

            entity.Ticker        = request.Ticker;
            entity.PurchasePrice = request.PurchasePrice;
            entity.Shares        = request.Shares;
            entity.Account       = account ?? throw new NotFoundException(nameof(Account), request.AccountId);
            entity.SetCurrency(AbstractEnumeration.FromName <Currency>(request.Currency));

            if (request.PurchaseDate != null)
            {
                entity.Created = (DateTime)request.PurchaseDate;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #3
0
        public async Task <AccountVm> Handle(GetAccountByIdRequest byIdRequest, CancellationToken cancellationToken)
        {
            var account = await _context.Accounts
                          .AsNoTracking()
                          .FirstOrDefaultAsync(h => h.Id == byIdRequest.Id && h.UserId == _identityService.UserId, cancellationToken: cancellationToken);

            account.SetAccountType(AbstractEnumeration.FromValue <AccountType>(account.AccountTypeId));
            account.SetInstitution(AbstractEnumeration.FromValue <Institution>(account.InstitutionId));
            return(_mapper.Map <AccountVm>(account));
        }
Beispiel #4
0
        public async Task <HoldingVm> Handle(GetHoldingByIdRequest request, CancellationToken cancellationToken)
        {
            var holding = await _context.Holdings
                          .AsNoTracking()
                          .Include(x => x.Account)
                          .FirstOrDefaultAsync(h => h.Id == request.Id && h.UserId == _identityService.UserId, cancellationToken: cancellationToken);

            holding?.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
            holding?.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));

            return(_mapper.Map <HoldingVm>(holding));
        }
Beispiel #5
0
        public async Task <Guid> Handle(CreateAccountRequest request, CancellationToken cancellationToken)
        {
            var entity = new Account(request.Name,
                                     AbstractEnumeration.FromValue <Institution>(request.Institution),
                                     AbstractEnumeration.FromValue <AccountType>(request.Type));

            _context.Accounts.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Beispiel #6
0
        public async Task <Unit> Handle(UpdateAccountRequest request, CancellationToken cancellationToken)
        {
            var entity = await _context.Accounts.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Account), request.Id);
            }

            entity.Name = request.Name;
            entity.SetAccountType(AbstractEnumeration.FromValue <AccountType>(request.Type));
            entity.SetInstitution(AbstractEnumeration.FromValue <Institution>(request.Institution));

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #7
0
        public async Task <List <AccountVm> > Handle(GetAccountsRequest request, CancellationToken cancellationToken)
        {
            var results = new List <AccountVm>();

            var accounts = _context.Accounts
                           .Where(x => x.UserId == _identityService.UserId)
                           .OrderBy(x => x.Name)
                           .ToList();

            foreach (Account account in accounts)
            {
                account.SetAccountType(AbstractEnumeration.FromValue <AccountType>(account.AccountTypeId));
                account.SetInstitution(AbstractEnumeration.FromValue <Institution>(account.InstitutionId));
                results.Add(_mapper.Map <AccountVm>(account));
            }

            return(await Task.Run(() => results.ToList(), cancellationToken));
        }
Beispiel #8
0
        public async Task <List <HoldingVm> > Handle(GetHoldingsRequest request, CancellationToken cancellationToken)
        {
            var results = new List <HoldingVm>();

            var holdings = _context.Holdings
                           .Where(x => x.UserId == _identityService.UserId)
                           .OrderByDescending(x => x.Created)
                           .ToList();

            foreach (var holding in holdings)
            {
                holding.SetCurrency(AbstractEnumeration.FromValue <Currency>(holding.CurrencyId));
                holding.SetAccountDetails(await _holdingsService.GetDetailsByTickerAsync(holding.Ticker));
                results.Add(_mapper.Map <HoldingVm>(holding));
            }

            return(await Task.Run(() => results.ToList(), cancellationToken));
        }
Beispiel #9
0
        public async Task <Guid> Handle(CreateHoldingRequest request, CancellationToken cancellationToken)
        {
            var account = await _context.Accounts
                          .FirstOrDefaultAsync(x => x.Id == request.AccountId, cancellationToken : cancellationToken);

            if (account == null)
            {
                throw new Exception();
            }

            var entity = new Holding(request.Ticker, request.PurchasePrice, request.Shares, account,
                                     request.PurchaseDate, AbstractEnumeration.FromName <Currency>(request.Currency));

            entity.DomainEvents.Add(new HoldingPurchasedEvent(entity));

            _context.Holdings.Add(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }