Beispiel #1
0
        public async Task <Response <string> > UpdatePostAsync(int id, PostForAdminDto dto)
        {
            var response = new Response <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
            };

            _context.Posts.Update(post);
            await _context.SaveChangesAsync();

            var tags = await _context.Tags.ToListAsync();

            var oldPostTags = (from post_tags in await _context.PostTags.ToListAsync()
                               join tag in await _context.Tags.ToListAsync()
                               on post_tags.TagId equals tag.Id
                               where post_tags.PostId.Equals(post.Id)
                               select new
            {
                post_tags.Id,
                tag.TagName
            }).ToList();

            var removedIds      = oldPostTags.Where(item => !dto.Tags.Any(x => x == item.TagName) && tags.Any(t => t.TagName == item.TagName)).Select(item => item.Id).ToList();
            var removedPostTags = await _context.PostTags.Where(x => removedIds.Contains(x.Id)).ToListAsync();

            _context.PostTags.RemoveRange(removedPostTags);
            await _context.SaveChangesAsync();

            var newTags = dto.Tags.Where(item => !tags.Any(x => x.TagName == item)).Select(item => new Tag
            {
                TagName     = item,
                DisplayName = item
            }).ToList();
            await _context.Tags.AddRangeAsync(newTags);

            await _context.SaveChangesAsync();

            var postTags = dto.Tags.Where(item => !oldPostTags.Any(x => x.TagName == item)).Select(item => new PostTag
            {
                PostId = id,
                TagId  = _context.Tags.FirstOrDefault(x => x.TagName == item).Id
            }).ToList();
            await _context.PostTags.AddRangeAsync(postTags);

            await _context.SaveChangesAsync();

            response.Result = "更新成功";
            return(response);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public async Task <Response <string> > UpdatePost(int id, [FromBody] PostForAdminDto dto)
        {
            var response = new Response <string>();

            var result = await _blogService.UpdatePost(id, dto);

            if (!result.Success)
            {
                response.SetMessage(ResponseStatusCode.Error, result.GetErrorMessage());
            }
            else
            {
                response.Result = result.Result;
            }
            return(response);
        }
Beispiel #4
0
        public async Task <Response <string> > InsertPostAsync([FromBody] PostForAdminDto dto)
        {
            var response = new Response <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
            };
            await _context.Posts.AddAsync(post);

            await _context.SaveChangesAsync();

            var tags = await _context.Tags.ToListAsync();

            var newTags = dto.Tags.Where(item => !tags.Any(x => x.TagName.Equals(item))).SelectToList(item => new Tag
            {
                TagName     = item,
                DisplayName = item
            });
            await _context.Tags.AddRangeAsync(newTags);

            await _context.SaveChangesAsync();

            var postTags = dto.Tags.SelectToList(item => new PostTag
            {
                PostId = post.Id,
                TagId  = _context.Tags.FirstOrDefault(x => x.TagName == item).Id
            });
            await _context.PostTags.AddRangeAsync(postTags);

            await _context.SaveChangesAsync();

            response.Result = "新增成功";
            return(response);
        }
Beispiel #5
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);
        }