public async Task <Domain.Models.Trade> AddAsync(Domain.Models.Trade trade, Domain.Models.Portfolio portfolio)
        {
            if (trade.Id != 0)
            {
                throw new ArgumentException("Trade already exists.");
            }

            using var context = new mmpproject2Context(_contextOptions);
            var dbPortfolio = await context.Portfolios
                              .Include(p => p.Trades)
                              .FirstAsync(p => p.Id == portfolio.Id);

            var newTrade = Mapper.MapTrade(trade);

            dbPortfolio.Trades.Add(newTrade);
            context.Trades.Add(newTrade);

            await context.SaveChangesAsync();

            var created = await context.Trades
                          .Include(t => t.Stock)
                          .FirstAsync(t => t.Id == newTrade.Id);

            return(Mapper.MapTrade(created));
        }
Example #2
0
 /// <summary>
 /// Maps a business domain portfolio to an EntityFramework model
 /// </summary>
 /// <param name="portfolio">The business domain portfolio object</param>
 /// <returns>The portfolio DataAccess object</returns>
 public static Models.Portfolio MapPortfolio(Domain.Models.Portfolio portfolio)
 {
     return(new Models.Portfolio {
         Id = portfolio.Id,
         Name = portfolio.Name,
         Funds = portfolio.Funds
     });
 }
Example #3
0
        public async Task <IEnumerable <Domain.Models.Asset> > GetAllAsync(Domain.Models.Portfolio portfolio)
        {
            using var context = new mmpproject2Context(_contextOptions);
            var assets = await context.Assets.Where(a => a.PortfolioId == portfolio.Id)
                         .Include(a => a.Stock)
                         .ToListAsync();

            return(assets.Select(Mapper.MapAsset));
        }
        public async Task <IEnumerable <Domain.Models.Trade> > GetAllAsync(Domain.Models.Portfolio portfolio)
        {
            using var context = new mmpproject2Context(_contextOptions);
            var trades = await context.Trades.Where(t => t.PortfolioId == portfolio.Id)
                         .Include(t => t.Stock)
                         .ToListAsync();

            return(trades.Select(Mapper.MapTrade));
        }
        public async Task UpdateAsync(Domain.Models.Portfolio portfolio)
        {
            using var context = new mmpproject2Context(_contextOptions);
            var current = await context.Portfolios.FindAsync(portfolio.Id);

            var updated = Mapper.MapPortfolio(portfolio);

            updated.UserId = current.UserId;
            context.Entry(current).CurrentValues.SetValues(updated);

            await context.SaveChangesAsync();
        }
        public async Task <Domain.Models.Portfolio> AddAsync(Domain.Models.Portfolio portfolio, Domain.Models.User user)
        {
            if (portfolio.Id != 0)
            {
                throw new ArgumentException("Portfolio already exists.");
            }

            using var context = new mmpproject2Context(_contextOptions);
            var dbUser = await context.Users
                         .Include(u => u.Portfolios)
                         .FirstOrDefaultAsync(u => u.Id == user.Id);

            var newPortfolio = Mapper.MapPortfolio(portfolio);

            dbUser.Portfolios.Add(newPortfolio);
            context.Portfolios.Add(newPortfolio);

            await context.SaveChangesAsync();

            return(Mapper.MapPortfolio(newPortfolio));
        }
Example #7
0
        public async Task <Domain.Models.Asset> AddAsync(Domain.Models.Asset asset, Domain.Models.Portfolio portfolio)
        {
            if (asset.Id != 0)
            {
                throw new ArgumentException("Asset already exists.");
            }
            using var context = new mmpproject2Context(_contextOptions);
            var dbPortfolio = await context.Portfolios
                              .Include(p => p.Assets)
                              .FirstAsync(p => p.Id == portfolio.Id);

            var newAsset = Mapper.MapAsset(asset);

            dbPortfolio.Assets.Add(newAsset);
            context.Assets.Add(newAsset);

            await context.SaveChangesAsync();

            var created = await context.Assets
                          .Include(a => a.Stock)
                          .FirstAsync(a => a.Id == newAsset.Id);

            return(Mapper.MapAsset(created));
        }