예제 #1
0
        public void FillFromCSV(string filename)
        {
            IList <District> districts = new List <District>();
            IList <Report>   reports   = new List <Report>();

            reportDao.Drop();
            districtDao.Drop();

            AddUser("admin", "geheim");

            using (StreamReader reader = new StreamReader(@$ "{filename}"))
            {
                if (!reader.EndOfStream)
                {
                    reader.ReadLine();
                }

                while (!reader.EndOfStream)
                {
                    string   line               = reader.ReadLine();
                    string[] elements           = line.Split(';');
                    DateTime reportTime         = DateTime.Parse(elements[0]);
                    string   districtName       = elements[1];
                    long     stateId            = Convert.ToInt64(elements[2].Substring(0, 1));
                    long     districtId         = Convert.ToInt64(elements[2]);
                    long     districtPopulation = Convert.ToInt64(elements[3]);
                    long     curInfected        = Convert.ToInt64(elements[4]);
                    double   incidence          = Convert.ToDouble(elements[7]);
                    long     curDeceased        = Convert.ToInt64(elements[8]);
                    long     curRecovered       = Convert.ToInt64(elements[10]);

                    District curDistrict = new District
                    {
                        Id               = districtId,
                        Name             = districtName,
                        Population       = districtPopulation,
                        ResponsibleState = new State {
                            Id = stateId
                        }
                    };

                    if (!districts.Contains(curDistrict))
                    {
                        districts.Add(curDistrict);
                    }

                    Report curReport = new Report
                    {
                        Date = reportTime,
                        ResponsibleDistrict = curDistrict,
                        Incidence           = incidence,
                        Infected            = curInfected,
                        Recovered           = curRecovered,
                        Deceased            = curDeceased,
                        // user for first inputs = Admin
                        FromUser = new User {
                            Id = 0
                        }
                    };
                    reports.Add(curReport);
                }
            }


            foreach (District district in districts)
            {
                districtDao.Create(district);
            }
            foreach (Report report in reports)
            {
                reportDao.Create(report);
            }
        }