public int AddBalance(PortfolioBalance balance)
 {
     PortfolioBalances.Add(balance);
     SaveChanges();
     return(balance.Id);
 }
Пример #2
0
        /// <summary>
        /// It creates a portfolio from the transactions.
        /// First line of fileStream has to be the initial balance.
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="portfolioname"></param>
        /// <param name="fileStream">It expects a CSV file. First line has to be the initial balance</param>
        /// <returns></returns>
        public bool CreatePortfolio(string userName, string portfolioname, Stream fileStream, bool firstRowIsInitalBalance, bool overrideIfExists, out Portfolio portfolio, out string message)
        {
            List <string[]> transactionsList = new List <string[]>();

            User user = portfoliosContext.GetUser(userName);

            if (user == null)
            {
                portfolio = null;
                message   = $"User '{userName}' doesn't exist.";
                return(false);
            }

            portfolio = portfoliosContext.GetPortfoliosByUserNameAndPortfolioName(userName, portfolioname);
            int portfolioId;

            if (overrideIfExists && portfolio != null)
            {
                portfoliosContext.DeletePortfolio(portfolio);
                portfolio = null;
            }

            if (portfolio == null)
            {
                portfolio   = portfoliosContext.CreatePortfolio(user, portfolioname);
                portfolioId = portfolio.Id;
            }
            else
            {
                portfolioId = portfolio.Id;
            }

            fileStream.Position = 0;
            using (StreamReader reader = new StreamReader(fileStream, System.Text.Encoding.UTF8, true))
            {
                //First row is Header
                if (reader.Peek() >= 0)
                {
                    string line = reader.ReadLine();
                }

                if (firstRowIsInitalBalance)
                {
                    if (reader.Peek() >= 0)
                    {
                        string           line   = reader.ReadLine();
                        string[]         fields = line.Split(',');
                        PortfolioBalance pb     = PortfolioBalance.From(fields);
                        if (portfolio.Balances.Where(b => b.TransactionCode == pb.TransactionCode).Any() == false)
                        {
                            pb.PortfolioId = portfolioId;
                            portfoliosContext.AddBalance(pb);//And it also adds the balance to the balances collection of the portfolio
                            portfolio.InitialBalance = pb.NetCashBalance;
                        }
                    }
                }
                else
                {
                    //TODO: Get InitialBalance from DataBase
                }

                //Second to end row are the transactions
                while (reader.Peek() >= 0)
                {
                    string   line   = reader.ReadLine();
                    string[] fields = line.Split(',');
                    transactionsList.Add(fields);
                    Transaction newTransaction = Transaction.From(fields);
                    if (newTransaction != null && portfolio.Transactions.Where(t => t.TransactionCode == newTransaction.TransactionCode).Any() == false)
                    {
                        newTransaction.UserId      = user.Id;
                        newTransaction.PortfolioId = portfolioId;
                        if (TryGetAsset(newTransaction.Symbol, out AssetBase asset))
                        {
                            if (string.IsNullOrEmpty(newTransaction.Symbol))
                            {
                                newTransaction.Symbol = asset.Ticker;
                            }
                            newTransaction.Asset = asset;
                        }
                        else
                        {
                            newTransaction.Asset = null;
                        }
                        portfolio.Transactions.Add(newTransaction);
                        //portfoliosContext.AddTransaction(newTransaction);//And it also adds the transaction to the transactions collection of the portfolio
                    }
                    else
                    {
                        //TODO: Inform to user that transaction wasn't saved
                    }
                }
            }

            CalculateAssetAllocations(portfolio);
            portfoliosContext.Update(portfolio);

            message = $"Portfilio '{portfolioname}' created successfuly.";
            return(true);
        }