public ActionResult Create([Bind(Include = "CategoryId,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "AuthorId,Name")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Ejemplo n.º 3
0
        public ActionResult Create([Bind(Include = "ImageId,Url,ArticleId")] Image image)
        {
            if (ModelState.IsValid)
            {
                db.Images.Add(image);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ArticleId = new SelectList(db.Articles, "ArticleId", "Title", image.ArticleId);
            return(View(image));
        }
Ejemplo n.º 4
0
        public ActionResult Create([Bind(Include = "ArticleId,Title,Content,PubDate,CategoryId,AuthorId")] Article article)
        {
            if (ModelState.IsValid)
            {
                db.Articles.Add(article);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AuthorId   = new SelectList(db.Authors, "AuthorId", "Name", article.AuthorId);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Description", article.CategoryId);
            return(View(article));
        }
Ejemplo n.º 5
0
        public ActionResult Index(string RSSURL)
        {
            // Tạo đối tượng WebClient để downl nội dung của 1 Url
            WebClient wclient = new WebClient();

            wclient.Encoding = Encoding.UTF8;
            string RSSDATA = wclient.DownloadString(RSSURL);
            // Ép kiểu sang xml và đọc các thông tin cơ bản của 1 rss feed
            XDocument xml         = XDocument.Parse(RSSDATA);
            var       RSSFeedData = (from x in xml.Descendants("item")
                                     select new RSSFeed
            {
                Title = ((string)x.Element("title")),
                Link = ((string)x.Element("link")),
                Description = ((string)x.Element("description")),
                PubDate = ((string)x.Element("pubDate"))
            });

            ViewBag.RSSFeed = RSSFeedData;
            ViewBag.URL     = RSSURL;

            try
            {
                foreach (var item in ViewBag.RSSFeed)
                {
                    string title   = item.Title;
                    var    article = (from a in db.Articles
                                      where a.Title == title
                                      select a).FirstOrDefault <Article>();
                    if (article != null)
                    {
                        // neu da ton tai bao bai thi continue sang buoc lap ke tiep
                        continue;
                    }
                    else
                    {
                        string s = wclient.DownloadString(item.Link);

                        var content = s.Substring(
                            s.IndexOf("<div class=\"content\">"),
                            s.IndexOf("<div class=\"author\">") - s.IndexOf("<div class=\"content\">"));
                        var pic = s.Substring(
                            s.IndexOf("<figure class=\"image\""),
                            s.IndexOf("</figure>") - s.IndexOf("<figure class=\"image\"")
                            );
                        var image = pic.Substring(
                            pic.IndexOf("<img"),
                            pic.IndexOf("<figcaption>") - pic.IndexOf("<img")
                            );
                        var url = image.Substring(
                            image.IndexOf("http"),
                            image.IndexOf("\">") - image.IndexOf("http")
                            );

                        // Them vao csdl -> Category -> Author -> Article -> Image
                        var cate = db.Categories.Where(c => c.Description == "RSS")
                                   .FirstOrDefault <Category>();
                        var aut = db.Authors.Where(a => a.Name == "Đ.H Bửu")
                                  .FirstOrDefault <Author>();
                        Article art = new Article();
                        art.AuthorId   = aut.AuthorId;
                        art.CategoryId = cate.CategoryId;
                        art.PubDate    = DateTime.UtcNow;
                        art.Content    = content;
                        art.Title      = item.Title;
                        db.Articles.Add(art);
                        db.SaveChanges();

                        Image im = new Image();
                        im.ArticleId = art.ArticleId;
                        im.Url       = url;
                        db.Images.Add(im);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {
                return(RedirectToAction("Index", "Articles"));
            }

            return(RedirectToAction("Index", "Articles"));
        }