Exemplo n.º 1
0
        public ActionResult Update(Blog x)
        {
            if (ModelState.IsValid)
            {
                Blog xdb = db.Blog.Include("BlogToCategory").Include("BlogToTags").FirstOrDefault(b => b.Id == x.Id);

                xdb.Title           = x.Title;
                xdb.Content         = x.Content;
                xdb.Description     = x.Description;
                xdb.ModifiedDate    = DateTime.Now;
                xdb.AdminModifiedId = (int)Session["AdminId"];
                xdb.enableComments  = x.enableComments;


                if (x.BlogCoverImageFile != null)
                {
                    string imageName = DateTime.Now.ToString("ssfff") + Regex.Replace(x.BlogCoverImageFile.FileName, "[^A-Za-z0-9.]", "");
                    string imagePath = Path.Combine(Server.MapPath("~/Uploads/"), imageName);

                    string oldImagePath = Path.Combine(Server.MapPath("~/Uploads/"), xdb.BlogCoverImage);
                    System.IO.File.Delete(oldImagePath);

                    x.BlogCoverImageFile.SaveAs(imagePath);
                    xdb.BlogCoverImage = imageName;
                }


                db.Entry(xdb).State = EntityState.Modified;
                db.SaveChanges();

                using (DBContext db = new DBContext())
                {
                    foreach (var item in db.BlogToCategory.Where(c => c.BlogId == xdb.Id))
                    {
                        db.BlogToCategory.Remove(item);
                    }

                    db.SaveChanges();
                }

                if (x.BlogCategoryId != null)
                {
                    foreach (var item in x.BlogCategoryId)
                    {
                        BlogToCategory bcat = new BlogToCategory();
                        bcat.BlogId         = xdb.Id;
                        bcat.BlogCategoryId = item;

                        db.BlogToCategory.Add(bcat);
                        db.SaveChanges();
                    }
                }

                using (DBContext db = new DBContext())
                {
                    foreach (var item in db.BlogToTags.Where(c => c.BlogId == xdb.Id))
                    {
                        db.BlogToTags.Remove(item);
                    }

                    db.SaveChanges();
                }

                if (x.TagId != null)
                {
                    foreach (var item in x.TagId)
                    {
                        BlogToTags bcat = new BlogToTags();
                        bcat.BlogId = xdb.Id;
                        bcat.TagId  = item;

                        db.BlogToTags.Add(bcat);
                        db.SaveChanges();
                    }
                }
                int    AdminId      = (int)Session["AdminId"];
                string ConnectionId = db.Admin.FirstOrDefault(c => c.Id == AdminId).ConnectionId;
                GlobalHost.ConnectionManager.GetHubContext <NotifyHub>().Clients.AllExcept(ConnectionId).modelUpdate(((Models.Admin)Session["Admin"]).FullName, DateTime.Now.ToString("HH:mm"), xdb.Title, "Blog");

                TempData["Update"] = "Update";
                return(RedirectToAction("Index"));
            }
            ViewBag.BlogPage = true;

            ViewBag.Category = db.BlogCategory.ToList();
            ViewBag.Tag      = db.Tags.ToList();



            TempData["Update-Error"] = "Update-Error";

            return(View(x));
        }
Exemplo n.º 2
0
        public ActionResult Create(Blog x)
        {
            int AdminId = (int)Session["AdminId"];

            ViewBag.BlogPage = true;

            if (ModelState.IsValid)
            {
                Blog xdb = new Blog();

                if (x.BlogCoverImageFile == null)
                {
                    xdb.BlogCoverImage = "Default2";
                }
                else
                {
                    string imageName = DateTime.Now.ToString("ddMMyyyyHHmmssfff") + Regex.Replace(x.BlogCoverImageFile.FileName, "[^A-Za-z0-9.]", "");
                    string imagePath = Path.Combine(Server.MapPath("~/Uploads/"), imageName);

                    x.BlogCoverImageFile.SaveAs(imagePath);
                    xdb.BlogCoverImage = imageName;
                }


                xdb.Title          = x.Title;
                xdb.AdminId        = x.AdminId;
                xdb.Content        = x.Content;
                xdb.Description    = x.Description;
                xdb.PostDate       = DateTime.Now;
                xdb.isActive       = false;
                xdb.enableComments = x.enableComments;
                if (db.AdminSettings.FirstOrDefault(c => c.AdminId == AdminId).alwaysActive)
                {
                    xdb.isActive = true;
                }
                else
                {
                    xdb.isActive = false;
                }

                db.Blog.Add(xdb);
                db.SaveChanges();

                if (x.BlogCategoryId != null)
                {
                    foreach (var item in x.BlogCategoryId)
                    {
                        BlogToCategory bcat = new BlogToCategory();
                        bcat.BlogId         = xdb.Id;
                        bcat.BlogCategoryId = item;

                        db.BlogToCategory.Add(bcat);
                    }
                }

                db.SaveChanges();

                if (x.TagId != null)
                {
                    foreach (var item in x.TagId)
                    {
                        BlogToTags btag = new BlogToTags();
                        btag.BlogId = xdb.Id;
                        btag.TagId  = item;

                        db.BlogToTags.Add(btag);
                    }
                }
                db.SaveChanges();
                TempData["Create"] = "Create";

                string ConnectionId = db.Admin.FirstOrDefault(c => c.Id == AdminId).ConnectionId;
                GlobalHost.ConnectionManager.GetHubContext <NotifyHub>().Clients.AllExcept(ConnectionId).modelCreate(((Models.Admin)Session["Admin"]).FullName, DateTime.Now.ToString("HH:mm"), xdb.Title, "Blog");

                return(RedirectToAction("Index", "Blog"));
            }
            ViewBag.Category = db.BlogCategory.ToList();
            ViewBag.Tag      = db.Tags.ToList();
            ViewBag.Admin    = (int)Session["AdminId"];
            ViewBag.BlogPage = true;

            TempData["Create-Error"] = "Create-Error";

            return(View(x));
        }