Пример #1
0
        public ActionResult CreatePost(string title, string content, string tags, string link)
        {
            Post p = new Post();
            p.Title = title;
            p.Content = content;
            p.PublishDate = DateTime.UtcNow;
            p.StaticLink = link;
            p.Tags = new List<Tag>();
            p.Writer = Membership.GetUser().UserName;

            foreach (var tag in tags.Split(','))
            {                
                Tag t = _TagRepository.Find(tag.Trim());
                if (t == null)
                    t = new Tag { TagName = tag.Trim() };

                p.Tags.Add(t);
            }

            _PostRepository.Add(p);
            _PostRepository.Save();

            return View("Poster");
        }
Пример #2
0
        public ActionResult EditPost(long id, string title, string content, string tags, string link)
        {
            Post p = _PostRepository.Find(id);
            if (p == null)
                return HttpNotFound();
            p.Title = title;
            p.Content = content;
            p.PublishDate = DateTime.UtcNow;
            p.StaticLink = link;

            string[] newTagNames=tags.Split(',');
            IQueryable<Tag> removeTags = p.Tags.Where(x=>newTagNames.Count(y=>y == x.TagName)==0).AsQueryable();
            string[] addTags = newTagNames.Where(x=>p.Tags.Count(y=>y.TagName == x) == 0).ToArray();

            foreach (var tag in removeTags.ToList())
            {
                p.Tags.Remove(tag);
            }
            foreach (var tag in addTags)
            {
                Tag t = _TagRepository.Find(tag.Trim());
                if (t == null)
                    t = new Tag { TagName = tag.Trim() };

                p.Tags.Add(t);
            }

            _PostRepository.Save();

            return View("Poster", p);
        }