コード例 #1
0
ファイル: ArticleController.cs プロジェクト: clustersoft/cms
        public ResponseInfoModel AddInfo([FromBody] CreateArticleInput input)
        {
            ResponseInfoModel json = new ResponseInfoModel()
            {
                Success = 1, Result = new object()
            };

            try
            {
                CheckModelState();
                var output = _articleService.AddInfo(input);
                if (output == null)
                {
                    json.Success = 0;
                    json.Result  = 1;
                }
                else
                {
                    _logService.Insert(new Log()
                    {
                        ActionContent = LocalizationConst.Insert,
                        SourceType    = _moduleName,
                        SourceID      = output.ID,
                        LogTime       = DateTime.Now,
                        LogUserID     = input.CreateUser,
                        LogIPAddress  = IPHelper.GetIPAddress,
                    });
                }
            }
            catch (Exception e)
            {
                DisposeUserFriendlyException(e, ref json, "api/article/addInfo", LocalizationConst.InsertFail);
            }
            return(json);
        }
コード例 #2
0
 private async Task CheckCoverImageExist(CreateArticleInput input)
 {
     if (!await AttachmentExist(input.CoverImage))
     {
         throw new BusinessException("封面图片对应的附件不存在!", nameof(input.CoverImage), input.CoverImage);
     }
 }
コード例 #3
0
        public virtual async Task <long> CreateArticleAsync(CreateArticleInput input)
        {
            var blog = new Blog
            {
                Article = ObjectMapper.Map <Article>(input)
            };

            return(await _blogManager.Create(blog));
        }
コード例 #4
0
 private async Task CheckAttachmentExist(CreateArticleInput input)
 {
     if (input.AttachmentIds.IsNotNullOrEmpty())
     {
         foreach (var attachmentId in input.AttachmentIds)
         {
             if (await AttachmentExist(attachmentId))
             {
                 throw new BusinessException("附件不存在!", nameof(input.AttachmentIds), attachmentId);
             }
         }
     }
 }
コード例 #5
0
        public async Task <string> CreateArticle(CreateArticleInput input)
        {
            var article = _mapper.Map <Article>(input);

            await CheckCoverImageExist(input);

            await CheckAttachmentExist(input);

            article.Categories = await GetCategorys(input);

            SetPublishDate(article);

            var id = await _articleRepository.Insert(article);

            return(id);
        }
コード例 #6
0
        private async Task <IList <Category> > GetCategorys(CreateArticleInput input)
        {
            if (input.CategoryIds.IsNotNullOrEmpty())
            {
                var categoryIds = input.CategoryIds.Distinct();

                var categorys = await _categoryRepository.GetById(categoryIds);

                if (categoryIds.Count() != categorys.Count)
                {
                    throw new BusinessException("分类信息不存在!");
                }

                return(categorys);
            }
            else
            {
                return(null);
            }
        }
コード例 #7
0
        public async Task <CreateArticlePayload> CreateArticleAsync(
            CreateArticleInput input,
            [Service] IDateTimeOffset dateTimeOffset,
            [Service] IGuid guid,
            [Service] ITopicEventSender sender,
            [ScopedService] NmediaContext dbContext,
            CancellationToken cancellationToken
            )
        {
            Nmedian?nmedian = null;

            if (input.NmedianId.HasValue)
            {
                nmedian = await dbContext.Nmedians.SingleOrDefaultAsync(
                    x => x.Uuid == input.NmedianId.Value,
                    cancellationToken
                    ) ?? throw new ArgumentException($"The Nmédian (ID={input.NmedianId}) could not be found.", nameof(input));
            }

            var entity = new Article
            {
                Categories = input.Categories,
                Content    = input.Content,
                Created    = dateTimeOffset.Now,
                Nmedian    = nmedian,
                NmedianId  = nmedian?.Id,
                Picture    = input.Picture,
                Published  = input.Published,
                Title      = input.Title,
                Uuid       = guid.NewGuid()
            };

            dbContext.Articles.Add(entity);
            await dbContext.SaveChangesAsync(cancellationToken);

            await sender.SendAsync(nameof(Subscription.OnArticleSaved), entity, cancellationToken);

            return(new CreateArticlePayload(entity));
        }
コード例 #8
0
        public async Task <IActionResult> CreateArticle([FromBody] CreateArticleInput input)
        {
            var id = await _articleService.CreateArticle(input);

            return(Created($"/api/v1/articles/{id}", null));
        }