예제 #1
0
        public ActionResult Edit(PostEditViewModel postEditViewModel)
        {
            if (ModelState.IsValid)
            {
                postPresentationService.SavePostEditViewModel(postEditViewModel);
                return RedirectToAction("Index");
            }

            return View(postEditViewModel);
        }
예제 #2
0
 public void SavePostEditViewModel(PostEditViewModel postEditViewModel)
 {
     Post post = postRepository.Get(postEditViewModel.Id) ?? new Post
                                                                 {
                                                                     PostDate = DateTime.Now,
                                                                     HitCount = 1,
                                                                     Id = 0,
                                                                     Body = string.Empty,
                                                                     Name = "New post",
                                                                     Tags = new Collection<Tag>(),
                                                                 };
     post.Name = postEditViewModel.Name;
     post.Body = postEditViewModel.Body;
     post.Tags.Clear();
     postEditViewModel.Tags = postEditViewModel.Tags ?? string.Empty;
     var tagNames = postEditViewModel.Tags.Split(',').Select(s => s.Replace(" ", string.Empty)).Where(s => !string.IsNullOrWhiteSpace(s));
     foreach (string tagName in tagNames)
     {
         var tag = tagRepository.Get(tagName) ?? new Tag() {Name = tagName, Posts = new HashSet<Post>()};
         post.Tags.Add(tag);
     }
     postRepository.Save(post);
     tagRepository.DeleteUnusedTags();
 }