Пример #1
0
        public void Update(ZIPEntities context)
        {
            var currentVersion = context.Versions.Where(a => a.Type == "ZIPCodeLookup").FirstOrDefault();
            if (currentVersion == null)
            {
                currentVersion = new Models.Version() { Type = "ZIPCodeLookup", LastUpdateAttempt = DateTime.Now, LastUpdateSuccess = DateTime.Parse("01/01/1900") };
                context.Versions.Add(currentVersion);
            }
            else
            {
                currentVersion.LastUpdateAttempt = DateTime.Now;
            }

            int itemsAdded = 0;
            int itemsModified = 0;
            string remoteFile = "http://www.unitedstateszipcodes.org/zip_code_database.csv";
            string localFile = AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + @"\download\zip_code_database.csv";

            var webClient = new WebClient();
            webClient.DownloadFile(remoteFile, localFile);

            using (var reader = new StreamReader(localFile))
            {
                while (reader.Peek() >= 0)
                {
                    var zipLine = reader.ReadLine();
                    if (zipLine != null)
                    {
                        List<ZIP> updatedZIPs = ParseZipLine(zipLine);
                        if (updatedZIPs != null)
                        {
                            foreach (var updatedZIP in updatedZIPs)
                            {
                                var existingZIP = context.ZIPs.Find(updatedZIP.ZIPCode);
                                if (existingZIP == null)
                                {
                                    itemsAdded++;
                                    context.ZIPs.Add(updatedZIP);
                                }
                                else
                                {
                                    itemsModified++;
                                    context.Entry<ZIP>(existingZIP).CurrentValues.SetValues(updatedZIP);
                                }
                            }
                        }
                    }
                }
            }

            currentVersion.LastUpdateItemsAdded = itemsAdded;
            currentVersion.LastUpdateItemsModified = itemsModified;
            currentVersion.LastUpdateSuccess = DateTime.Now;
            context.SaveChanges();
        }
Пример #2
0
 // GET update?password=secret
 public bool Get(string password)
 {
     if (password == "DCGeverm0re")
     {
         var context = new ZIPEntities();
         var processor = new UpdateProcessor();
         try
         {
             processor.Update(context);
         }
         catch (Exception ex)
         {
             return false;
         }
         return true;
     }
     return false;
 }