Exemple #1
0
        public ActionResult editPost(int id)
        {
            Post post = db.postRepository.FindByID(id);

            if (post == null)
            {
                return(RedirectToAction("ListPost"));
            }
            newPostViewModel model = new newPostViewModel();

            if (post.tbl_User.username == User.Identity.Name || User.IsInRole("admin"))
            {
                Dynasty dn;
                Enum.TryParse(post.dynasty, out dn);
                Rated rated;
                Enum.TryParse(post.Rated.ToString(), out rated);
                PostType type;
                Enum.TryParse(post.post_type.ToString(), out type);
                List <SelectListItem> tag = PostData.getTagList();
                //string[] oldtag = post.post_tag.Split(',');
                foreach (var x in tag)
                {
                    foreach (var t in post.Tbl_Tags)
                    {
                        if (x.Value == t.TagID.ToString())
                        {
                            x.Selected = true;
                        }
                    }
                }
                model = new newPostViewModel
                {
                    post_id      = post.post_id,
                    dynasty      = dn,
                    post_content = post.post_content,
                    AvatarImage  = post.AvatarImage,
                    post_review  = post.post_review,
                    changeAvatar = false,
                    imagepath    = "/images/slides/" + post.post_id,
                    Rated        = rated,
                    Status       = post.status,
                    post_tag     = tag,
                    post_teaser  = post.post_teaser,
                    post_title   = post.post_title,
                    post_type    = type,
                };
                return(View(model));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Exemple #2
0
        public ActionResult newPost()
        {
            newPostViewModel model = new newPostViewModel
            {
                post_type    = PostType.Normal,
                post_tag     = PostData.getTagList(), //PostData.getTagList(),
                Status       = true,
                changeAvatar = true,
            };

            return(View(model));
        }
Exemple #3
0
        public ActionResult editPost(newPostViewModel model)
        {
            List <Tag> taglist = new List <Tag>();

            taglist.AddRange(model.post_tag.Where(m => m.Selected)
                             .Select(m => new Tag {
                TagID = int.Parse(m.Value), TagName = m.Text
            })
                             );
            List <Tag> untaglist = new List <Tag>();

            untaglist.AddRange(model.post_tag.Where(m => m.Selected == false)
                               .Select(m => new Tag {
                TagID = int.Parse(m.Value), TagName = m.Text
            })
                               );
            if (ModelState.IsValid && taglist.Count > 0)
            {
                // Nếu như teaser trống hoặc quá ngắn, sẽ dùng content thay thế từ content bài viết
                if (model.post_teaser == null || model.post_teaser.Length <= 20)
                {
                    model.post_teaser = CommonFunction.GetTeaserFromContent(model.post_content, 200);
                }
                //Upload ảnh và lưu ảnh với slug trùng tên tiêu đề bài viết
                bool isSavedSuccessfully = true;

                try
                {
                    if (model.changeAvatar == true && model.avatarFile != null && model.avatarFile.ContentLength > 0)
                    {
                        string subPath = Server.MapPath("~/Upload/images/");
                        bool   exists  = System.IO.Directory.Exists(subPath);
                        if (!exists)
                        {
                            System.IO.Directory.CreateDirectory(subPath);
                        }
                        //xóa ảnh cũ
                        bool exist = System.IO.File.Exists(Server.MapPath("~/Upload/images/" + model.AvatarImage));
                        if (exist)
                        {
                            System.IO.File.Delete(Server.MapPath("~/Upload/images/" + model.AvatarImage));
                        }
                        // lưu ảnh mới với tên là slug tiêu đề mới
                        string extension = Path.GetExtension(model.avatarFile.FileName);
                        model.AvatarImage = SlugGenerator.SlugGenerator.GenerateSlug(model.post_title) + "-" + model.post_id + extension;
                        model.avatarFile.SaveAs(Server.MapPath("~/Upload/images/") + model.AvatarImage);
                    }
                }
                catch (Exception ex)
                {
                    isSavedSuccessfully = false;
                }
                if (isSavedSuccessfully)
                {
                    Post pOST = db.postRepository.FindByID(model.post_id);
                    if (pOST.tbl_User.username != User.Identity.Name)
                    {
                        return(RedirectToAction("ListPost"));
                    }
                    pOST.dynasty      = model.dynasty.ToString();
                    pOST.edit_date    = DateTime.Now;
                    pOST.AvatarImage  = model.AvatarImage;
                    pOST.post_content = model.post_content;
                    pOST.post_review  = model.post_review;
                    pOST.post_title   = model.post_title;
                    pOST.post_type    = (int)model.post_type;
                    pOST.Rated        = (int)model.Rated;
                    pOST.post_teaser  = model.post_teaser;
                    pOST.status       = model.Status;
                    pOST.post_tag     = model.meta_tag;
                    foreach (var i in taglist)
                    {
                        Tag tags = db.tagRepository.FindByID(i.TagID);
                        if (!pOST.Tbl_Tags.Contains(tags))
                        {
                            pOST.Tbl_Tags.Add(tags);
                            tags.Tbl_POST.Add(pOST);
                        }
                        //else if(pOST.Tbl_Tags.Contains(tags))
                        //{
                        //    pOST.Tbl_Tags.Remove(tags);
                        //    tags.Tbl_POST.Remove(pOST);
                        //}
                    }
                    foreach (var i in untaglist)
                    {
                        Tag tags = db.tagRepository.FindByID(i.TagID);
                        if (pOST.Tbl_Tags.Contains(tags))
                        {
                            pOST.Tbl_Tags.Remove(tags);
                            tags.Tbl_POST.Remove(pOST);
                        }
                    }
                    if (model.UpdateSlug)
                    {
                        string slug = SlugGenerator.SlugGenerator.GenerateSlug(model.post_title.ToLower());
                        pOST.post_slug = slug;
                        if (db.postRepository.AllPosts().Any(m => m.post_slug == slug))
                        {
                            pOST.post_slug = slug + "-" + 1;
                        }
                    }
                    db.postRepository.UpdatePost(pOST);
                    db.Commit();
                    if (model.post_type.Equals(PostType.Slide))
                    {
                        return(View("UploadImage", new uploadViewModel {
                            id = pOST.post_id, title = pOST.post_title
                        }));
                    }
                }

                return(RedirectToAction("ListPost"));
            }
            newPostViewModel nmodel = new newPostViewModel
            {
                post_type = PostType.Normal,
                post_tag  = PostData.getTagList(),
            };

            return(View(nmodel));
        }
Exemple #4
0
        public ActionResult newPost(newPostViewModel model)
        {
            List <Tag> taglist = new List <Tag>();

            taglist.AddRange(model.post_tag.Where(m => m.Selected)
                             .Select(m => new Tag {
                TagID = int.Parse(m.Value), TagName = m.Text
            })
                             );

            if (ModelState.IsValid && taglist.Count > 0)
            {
                // Nếu như teaser trống hoặc quá ngắn, sẽ dùng content thay thế từ content bài viết
                if (model.post_teaser == null || model.post_teaser.Length <= 20)
                {
                    model.post_teaser = CommonFunction.GetTeaserFromContent(model.post_content, 200);
                }
                //Upload ảnh và lưu ảnh với slug trùng tên tiêu đề bài viết
                bool isSavedSuccessfully = true;
                try
                {
                    if (model.avatarFile != null && model.avatarFile.ContentLength > 0)
                    {
                        string subPath = Server.MapPath("~/Upload/images/");
                        bool   exists  = System.IO.Directory.Exists(subPath);
                        if (!exists)
                        {
                            System.IO.Directory.CreateDirectory(subPath);
                        }
                        string extension = Path.GetExtension(model.avatarFile.FileName);
                        model.AvatarImage = SlugGenerator.SlugGenerator.GenerateSlug(model.post_title) + "-" + new Random().Next(1, 100) + extension;
                        model.avatarFile.SaveAs(Server.MapPath("~/Upload/images/") + model.AvatarImage);
                    }
                }
                catch (Exception ex)
                {
                    isSavedSuccessfully = false;
                }
                if (isSavedSuccessfully == true)
                {
                    User user = db.userRepository.FindByUsername(User.Identity.Name);
                    Post pOST = new Post
                    {
                        userid       = user.userid,
                        dynasty      = model.dynasty.ToString(),
                        create_date  = DateTime.Now,
                        AvatarImage  = model.AvatarImage,
                        post_content = model.post_content,
                        post_review  = model.post_review,
                        post_title   = model.post_title,
                        post_type    = (int)model.post_type,
                        ViewCount    = 0,
                        Rated        = (int)model.Rated,
                        post_teaser  = model.post_teaser,
                        status       = model.Status,
                        post_tag     = model.meta_tag,
                    };
                    foreach (var i in taglist)
                    {
                        Tag tags = db.tagRepository.FindByID(i.TagID);
                        pOST.Tbl_Tags.Add(tags);
                        tags.Tbl_POST.Add(pOST);
                    }
                    string slug = SlugGenerator.SlugGenerator.GenerateSlug(pOST.post_title.ToLower());
                    pOST.post_slug = slug;
                    if (db.postRepository.AllPosts().Any(m => m.post_slug == slug))
                    {
                        pOST.post_slug = slug + "-" + 1;
                    }
                    db.postRepository.AddPost(pOST);
                    db.Commit();
                    if (model.post_type.Equals(PostType.Slide))
                    {
                        return(View("UploadImage", new uploadViewModel {
                            id = pOST.post_id, title = pOST.post_title
                        }));
                    }
                    return(RedirectToAction("ListPost"));
                }
            }
            // nếu lỗi thì đẩy về tiếp
            //newPostViewModel nmodel = new newPostViewModel
            //{
            //    post_type = PostType.Normal,
            //    post_tag = PostData.getTagList(),
            //};

            return(View(model));
        }