Example #1
0
 public void SaveState(State repo)
 {
     if (repo != null)
         DataContext.States.Add(repo);
 }
 public ActionResult UpdatePostalCodes()
 {
     var reader = new StreamReader(System.IO.File.OpenRead(Server.MapPath("~/Content/PostCodes.csv")));
     reader.ReadLine();
     while (!reader.EndOfStream)
     {
         string p = reader.ReadLine();
         if (p != null)
         {
             string[] postalData = p.Split(',');
             var postalCode = Convert.ToInt32(postalData[0]);
             var suburbName = postalData[1].Replace("\"", "");
             var stateName = postalData[2].Replace("\"", "");
             // Creating State
             var state = _postalRepo.GetState(stateName);
             if (state == null)
             {
                 state = new State
                 {
                     State_Name = stateName
                 };
                 _postalRepo.SaveState(state);
                 _postalRepo.SaveChanges();
             }
             var suburub = _postalRepo.GetSuburb(suburbName);
             if (suburub == null)
             {
                 suburub = new Suburb
                 {
                     StateId = state.StateId,
                     Suburb_Name = suburbName
                 };
                 _postalRepo.SaveSubUrb(suburub);
                 _postalRepo.SaveChanges();
             }
             var postCode = _postalRepo.GetPostCode(postalCode, suburub.SuburbId);
             if (postCode == null)
             {
                 postCode = new PostCode
                 {
                     PostCode1 = postalCode,
                     SuburbID = suburub.SuburbId
                 };
                 _postalRepo.SavePostCode(postCode);
                 _postalRepo.SaveChanges();
             }
         }
     }
     return Json("ok");
 }