public ActionResult PostAdd(int? id, Blog.Data.Model.User user) { var postView = new PostCreateEditView { Id = -1, CategoryId = -1, Author = user.Login, MainImage = Url.Content("~/Content/img/post_defult.jpg") }; if (id.HasValue) { var post = UoW.Repository<Post>().GetById(id.Value); postView = Mapper.Map<Post, PostCreateEditView>(post); postView.CategoryId = post.Category.Id; postView.CurrentTags = JsonConvert.SerializeObject(post.Tag.Select(x => x.Text).ToArray()); } postView.CurrentCategories = UoW.Repository<Category>().Get().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Title, Selected = postView.CategoryId > -1 && postView.CategoryId == x.Id }).ToList(); var alltags = UoW.Repository<Tag>().Get().Select(x => x.Text).ToList(); postView.AllTags = JsonConvert.SerializeObject(alltags); return View(postView); }
public ActionResult PostSaveOrUpdate(PostCreateEditView pv) { if (ModelState.IsValid) { var post = Mapper.Map<PostCreateEditView, Post>(pv); post.Category = UoW.Repository<Category>().GetById(pv.CategoryId); post.Author = UoW.Repository<User>() .Get() .Single(x => x.Login.Equals(pv.Author, StringComparison.OrdinalIgnoreCase)); if (post.Id == -1) { post.CreateIn = DateTime.Now; UoW.Repository<Post>().Insert(post); } else UoW.Repository<Post>().Update(post); var tags = pv.Tags.Split(',', '.', '|'); foreach (var tagText in tags) { var tag = UoW.Repository<Tag>() .Get() .SingleOrDefault(x => x.Text.Equals(tagText, StringComparison.OrdinalIgnoreCase)); if (tag == null) { tag = new Tag { Text = tagText, Post = new Collection<Post>() }; tag.Post.Add(post); UoW.Repository<Tag>().Insert(tag); } else { tag.Post.Add(post); } } UoW.Save(); } return RedirectToAction("Index", "Admin"); }