示例#1
0
        private async Task SaveTags(List <String> newTags, Post post)
        {
            var oldTags = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == post.Id).ToList();

            foreach (var oldTag in oldTags)
            {
                await _postTagRepository.DeleteAsync(oldTag);
            }

            var tags = await _tagRepository.GetListAsync();

            foreach (var newTag in newTags)
            {
                var tag = tags.FirstOrDefault(t => t.Name == newTag);

                if (tag == null)
                {
                    tag = await _tagRepository.InsertAsync(new Tag(newTag));
                }

                await _postTagRepository.InsertAsync(new PostTag(post.Id, tag.Id));
            }
        }
示例#2
0
        private async Task RemoveOldTags(List <string> newTags, Post post)
        {
            var oldTags = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == post.Id).ToList();

            foreach (var oldTag in oldTags)
            {
                var tag = await _tagRepository.GetAsync(oldTag.TagId);

                var oldTagNameInNewTags = newTags.FirstOrDefault(t => t == tag.Name);

                if (oldTagNameInNewTags == null)
                {
                    await _postTagRepository.DeleteAsync(oldTag);

                    tag.DecreaseUsageCount();
                    await _tagRepository.UpdateAsync(tag);
                }
                else
                {
                    newTags.Remove(oldTagNameInNewTags);
                }
            }
        }
示例#3
0
 public bool DeletePostTag(PostTag postTag)
 {
     return(_postTag.DeleteAsync(postTag).Result);
 }
示例#4
0
        /// <summary>
        /// 更新文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <ActionOutput <string> > UpdatePost(int id, PostForAdminDto dto)
        {
            var output = new ActionOutput <string>();

            var post = new Post
            {
                Id           = id,
                Title        = dto.Title,
                Author       = dto.Author,
                Url          = $"{dto.CreationTime?.ToString(" yyyy MM dd ").Replace(" ", "/")}{dto.Url}/",
                Html         = dto.Html,
                Markdown     = dto.Markdown,
                CreationTime = dto.CreationTime,
                CategoryId   = dto.CategoryId
            };

            await _postRepository.UpdateAsync(post);

            var tags = await _tagRepository.GetAllListAsync();

            var oldPostTags = (from post_tags in await _postTagRepository.GetAllListAsync()
                               join tag in await _tagRepository.GetAllListAsync()
                               on post_tags.TagId equals tag.Id
                               where post_tags.PostId == post.Id
                               select new
            {
                post_tags.Id,
                tag.TagName
            }).ToList();

            foreach (var item in oldPostTags)
            {
                if (!dto.Tags.Any(x => x == item.TagName) && tags.Any(t => t.TagName == item.TagName))
                {
                    await _postTagRepository.DeleteAsync(item.Id);
                }
            }

            var newTags = new List <Tag>();

            foreach (var item in dto.Tags)
            {
                if (!tags.Any(x => x.TagName == item))
                {
                    newTags.Add(new Tag
                    {
                        TagName     = item,
                        DisplayName = item
                    });
                }
            }
            await _tagRepository.BulkInsertTagsAsync(newTags);

            var postTags = new List <PostTag>();

            foreach (var item in dto.Tags)
            {
                if (!oldPostTags.Any(x => x.TagName == item))
                {
                    var tagId = _tagRepository.FirstOrDefaultAsync(x => x.TagName == item).Result.Id;
                    postTags.Add(new PostTag
                    {
                        PostId = id,
                        TagId  = tagId
                    });
                }
            }
            await _postTagRepository.BulkInsertPostTagsAsync(postTags);

            output.Result = "success";

            return(output);
        }