public JsonResult togglePost(string rowkey) { BlogPostClient bpc = new BlogPostClient(); BlogTagClient btc = new BlogTagClient(); BlogPost blogPost = bpc.GetByPartitionAndRowKey("blogPost", rowkey); if (blogPost != null) { blogPost.Removed = !blogPost.Removed; bpc.Update(blogPost); List <string> tags = new List <string>(blogPost.Tags.Split(',')); foreach (string tag in tags) { BlogTag blogTag = btc.GetByPartitionAndRowKey("blogTag", tag); if (blogTag != null) { if (blogPost.Removed) { blogTag.PublicPosts--; } else { blogTag.PublicPosts++; } btc.Update(blogTag); } } } return(new JsonResult { Data = new { result = "ok" } }); }
public BlogViewModel Add(BlogViewModel blogVm) { var blog = _mapper.Map <BlogViewModel, Blog>(blogVm); if (!string.IsNullOrEmpty(blog.Tags)) { var tags = blog.Tags.Split(','); foreach (var t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.BlogTag }; _tagRepository.Add(tag); } var blogTag = new BlogTag { TagId = tagId }; _blogTagRepository.Add(blogTag); } } _blogRepository.Add(blog); return(blogVm); }
public int Add(BlogViewModel blogVm) { Blog blogDb = _mapper.Map <Blog>(blogVm); _blogRepository.Add(blogDb); _unitOfWork.Commit(); if (!string.IsNullOrEmpty(blogDb.Tags)) { string[] listTag = blogDb.Tags.Split(','); for (int i = 0; i < listTag.Length; i++) { var tagId = StringHelper.ToUnsignString(listTag[i]); if (_tagRepository.FindById(tagId) == null) { Tag tag = new Tag() { Id = tagId, Name = listTag[i], Type = CommonConstants.BlogTag, }; _tagRepository.Add(tag); } BlogTag blogTag = new BlogTag() { BlogId = blogDb.Id, TagId = tagId, }; _blogTagRepository.Add(blogTag); } } return(blogDb.Id); }
public void Update(Blog blogDb) { _blogRepository.Update(blogDb); _unitOfWork.Commit(); if (!string.IsNullOrEmpty(blogDb.Tags)) { DeleteBlogTagByBlogId(blogDb.Id); _unitOfWork.Commit(); string[] listTag = blogDb.Tags.Split(','); for (int i = 0; i < listTag.Length; i++) { var tagId = StringHelper.ToUnsignString(listTag[i]); if (_tagRepository.FindById(tagId) == null) { Tag tag = new Tag() { Id = tagId, Name = listTag[i], Type = CommonConstants.BlogTag, }; _tagRepository.Add(tag); } BlogTag blogTag = new BlogTag() { BlogId = blogDb.Id, TagId = tagId, }; _blogTagRepository.Add(blogTag); } } }
public void Update(BlogViewModel blog) { _blogRepository.Update(blog.AddModel()); if (!string.IsNullOrEmpty(blog.Tags)) { string[] tags = blog.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.ProductTag }; _tagRepository.Add(tag); } _blogTagRepository.RemoveMultiple(_blogTagRepository.FindAll(x => x.Id == blog.Id).ToList()); BlogTag blogTag = new BlogTag { BlogId = blog.Id, TagId = tagId }; _blogTagRepository.Add(blogTag); } } }
private void AddBlogTag(IBlogPostCommand command, BlogPost post) { if (command.BlogTags == null) { return; } foreach (var item in command.BlogTags.Where(c => c.Name != null)) { var tag = new BlogTag { Name = item.Name, CreationDate = DateTime.Now, Creator = "sdf", }; if (item.IsNew) { post.AddTag(tag); // post.BlogTags.ForEach(c => c.BlogTags.Add(tag)); } else { var productTag = _blogRepository.GetBlogTagByValue(item.Name); post.BlogTags.Add(productTag); } } }
public void Add(BlogViewModel blogVm, out bool result, out string message) { var blog = Mapper.Map <BlogViewModel, Blog>(blogVm); if (!string.IsNullOrEmpty(blog.Tags)) { var tags = blog.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.TagType.BlogTag }; _tagRepository.Add(tag, out result, out message); } var blogTag = new BlogTag { TagId = tagId }; blog.BlogTags.Add(blogTag); } } _blogRepository.Add(blog, out result, out message); }
public async Task <BlogViewModel> AddAsync(BlogViewModel blogViewModel) { var blog = _mapper.Map <Blog>(blogViewModel); // if blog has tags if (!string.IsNullOrEmpty(blog.Tags)) { var tags = blog.Tags.Split(","); foreach (var item in tags) { var tagId = TextHelper.ToUnsignString(item); // if tag not-exists in database => add a new tag if (!_tagRepository.GetAll(x => x.Id == tagId).Any()) { var tag = new Tag { Id = tagId, Name = item, Type = CommonConstants.BlogTag }; _tagRepository.Add(tag); } var blogTag = new BlogTag { TagId = tagId }; blog.BlogTags.Add(blogTag); } } // add and commit _blogRepository.Add(blog); await _unitOfWork.CommitAsync(); return(blogViewModel); }
public async Task <ApiResponse <object> > UpdateTagAsync([FromForm] BlogTag tag, [FromRoute] string id) { var response = new ApiResponse <object>(); try { _blogGrpcRequest.Tag = tag; var blogResponse = await _blogGrpcService.UpdateTagAsync(_blogGrpcRequest); if (blogResponse.IsOk) { response.RedirectTo = "/blog/tag"; } else { response.IsOk = false; response.Message = blogResponse.Message; } } catch (Exception ex) { response.IsOk = false; response.Message = ex.Message; _logger.LogError(ex, $"Update tag failed. {JsonUtil.Serialize(tag)}"); } return(response); }
public void Update(BlogViewModel blog) { _blogRepository.Update(Mapper.Map <BlogViewModel, Blog>(blog)); if (!string.IsNullOrEmpty(blog.Tags)) { string[] tags = blog.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.SelectAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstant.ProductTag }; _tagRepository.Insert(tag); } _blogTagRepository.DeleteMultiple(_blogTagRepository.SelectAll(x => x.Id == blog.Id).ToList()); BlogTag blogTag = new BlogTag { BlogId = blog.Id, TagId = tagId }; _blogTagRepository.Insert(blogTag); } } }
public async Task <ApiResponse <object> > AddTagAsync([FromBody] BlogTag tag) { var response = new ApiResponse <object>(); try { _blogGrpcRequest.Tag = tag; var blogResponse = await _blogGrpcService.AddTagAsync(_blogGrpcRequest); if (blogResponse.IsOk) { response.RedirectTo = "/blog/tag"; } else { response.IsOk = false; response.Message = blogResponse.Message; } } catch (Exception ex) { response.IsOk = false; response.Message = ex.Message; } return(response); }
public async Task <BlogViewModel> Add(BlogViewModel blogVm) { var blog = new BlogViewModel().Map(blogVm); if (!string.IsNullOrEmpty(blog.Tags)) { var tags = blog.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!(await _tagRepository.FindAll(x => x.Id == tagId)).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.BlogTag }; await _tagRepository.Add(tag); } var blogTag = new BlogTag { TagId = tagId }; blog.BlogTags.Add(blogTag); } } await _blogRepository.Add(blog); return(blogVm); }
public async Task Update(BlogViewModel blog) { await _blogRepository.Update(new BlogViewModel().Map(blog)); if (!string.IsNullOrEmpty(blog.Tags)) { var blogTags = await _blogTagRepository.FindAll(x => x.BlogId == blog.Id); await _blogTagRepository.RemoveMultiple(blogTags.ToList()); string[] tags = blog.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!(await _tagRepository.FindAll(x => x.Id == tagId)).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.ProductTag }; await _tagRepository.Add(tag); } await _blogTagRepository.RemoveMultiple((await _blogTagRepository.FindAll(x => x.Id == blog.Id)).ToList()); BlogTag blogTag = new BlogTag { BlogId = blog.Id, TagId = tagId }; await _blogTagRepository.Add(blogTag); } } }
public Blog Get(string tagName) { Blog blog = null; DataProvider.ExecuteCmd(GetConnection, "dbo.Blogs_SelectByTagName" , inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@tagName", tagName); //model binding }, map : delegate(IDataReader reader, short set) { if (set == 0) { blog = MapBlog(reader); } else if (set == 1) { BlogTag bt = MapBlogTag(reader); if (blog.Tags == null) { blog.Tags = new List <BlogTag>(); } blog.Tags.Add(bt); } } ); return(blog); }
public void Update(BlogViewModel blogVm) { // update blog _blogRepository.Update(_mapper.Map <BlogViewModel, Blog>(blogVm)); // checking update blog tags if (!string.IsNullOrEmpty(blogVm.Tags)) { var tags = blogVm.Tags.Split(','); foreach (var t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.ProductTag }; _tagRepository.Add(tag); } _blogTagRepository.RemoveMultiple(_blogTagRepository.FindAll(x => x.BlogId == blogVm.Id).ToList()); BlogTag blogTag = new BlogTag { BlogId = blogVm.Id, TagId = tagId }; _blogTagRepository.Add(blogTag); } } }
public static List <BlogTag> search(string searchString) { List <BlogTag> list = new List <BlogTag>(); DataProvider.ExecuteCmd(GetConnection, "dbo.BlogTag_Search", inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@searchString", searchString); }, map : delegate(IDataReader reader, short set) { switch (set) { case 0: BlogTag bt = MapBlogTag(reader); if (list == null) { list = new List <BlogTag>(); } list.Add(bt); break; } } ); return(list); }
public async Task <IActionResult> Edit(int id, [Bind("ID,Name")] BlogTag blogTag) { if (id != blogTag.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(blogTag); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BlogTagExists(blogTag.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(blogTag)); }
public ActionResult SearchBlogTagView(string tagname, string blogname) { BlogTag BlogsTags = new BlogTag { Blogs = db.Blogs.Where(x => x.Posts.Count > 0).Include(x => x.Author).ToList(), Tags = db.Tags.ToList() }; foreach (var item in BlogsTags.Tags.GroupBy(x => x.Name)) { int?count = 0; foreach (var itema in item.Where(x => x.Name == item.Key).ToList()) { count += itema.Posts.Sum(x => x.Views); } BlogsTags.TagNameViews.Add(item.Key, count); } BlogsTags.TagNameViews.Values.OrderBy(x => x); foreach (var item in BlogsTags.Blogs.GroupBy(x => x.Titel)) { int?sum = 0; foreach (var blog in item) { sum += blog.Posts.Sum(x => x.Views); } BlogsTags.BlogFilter.Add(item.First(x => x.Id > 0), sum); } BlogsTags.BlogFilter.Values.OrderBy(x => x.Value); #region TagRegion if (tagname != null) { var tags = db.Tags.Where(x => x.Name == tagname).ToList(); foreach (var tag in tags) { foreach (var itema in tag.Posts) { BlogsTags.PostFilter.Add(itema); } } return(View(BlogsTags)); } #endregion #region blogRegion if (blogname != null) { foreach (var blog in BlogsTags.Blogs.Where(x => x.Titel == blogname)) { foreach (var post in blog.Posts) { BlogsTags.PostFilter.Add(post); } } return(View(BlogsTags)); } #endregion ViewBag.posts = db.Posts.OrderByDescending(x => x.Views).Take(5).ToList(); return(View(BlogsTags)); }
public void UpdateBlogTag(BlogTag BlogTagToUpdate, BlogTag model) { BlogTagToUpdate.Status = model.Status; BlogTagToUpdate.Name = model.Name; BlogTagToUpdate.ModifiedBy = model.ModifiedBy; BlogTagToUpdate.ModifiedAt = DateTime.Now; _context.SaveChanges(); }
public List <Blog> SelectAllByUserId() { List <Blog> blogs = null; Blog blog = null; string connectiongString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectiongString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "dbo.Blog_SelectAllByUserId"; SqlParameterCollection paramCollection = cmd.Parameters; paramCollection.AddWithValue("@UseridCreated", UserService.GetCurrentUserId()); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { blog = BlogService.MapBlog(reader); if (blogs == null) { blogs = new List <Blog>(); } blogs.Add(blog); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogTag tag = BlogService.MapBlogTag(reader, out blogId); blog = blogs.Find(item => item.Id == blogId); if (blog.Tags == null) { blog.Tags = new List <BlogTag>(); } blog.Tags.Add(tag); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogPhoto photo = BlogService.MapBlogPhoto(reader, out blogId); blog = blogs.Find(item => item.Id == blogId); if (blog.Photos == null) { blog.Photos = new List <BlogPhoto>(); } blog.Photos.Add(photo); } } } return(blogs); }
private static BlogTag MapBlogTag(IDataReader reader) { BlogTag blogTag = new BlogTag(); int startingIndex = 0; //startingOrdinal blogTag.Id = reader.GetSafeInt32(startingIndex++); blogTag.Keyword = reader.GetSafeString(startingIndex++); return(blogTag); }
public async Task<IHttpActionResult> Post(BlogTag m) { if (!ModelState.IsValid) return BadRequest(ModelState); m.CreateTime = System.DateTimeOffset.Now; m = db.BlogTag.Add(m); await db.SaveChangesAsync(); return Created<BlogTag>(m); }
private ActionResult Edit(string hexId, bool isMd) { EditViewModel vm = new EditViewModel() { MarkDown = isMd?"Y":"N" }; if (string.IsNullOrEmpty(hexId)) { if (isMd) { vm.Content = FileHelper.GetString(Server.MapPath("~/Plugins/editormd/template.md")); } } else { long blogId = Convert.ToInt64(Helper.FromHex(hexId)); Blog blog = this._blogService.GetEntityByID(blogId); if (blog == null) { throw new NotFoundException("找不到当前文章!"); } vm = MapperManager.Map <EditViewModel>(blog); vm.PersonTags = blog.BlogTags; vm.HexId = hexId; if (!string.IsNullOrEmpty(blog.BlogTags)) { Dictionary <long, string> tagDic = new Dictionary <long, string>(); string[] tagArr = blog.BlogTags.Split(','); foreach (string tagId in tagArr) { BlogTag blogTag = _tagService.GetEntityByID(Convert.ToInt32(tagId)); if (blogTag != null) { tagDic.Add(blogTag.Id, blogTag.Name); } } ViewBag.PersonTags = tagDic; } } if (string.IsNullOrEmpty(vm.PersonTags)) { vm.PersonTags = ""; } IBlogTypeService typeService = ContainerManager.Resolve <IBlogTypeService>(); ICategoryService cateService = ContainerManager.Resolve <ICategoryService>(); IEnumerable <Category> categories = cateService.GetEntities(c => true). OrderByDescending(c => c.Order); IEnumerable <BlogType> types = typeService.GetEntities(t => true). OrderByDescending(t => t.Order); IEnumerable <BlogTag> tags = _tagService.GetEntities(t => t.UserId == UserContext.LoginUser.Id); ViewBag.Categories = categories; ViewBag.Types = types; ViewBag.Tags = tags; return(View(vm)); }
public IActionResult Create(BlogTag BlogTag) { if (ModelState.IsValid) { BlogTag.CreatedBy = _admin.Fullname; _BlogTagRepository.CreateBlogTag(BlogTag); return(RedirectToAction("index")); } return(View(BlogTag)); }
public IActionResult Edit(int id) { BlogTag abs = _BlogTagRepository.GetBlogTagById(id); if (abs == null) { return(NotFound()); } return(View(abs)); }
/// <summary> /// 获取BlogTag字段的Remark特性值 /// </summary> /// <param name="blogTag"></param> /// <returns></returns> public static string GetRemark(this BlogTag blogTag) { var fieldName = Enum.GetName(typeof(BlogTag), blogTag); var attributes = typeof(BlogTag).GetField(fieldName ?? string.Empty)?.GetCustomAttributes(false); return(!((attributes ?? Array.Empty <object>()).FirstOrDefault(p => p.GetType() == typeof(EnumRemarkAttribute)) is EnumRemarkAttribute enumDisplayAttribute) ? fieldName : enumDisplayAttribute.Remark); }
private static BlogTag MapTagsUsedByBlogs(IDataReader reader) { BlogTag item = new BlogTag(); int startingIndex = 0; item.TagId = reader.GetSafeInt32(startingIndex++); item.TagName = reader.GetSafeString(startingIndex++); return(item); }
public IActionResult Delete(int id) { BlogTag abs = _BlogTagRepository.GetBlogTagById(id); if (abs == null) { return(NotFound()); } _BlogTagRepository.DeleteBlogTag(abs); return(RedirectToAction("index")); }
public async Task <IActionResult> Create([Bind("ID,Name")] BlogTag blogTag) { if (ModelState.IsValid) { _context.Add(blogTag); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(blogTag)); }
public List <Blog> SelectAll() { List <Blog> blogs = null; string connectiongString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectiongString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "dbo.blog_selectAll"; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Blog blog = BlogService.MapBlog(reader); if (blogs == null) { blogs = new List <Blog>(); } blogs.Add(blog); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogTag tag = BlogService.MapBlogTag(reader, out blogId); Blog d = blogs.Find(item => item.Id == blogId); if (d.Tags == null) { d.Tags = new List <BlogTag>(); } d.Tags.Add(tag); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogPhoto photo = BlogService.MapBlogPhoto(reader, out blogId); Blog bl = blogs.Find(item => item.Id == blogId); if (bl.Photos == null) { bl.Photos = new List <BlogPhoto>(); } bl.Photos.Add(photo); } } } return(blogs); }
public ActionResult EssayTag(string username, string hexId) { UserInfo user = GetUser(username); long tagId = Convert.ToInt64(Helper.FromHex(hexId)); BlogTag tag = _blogTagService.GetEntity(t => t.Id == tagId && t.UserId == user.Id); ViewBag.TagName = tag == null ? "" : tag.Name; ViewBag.User = user; ViewBag.TagId = hexId; return(View()); }
public async Task<IHttpActionResult> Put(BlogTag putModel) { var m = await db.BlogTag.FindAsync(putModel.Id); if (m == null) return NotFound(); else { m.Deleted = putModel.Deleted; //TODO:其他属性待补充 m.Name = putModel.Name; m.UpdateBy = CurrentUserId; m.UpdateTime = DateTimeOffset.Now; if (!ModelState.IsValid) return BadRequest(ModelState); await db.SaveChangesAsync(); return Updated(m); } }