コード例 #1
0
ファイル: TickerDAO.cs プロジェクト: eoinr393/CompanyAPI
        /// <summary>
        /// Updates the tickers relating to a given companyId adding new ones and removing ones no longer associated with company
        /// </summary>
        /// <param name="companyId">the companyId to update</param>
        /// <param name="tickersNames">the names of the new tickers</param>
        /// <returns>List of updated tickers</returns>
        public async Task <List <Ticker> > UpdateCompanyTickers(int companyId, List <string> tickersNames)
        {
            List <Ticker> currentTickers = await this.GetTickersByCompanyId(companyId);

            List <Ticker> removedTickers = currentTickers.Where(c => !tickersNames.Contains(c.Name)).ToList();

            dbContext.RemoveRange(removedTickers);

            List <string> newTickerNames = tickersNames.Except(currentTickers.Select(ct => ct.Name)).ToList();
            List <Ticker> newTickers     = new List <Ticker>();

            foreach (string s in newTickerNames)
            {
                newTickers.Add(new Ticker
                {
                    Name      = s,
                    CompanyId = companyId
                });
            }

            await dbContext.AddRangeAsync(newTickers);

            dbContext.SaveChanges();

            return(await this.GetTickersByCompanyId(companyId));
        }
コード例 #2
0
        /// <summary>
        /// Updates the ExchangeIDs of a given CopmanyID
        /// </summary>
        /// <param name="companyId">the CompanyId</param>
        /// <param name="exchangeIds">the new exchange Id's that should be related to the companyId</param>
        /// <returns>The updated list of company exchanges</returns>
        public async Task <List <CompanyExchange> > UpdateCompanyExchanges(int companyId, List <int> exchangeIds)
        {
            //Get current existing
            var companyExchanges = await this.GetCompanyExchangesByCompanyId(companyId);

            //Exchange Ids in CompanyExchange that be removed as were deleted/overwritten
            List <CompanyExchange> removedCompanyExchanges = companyExchanges.Where(e => !exchangeIds.Contains(e.ExchangeId)).ToList();

            dbContext.RemoveRange(removedCompanyExchanges);

            //Newly added exchange Ids
            List <int>             newCompanyExchanges = exchangeIds.Where(e => !companyExchanges.Select(ce => ce.ExchangeId).ToList().Contains(e)).ToList();
            List <CompanyExchange> newExchanges        = new List <CompanyExchange>();

            foreach (int exchangeId in newCompanyExchanges)
            {
                newExchanges.Add(new CompanyExchange {
                    CompanyId = companyId, ExchangeId = exchangeId
                });
            }

            await dbContext.AddRangeAsync(newExchanges);

            await dbContext.SaveChangesAsync();

            return(await this.GetCompanyExchangesByCompanyId(companyId));
        }