Ejemplo n.º 1
0
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <ActionOutput <string> > InsertPost(PostForAdminDto dto)
        {
            var output = new ActionOutput <string>();

            var post = new Post
            {
                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
            };
            var id = _postRepository.InsertAsync(post).Result.Id;

            var tags = await _tagRepository.GetAllListAsync();

            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)
            {
                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);
        }