Beispiel #1
0
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <ServiceResult> InsertPostAsync(EditPostInput input)
        {
            var result = new ServiceResult();

            var post = ObjectMapper.Map <EditPostInput, Post>(input);

            post.Url = $"{post.CreationTime.ToString(" yyyy MM dd ").Replace(" ", "/")}{post.Url}/";
            await _postRepository.InsertAsync(post);

            var tags = await _tagRepository.GetListAsync();

            var newTags = input.Tags
                          .Where(item => !tags.Any(x => x.TagName.Equals(item)))
                          .Select(item => new Tag
            {
                TagName     = item,
                DisplayName = item
            });
            await _tagRepository.BulkInsertAsync(newTags);

            var postTags = input.Tags.Select(item => new PostTag
            {
                PostId = post.Id,
                TagId  = _tagRepository.FirstOrDefault(x => x.TagName == item).Id
            });
            await _postTagRepository.BulkInsertAsync(postTags);

            result.IsSuccess(ResponseText.INSERT_SUCCESS);
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// 更新文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <ServiceResult> UpdatePostAsync(int id, EditPostInput input)
        {
            var result = new ServiceResult();

            var post = await _postRepository.GetAsync(id);

            post.Title        = input.Title;
            post.Author       = input.Author;
            post.Url          = $"{input.CreationTime.ToString(" yyyy MM dd ").Replace(" ", "/")}{input.Url}/";
            post.Html         = input.Html;
            post.Markdown     = input.Markdown;
            post.CreationTime = input.CreationTime;
            post.CategoryId   = input.CategoryId;

            await _postRepository.UpdateAsync(post);

            var tags = await _tagRepository.GetListAsync();

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

            var removedIds = oldPostTags.Where(item => !input.Tags.Any(x => x == item.TagName) &&
                                               tags.Any(t => t.TagName == item.TagName))
                             .Select(item => item.Id);
            await _postTagRepository.DeleteAsync(x => removedIds.Contains(x.Id));

            var newTags = input.Tags
                          .Where(item => !tags.Any(x => x.TagName == item))
                          .Select(item => new Tag
            {
                TagName     = item,
                DisplayName = item
            });
            await _tagRepository.BulkInsertAsync(newTags);

            var postTags = input.Tags
                           .Where(item => !oldPostTags.Any(x => x.TagName == item))
                           .Select(item => new PostTag
            {
                PostId = id,
                TagId  = _tagRepository.FirstOrDefault(x => x.TagName == item).Id
            });
            await _postTagRepository.BulkInsertAsync(postTags);

            await _distributedEventBus.PublishAsync(new CachingRemoveEventData
            {
                Key = CachePrefix.Blog_Post
            });

            result.IsSuccess(ResponseText.UPDATE_SUCCESS);
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <ServiceResult> InsertPostAsync(EditPostInput input)
        {
            var result = new ServiceResult();
            var post   = ObjectMapper.Map <EditPostInput, Post>(input);

            post.Url = $"{post.CreationTime.ToString(" yyyy MM dd ").Replace(" ", "/")}{post.Url}";
            await _postRepository.InsertAsync(post);

            var tags = await _tagRepository.GetListAsync();

            var newTags = input.Tags.Where(item => !tags.Any(x => x.TagName.Equals(item))).Select(item => new Tag
            {
                TagName     = item,
                DisplayName = item
            });
            await _tagRepository.BulkInsertAsync(newTags);

            //执行清除缓存操作
            await _blogCacheService.RemoveAsync(CachePrefix.Blog_Post);

            result.IsSuccess(ResponseText.INSERT_SUCCESS);
            return(result);
        }
 public async Task <ServiceResult> UpdatePostAsync([Required] int id, [FromBody] EditPostInput input)
 {
     return(await _blogService.UpdatePostAsync(id, input));
 }
 public async Task <ServiceResult> InsertPostAsync([FromBody] EditPostInput input)
 {
     return(await _blogService.InsertPostAsync(input));
 }