public async Task <IActionResult> GetAll() { //Get all company info var allCompanies = await companyDAO.GetCompanys(); //Load up Ticker and Exchange data List <CompanyModel> companyInfo = new List <CompanyModel>(); foreach (Company.DataAccess.Models.Company c in allCompanies) { var tickers = await tickerDAO.GetTickersByCompanyId(c.Id); var companyExchanges = await companyExchangeDAO.GetCompanyExchangesByCompanyId(c.Id); var exchanges = new List <Company.DataAccess.Models.Exchange>(); foreach (var ce in companyExchanges) { exchanges.Add(await exchangeDAO.GetExchangeById(ce.ExchangeId)); } companyInfo.Add(CompanyMapper.DAOToAPIModel(c, exchanges, tickers)); } if (companyInfo.Count != 0) { return(Ok(companyInfo)); } return(NotFound("No company exists yet.")); }
public async Task <IActionResult> GetByID(string id) { int.TryParse(id, out int companyId); var company = companyId > 0 ? await companyDAO.GetCompanyById(companyId) : await companyDAO.GetCompanyByISIN(id); if (company == null) { return(NotFound($"no company found for id: {id}")); } var tickers = await tickerDAO.GetTickersByCompanyId(company.Id); var companyExchanges = await companyExchangeDAO.GetCompanyExchangesByCompanyId(company.Id); var exchanges = new List <Company.DataAccess.Models.Exchange>(); foreach (var ce in companyExchanges) { exchanges.Add(await exchangeDAO.GetExchangeById(ce.ExchangeId)); } var companyInfo = CompanyMapper.DAOToAPIModel(company, exchanges, tickers); return(Ok(companyInfo)); }
public async Task <IActionResult> Create(CompanyModel company) { if (!this.ValidateISIN(company.ISIN)) { return(BadRequest("ISIN value must begin with two letters and be unique")); } //Create Company var newCompany = await companyDAO.CreateCompany(new Company.DataAccess.Models.Company { Name = company.Name, ISIN = company.ISIN, Website = company.Website }); //Create Tickers foreach (var ticker in company.Ticker) { await tickerDAO.CreateTicker(new Company.DataAccess.Models.Ticker { CompanyId = newCompany.Id, Name = ticker }); } //Create Exchanges and link to Copmany in join table var exchanges = new List <Company.DataAccess.Models.Exchange>(); foreach (var exchange in company.CompanyExchange) { var newExchange = await exchangeDAO.CreateExchange(new Company.DataAccess.Models.Exchange { Name = exchange }); exchanges.Add(newExchange); await companyExchangeDAO.CreateCompanyExchange(new Company.DataAccess.Models.CompanyExchange { CompanyId = newCompany.Id, ExchangeId = newExchange.Id }); } var companyInfo = CompanyMapper.DAOToAPIModel(newCompany, exchanges, newCompany.Ticker.ToList()); return(Ok(companyInfo)); }