public ActionResult SavePost(EditPost p) { Blog blog = _db.Blogs.Find(p.BlogId); if (ModelState.IsValid) { //anti xss //p.Content = Sanitizer.GetSafeHtmlFragment(p.Content); byte[] fileData = null; string contentType = null; string fileName = null; if (p.PostId != 0) { Post pst = _db.Posts.Find(p.PostId); fileData = pst.File; contentType = pst.FileContentType; fileName = pst.FileName; pst = null; _db = new SlickBlogAppContext(); } Mapper.CreateMap<EditPost, Post>().ForMember(f => f.File, opt => opt.Ignore()); Post post = Mapper.Map<EditPost, Post>(p); Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey; UserInfo userInfo = _db.UserInfo.Single(ui => ui.UserGuid == userGuid); if (p.file != null) { if (p.file.ContentLength > 0) { Stream s = p.file.InputStream; byte[] appData = new byte[p.file.ContentLength + 1]; s.Read(appData, 0, p.file.ContentLength); post.File = appData; post.FileName = p.file.FileName; post.FileContentType = p.file.ContentType; } } else if (p.PostId != 0 && p.FileName!=null) { if (p.FileName.Equals(fileName)) { post.File = fileData; post.FileName = fileName; post.FileContentType = contentType; } } if (p.Tags != null) { List<Tag> tagList = new List<Tag>(); post.Tags = tagList; String[] tags = Regex.Split(p.Tags, ","); foreach (var tag in tags) { Tag blogTag; try { blogTag = _db.Tags.Single(t => t.TagName.Equals(tag)); } catch (InvalidOperationException) { blogTag = new Tag(); blogTag.TagName = tag; _db.Tags.Add(blogTag); } post.Tags.Add(blogTag); } } post.Author = userInfo; post.Blog = _db.Blogs.Find(p.BlogId); post.Published = false; post.PostDate = DateTime.Now; if (p.PostId != 0) { _db.Entry(post).State = EntityState.Modified; } else { _db.Posts.Add(post); } _db.SaveChanges(); //Mapper.CreateMap<Post, EditPost>(); //EditPost ep = Mapper.Map<Post, EditPost>(post); //ep.PostId = post.PostId; //ep.BlogId = p.BlogId; //ep.Address = p.Address; return Content(post.PostId.ToString()); } return Content("0"); }
public ActionResult PostEditor(String address, int? id) { Blog blog = _db.Blogs.Single(b => b.Address == address);//needs check if blog exists if (id != null) { Post post = blog.Posts.Single(p => p.PostId == id);//needs check if post exists Mapper.CreateMap<Post, EditPost>().ForMember(f => f.file, opt => opt.Ignore()).ForMember(f => f.Tags, opt => opt.Ignore()); ; EditPost ep = Mapper.Map<Post, EditPost>(post); //byte[] fileData; //string contentType; string fileName; if (post.File != null) { //fileData = post.File; //contentType = post.FileContentType; fileName = post.FileName; //ep.FileData = fileData; //ep.FileContentType = fileName; ep.FileName = fileName; } if (post.Tags.Count != 0) { StringBuilder sb = new StringBuilder(); sb.Append(post.Tags.First().TagName); if (post.Tags.Count > 1) { for (int i = 2; i < post.Tags.Count; i++) { sb.Append(","); sb.Append(post.Tags.ElementAt(i).TagName); } } ep.Tags = sb.ToString(); } ep.PostId = post.PostId; ep.BlogId = blog.BlogId; ep.Address = address; ep.BlogTitle = blog.Title; return View(ep); } else { EditPost post = new EditPost(); post.BlogId = blog.BlogId; post.Address = blog.Address; post.BlogTitle = blog.Title; return View(post); } }