/// <summary> /// 添加Blog /// </summary> /// <param name="blog"></param> /// <returns></returns> public async Task <ResponseModel> AddBlog(AddBlog blog) { _context.Blogs.Add(new BlogInfo { Title = blog.Title, Abstr = blog.Abstr, CreateTime = DateTime.Now, CategoryId = blog.CategoryId, Content = new BContent { Content = blog.Content } }); var i = await _context.SaveChangesAsync(); if (i > 0) { return new ResponseModel() { code = 200, result = "Blog添加成功", data = "" } } ; else { return new ResponseModel() { code = 0, result = "Blog添加失败", data = "" } }; }
/// <summary> /// 修改blog /// </summary> /// <param name="id">blogId</param> /// <param name="blog"></param> /// <returns></returns> public async Task <ResponseModel> UpdateBlog(int id, AddBlog blog) { var result = await _context.Blogs.Include(x => x.Content).Where(x => x.Id == id).FirstOrDefaultAsync(); result.Title = blog.Title; result.Abstr = blog.Abstr; result.CategoryId = blog.CategoryId; result.Content.Content = blog.Content; //_context.Update(result); var i = await _context.SaveChangesAsync(); if (i > 0) { return new ResponseModel() { code = 200, result = "Blog修改成功", data = "" } } ; else { return new ResponseModel() { code = 0, result = "Blog修改失败", data = "" } }; }
public IActionResult AddBlog(AddBlog blog) { blog.blogPost.RemoveAll(x => string.IsNullOrEmpty(x.Title)); Foodzfame.Data.Entities.Blog blog1 = blog; blog1.Img = Utils.GetByteArrayFromImage(blog.Image); blog.blogPost.ForEach(x => x.Img = Utils.GetByteArrayFromImage(x.Image)); blog1.BlogPosts = blog.blogPost.ToList <BlogPost>(); _dbContext.Add(blog1); _dbContext.SaveChanges(); return(RedirectToAction("Blog")); }
public void Process_NoBlogs_DoesNotAddToModel() { // arrange var blogManager = Mock.Of <IBlogManager>(); var sut = new AddBlog(blogManager); var workflowPipelineArgs = WorkflowPipelineArgsFactory.CreateWorkflowPipelineArgs(); var args = new PopulateScribanMailActionModelArgs(workflowPipelineArgs); // act sut.Process(args); // assert var blogExists = args.ModelContains(AddBlog.ModelKey); Assert.That(blogExists, Is.False); }
public void Process_BlogPresent_AddsBlog() { // arrange var blogItem = ItemFactory.CreateItem(); var settings = Mock.Of <IWeBlogSettings>(); var blogManager = Mock.Of <IBlogManager>(x => x.GetCurrentBlog(It.IsAny <Item>()) == new BlogHomeItem(blogItem.Object) ); var sut = new AddBlog(blogManager); var workflowPipelineArgs = WorkflowPipelineArgsFactory.CreateWorkflowPipelineArgs(blogItem.Object); var args = new PopulateScribanMailActionModelArgs(workflowPipelineArgs); // act sut.Process(args); // assert var value = args.GetModel(AddBlog.ModelKey); Assert.That(((BlogHomeItem)value).ID, Is.EqualTo(blogItem.Object.ID)); }
public ActionResult CreateBlog(AddBlog addBlogModel) { if (ModelState.IsValid) { User user = _blogRepository.ListUsers.Where(u => u.Id == idAuthorizationUser()).FirstOrDefault(); BlogModel blog = _blogRepository.ListBlogs.Where(u => u.Title == addBlogModel.Title && u.Description == addBlogModel.Description && u.User == user).FirstOrDefault(); if (blog == null) { blog = new BlogModel { Title = addBlogModel.Title, Description = addBlogModel.Description, User = user, DateCreated = DateTime.Now }; _blogRepository.AddBlog(blog); blog = _blogRepository.ListBlogs.Where(u => u.Title == addBlogModel.Title && u.Description == addBlogModel.Description && u.User == user).FirstOrDefault(); if (blog != null) { return RedirectToAction("SuccessCreatedBlog", "Account"); } } else { ModelState.AddModelError("", "Такой блог уже существует"); } } return View(addBlogModel); }
public async Task <IActionResult> Blog(int id, AddBlog blog) { var result = await _BlogService.UpdateBlog(id, blog); return(Json(result)); }
public async Task <IActionResult> Blog(AddBlog blog) { var result = await _BlogService.AddBlog(blog); return(Json(result)); }
public async Task <JsonResult> AddBlog(AddBlog blog) { var result = new SuccessResult(); try { using (var db = new TryNetCoreContext()) { var newblog = new Blog(); newblog.BlogName = blog.blogname; newblog.BlogAuthor = blog.blogauthorname; newblog.BlogCategoryName = blog.blogcategoryname; newblog.BlogPostDate = DateTime.Now; var blogcount = db.Blog.Count(); newblog.BlogRouteUrl = FriendlyUrl.FriendlyUrlMethod(blog.blogname + "-" + blogcount); newblog.BlogImagePath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, blog.blogimage, null); var blogtags = blog.blogtags.Split(','); for (int i = 0; i < blogtags.Length; i++) { newblog.BlogTags.Add(new BlogTags() { BlogTagName = blogtags[i] }); } // ------------------------- // // BLOG CONTENT AGİLİTY PACK // HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(blog.blogcontent); var htmlimages = doc.DocumentNode.SelectNodes("//img"); if (htmlimages != null) { foreach (var htmlimage in htmlimages) { var srcattribute = htmlimage.GetAttributeValue("src", "defaultvalue"); var parseattributearray = srcattribute.Split(','); var parsebase64 = parseattributearray[1]; byte[] imageBytes = Convert.FromBase64String(parsebase64); using (var ms = new MemoryStream()) { ms.Write(imageBytes, 0, imageBytes.Length); IFormFile file = new FormFile(ms, 0, imageBytes.Length, blog.blogimage.Name, blog.blogimage.FileName); var blogimagepath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, file, null); newblog.BlogImages.Add(new BlogImages() { ImagePath = blogimagepath }); var updatesrcattribute = blogimagepath; htmlimage.SetAttributeValue("src", updatesrcattribute); } } } newblog.BlogContent = doc.DocumentNode.InnerHtml; await db.Blog.AddAsync(newblog); await db.SaveChangesAsync(); } result.isSuccess = true; result.Message = "Başarılı"; return(Json(result)); } catch (Exception e) { result.isSuccess = false; result.Message = "Hata"; return(Json(result)); } }
public async Task <IActionResult> UpdateBlog(AddBlog blog, int blogid) { var result = new SuccessResult(); try { using (var db = new TryNetCoreContext()) { var updateblog = db.Blog.Where(i => i.Id == blogid) .Include(i => i.BlogTags) .Include(i => i.BlogImages) .FirstOrDefault(); updateblog.BlogName = blog.blogname; updateblog.BlogAuthor = blog.blogauthorname; updateblog.BlogCategoryName = blog.blogcategoryname; var blogcount = db.Blog.Count(); updateblog.BlogRouteUrl = FriendlyUrl.FriendlyUrlMethod(blog.blogname + "-" + blogcount); if (blog.blogimage != null && blog.blogimage.Length > 0) { updateblog.BlogImagePath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, blog.blogimage, updateblog.BlogImagePath); } updateblog.BlogTags.Clear(); var blogtags = blog.blogtags.Split(','); for (int i = 0; i < blogtags.Length; i++) { updateblog.BlogTags.Add(new BlogTags() { BlogTagName = blogtags[i] }); } // ------------------------- // // BLOG CONTENT AGİLİTY PACK // HtmlDocument deletedoc = new HtmlDocument(); deletedoc.LoadHtml(updateblog.BlogContent); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(blog.blogcontent); var htmlimages = doc.DocumentNode.SelectNodes("//img"); List <String> deleteimages = new List <String>(); if (htmlimages != null) { foreach (var htmlimage in htmlimages) { var srcattribute = htmlimage.GetAttributeValue("src", "defaultvalue"); var parseattributearray = srcattribute.Split(','); if (parseattributearray.Length > 1) { var parsebase64 = parseattributearray[1]; byte[] imageBytes = Convert.FromBase64String(parsebase64); using (var ms = new MemoryStream()) { ms.Write(imageBytes, 0, imageBytes.Length); IFormFile file = new FormFile(ms, 0, imageBytes.Length, "denemeisim", "denemeisim.jpg"); var blogimagepath = await TryNetCore.Utils.FileUpload.ImageUpload(host.ContentRootPath, file, null); updateblog.BlogImages.Add(new BlogImages() { ImagePath = blogimagepath }); var updatesrcattribute = blogimagepath; htmlimage.SetAttributeValue("src", updatesrcattribute); } } else { deleteimages.Add(parseattributearray[0]); } } } updateblog.BlogContent = doc.DocumentNode.InnerHtml; await db.SaveChangesAsync(); } result.isSuccess = true; result.Message = "Başarılı"; return(Json(result)); } catch (Exception e) { var a = e.Message; result.isSuccess = false; result.Message = "Hata"; return(Json(result)); } }