Exemple #1
0
        public static void Parse(out List <ASXListedCompany> stocks, string csvfile)
        {
            stocks = new List <ASXListedCompany>();
            StringReader reader     = new StringReader(csvfile);
            string       firstLine  = reader.ReadLine();
            string       headerLine = null;

            if (firstLine == null || !firstLine.StartsWith("ASX listed companies as at") ||
                reader.ReadLine() == null || (headerLine = reader.ReadLine()) == null ||
                !headerLine.Equals("Company name,ASX code,GICS industry group"))
            {
                return;
            }
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                ASXListedCompany c        = new ASXListedCompany();
                string[]         strStock = line.Split(',');
                c.Name          = strStock[0].Trim('\"');
                c.Code          = strStock[1].Trim('\"');
                c.IndustryGroup = strStock[2].Trim('\"');
                stocks.Add(c);
            }
        }
        public static ASXListedCompany FindCompanyRecord(StocksDB stocksDB, ASXListedCompany company)
        {
            ASXListedCompany companyRecord = null;

            if (stocksDB is null)
            {
                stocksDB = new StocksDB();
            }
            string collectionName = "ASXListedCompanies";
            var    collection     = stocksDB.GetCollection <ASXListedCompany>(collectionName);
            var    filter         = Builders <ASXListedCompany> .Filter.And(
                Builders <ASXListedCompany> .Filter.Eq("Code", company.Code),
                Builders <ASXListedCompany> .Filter.Eq("Name", company.Name),
                Builders <ASXListedCompany> .Filter.Eq("IndustryGroup", company.IndustryGroup));

            try
            {
                companyRecord = collection.Find <ASXListedCompany>(filter).First();
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            return(companyRecord);
        }
        public static void ProcessASXListedCompaniesBatch(List <ASXListedCompany> stocks)
        {
            StocksDB stocksDB = new StocksDB();
            ASXListedCompanyBatch batch;
            bool batchStated = StartUpdateASXListedCompaniesBatch(stocksDB, out batch);

            if (!batchStated)
            {
                return;
            }

            foreach (ASXListedCompany company in stocks)
            {
                ASXListedCompany companyRecord = FindCompanyRecord(stocksDB, company);
                if (companyRecord != null)
                {
                    companyRecord.Dates.UpdateStartedDate = batch.Dates.UpdateStartedDate;
                }
                else
                {
                    companyRecord                         = company;
                    companyRecord.Id                      = ObjectId.GenerateNewId();
                    companyRecord.Dates                   = new Dates();
                    companyRecord.Dates.CreatedDate       = batch.Dates.UpdateStartedDate;
                    companyRecord.Dates.UpdateStartedDate = companyRecord.Dates.CreatedDate;
                }

                SaveCompanyRecord(stocksDB, companyRecord);
            }

            MarkBatchCompleted(stocksDB, batch);
        }
        public static bool SaveCompanyRecord(StocksDB stocksDB, ASXListedCompany company)
        {
            if (stocksDB is null)
            {
                stocksDB = new StocksDB();
            }
            string collectionName = "ASXListedCompanies";
            var    collection     = stocksDB.GetCollection <ASXListedCompany>(collectionName);
            var    filter         = Builders <ASXListedCompany> .Filter.Eq("Id", company.Id);

            try
            {
                ReplaceOneResult replaceResult = collection.ReplaceOne(filter, company, new UpdateOptions {
                    IsUpsert = true
                });
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(false);
            }
            return(true);
        }