Пример #1
0
        public void AddFeed(Feed addfeed)
        {
            using (RssEntities addcontext = new RssEntities())
            {

                try
                {
                    addcontext.Feeds.AddObject(addfeed);
                    addcontext.SaveChanges();
                }
                catch (Exception e)
                {
                    string msg = e.Message;
                    string reason = "AddFeed Feeds.AddObject Exception";
                    throw new FaultException<DBRequestFault>
                        (new DBRequestFault(msg), reason);
                }
                try
                {
                    List<News> feedNews = LoadNewsFromFeed(addfeed);
                    foreach (News item in feedNews)
                    {
                        addcontext.News.AddObject(item);
                    }
                    addcontext.SaveChanges();
                }
                catch (Exception e)
                {
                    string msg = e.Message;
                    string reason = "AddFeed News.AddObjectt Exception";
                    throw new FaultException<DBRequestFault>
                        (new DBRequestFault(msg), reason);
                }
            }
        }
Пример #2
0
 public void AddFeed(string name, string url)
 {
     Feed newFeed = new Feed
     {
         ID = Guid.NewGuid(),
         Name = name,
         URL = url
     };
     try
     {
         context.Feeds.AddObject(newFeed);
         context.SaveChanges();
     }
     catch (Exception e)
     {
         string msg = e.Message;
         string reason = "AddFeed Feeds.AddObject Exception";
         throw new FaultException<DBRequestFault>
             (new DBRequestFault(msg), reason);
     }
     try
     {
         List<News> feedNews = LoadNewsFromFeed(newFeed);
         foreach (News item in feedNews)
         {
             context.News.AddObject(item);
         }
         context.SaveChanges();
     }
     catch (Exception e)
     {
         string msg = e.Message;
         string reason = "AddFeed News.AddObjectt Exception";
         throw new FaultException<DBRequestFault>
             (new DBRequestFault(msg), reason);
     }
 }
Пример #3
0
        private void UpdateFeed(Feed feed)
        {
            try
            {

                using (XmlReader reader = XmlReader.Create(feed.URL))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    //compare the item.LastbuildDate
                    int result = DateTime.Compare(feed.LastTimeUpdate, GetLastBuildDate(xmlDoc));
                    if (result < 0 || feed.News.Count == 0)
                    {
                        using (RssEntities localContext = new RssEntities())
                        {
                            Feed delFeed = localContext.Feeds.First(f => f.ID == feed.ID);
                            localContext.Feeds.DeleteObject(delFeed);
                            localContext.SaveChanges();

                            using (RssEntities addContext = new RssEntities())
                            {
                                Feed addfeed = new Feed
                                {
                                    ID = feed.ID,
                                    Name = feed.Name,
                                    URL = feed.URL
                                };
                                addContext.Feeds.AddObject(addfeed);
                                addContext.SaveChanges();
                                List<News> feedNews = LoadNewsFromFeed(addfeed);
                                foreach (News item in feedNews)
                                {
                                    addContext.News.AddObject(item);
                                }
                                addContext.SaveChanges();
                            }

                        }
                    }
                }
            }
            catch (FileNotFoundException fnfEx)
            {
                string msg = fnfEx.Message;
                string reason = "UpdateFeed FileNotFoundException";
                throw new FaultException<DBRequestFault>
                    (new DBRequestFault(msg), reason);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                string reason = "UpdateFeed Exception";
                throw new FaultException<DBRequestFault>
                    (new DBRequestFault(msg), reason);
            }
        }
Пример #4
0
        private List<News> LoadNewsFromFeed(Feed feed)
        {
            try
            {
                if (String.IsNullOrEmpty(feed.URL))
                {
                    throw new ArgumentException("You must provide a feed URL");
                }
                //start the parsing process
                using (XmlReader reader = XmlReader.Create(feed.URL))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    feed.LastTimeUpdate = GetLastBuildDate(xmlDoc);
                    //parse the item
                    List<News> news = ParseRssNewsItems(xmlDoc);
                    news.ForEach(el =>
                    {
                        el.FeedId = feed.ID;
                    });
                    return news;
                }

            }
            catch (ArgumentException argEx)
            {
                string msg = argEx.Message;
                string reason = "LoadNewsFromFeed ArgumentException";
                throw new FaultException<DBRequestFault>
                    (new DBRequestFault(msg), reason);
            }
        }
Пример #5
0
 /// <summary>
 /// Создание нового объекта Feed.
 /// </summary>
 /// <param name="id">Исходное значение свойства ID.</param>
 /// <param name="name">Исходное значение свойства Name.</param>
 /// <param name="uRL">Исходное значение свойства URL.</param>
 /// <param name="lastTimeUpdate">Исходное значение свойства LastTimeUpdate.</param>
 public static Feed CreateFeeds(global::System.Guid id, global::System.String name, global::System.String uRL, global::System.DateTime lastTimeUpdate)
 {
     Feed feeds = new Feed();
     feeds.ID = id;
     feeds.Name = name;
     feeds.URL = uRL;
     feeds.LastTimeUpdate = lastTimeUpdate;
     return feeds;
 }
Пример #6
0
 /// <summary>
 /// Устаревший метод для добавления новых объектов в набор EntitySet Feed. Взамен можно использовать метод .Add связанного свойства ObjectSet&lt;T&gt;.
 /// </summary>
 public void AddToFeeds(Feed feeds)
 {
     base.AddObject("Feeds", feeds);
 }