예제 #1
0
        /// <summary>
        ///  更新文章
        /// </summary>
        /// <param name="request">文章信息</param>
        /// <returns></returns>
        public async Task <ResultModel <string> > UpdateAsync(UpdateArticleRequest request)
        {
            var response = new ResultModel <string>();
            var article  = new ArticleInfo()
            {
                Abstract    = request.Abstract,
                Title       = request.Title,
                Id          = request.Id.ObjToInt(),
                Likes       = request.Likes,
                Views       = request.Views,
                Comments    = request.Comments,
                IsPublished = request.IsPublished,
                ImageUrl    = request.ImageUrl,
                ModifyTime  = DateTime.Now
            };
            var content = new ArticleContent()
            {
                Content    = request.Content,
                ArticleId  = article.Id,
                ModifyTime = DateTime.Now
            };
            var result = await _articleRepository.UseTranAsync(async() =>
            {
                await _articleRepository.UpdateAsync(article);
                await _articleContentRepository.UpdateAsync(content, it => it.ArticleId);
                ////先删除再添加
                await _articleTagRepository.DeleteAsync(x => x.ArticleId == article.Id);
                await _articleCategoryRepository.DeleteAsync(x => x.ArticleId == article.Id);
                var articleTags = request.TagIds.Select(tagId => new ArticleTag()
                {
                    TagId     = tagId.ObjToInt(),
                    ArticleId = article.Id,
                }).ToList();
                await _articleTagRepository.InsertAsync(articleTags);
                var articleCategories = request.CategoryIds.Select(categoryId => new ArticleCategory()
                {
                    ArticleId  = article.Id,
                    CategoryId = categoryId.ObjToInt()
                }).ToList();
                await _articleCategoryRepository.InsertAsync(articleCategories);
            });

            response.IsSuccess  = result.IsSuccess;
            response.ResultInfo = result.ErrorMessage;
            return(response);
        }
        public async Task <IResultModel> Delete(int id)
        {
            var result = await _repository.DeleteAsync(id);

            return(ResultModel.Result(result));
        }