Exemplo n.º 1
0
        public ActionResult AddTagToPost(PostViewModel model)
        {
            var           post     = _blogRepository.GetPostById(model.ID);
            var           postTags = _blogRepository.GetPostTags(post);
            List <string> pTagIds  = new List <string>();

            foreach (var pTag in postTags)
            {
                pTagIds.Add(pTag.Id);
            }
            var           newTags = model.Tags.Where(x => x.Checked == true).ToList();
            List <string> nTagIds = new List <string>();

            foreach (var pTag in newTags)
            {
                nTagIds.Add(pTag.Id);
            }
            if (!pTagIds.SequenceEqual(nTagIds))
            {
                foreach (var pTag in postTags)
                {
                    _blogRepository.RemovePostTags(model.ID, pTag.Id);
                }
                foreach (var tag in model.Tags)
                {
                    PostTag postTag = new PostTag();
                    if (tag.Checked == true)
                    {
                        postTag.PostId  = model.ID;
                        postTag.TagId   = tag.Id;
                        postTag.Checked = true;
                        _blogRepository.AddPostTags(postTag);
                    }
                }
                _blogRepository.Save();
            }
            return(RedirectToAction("EditPost", new { slug = post.UrlSeo }));
        }