Blogging module post delete in app services and repositories

pull/441/head
Yunus Emre Kalkan 7 years ago
parent 67ab323d9a
commit e6550bbb72

@ -13,6 +13,8 @@ namespace Volo.Blogging.Posts
Task<PostWithDetailsDto> GetAsync(Guid id);
Task DeleteAsync(Guid id);
Task<PostWithDetailsDto> CreateAsync(CreatePostDto input);
Task<PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input);

@ -41,7 +41,7 @@ namespace Volo.Blogging.Posts
foreach (var postDto in postDtos)
{
postDto.Tags = await GetTagsOfPost(postDto);
postDto.Tags = await GetTagsOfPost(postDto.Id);
postDto.CommentCount = await _commentRepository.GetCommentCountOfPostAsync(postDto.Id);
}
@ -63,7 +63,7 @@ namespace Volo.Blogging.Posts
var postDto = ObjectMapper.Map<Post, PostWithDetailsDto>(post);
postDto.Tags = await GetTagsOfPost(postDto);
postDto.Tags = await GetTagsOfPost(postDto.Id);
return postDto;
}
@ -74,11 +74,21 @@ namespace Volo.Blogging.Posts
var postDto = ObjectMapper.Map<Post, PostWithDetailsDto>(post);
postDto.Tags = await GetTagsOfPost(postDto);
postDto.Tags = await GetTagsOfPost(postDto.Id);
return postDto;
}
public async Task DeleteAsync(Guid id)
{
var tags = await GetTagsOfPost(id);
_tagRepository.DecreaseUsageCountOfTags(tags.Select(t=>t.Id).ToList());
_postTagRepository.DeleteOfPost(id);
_commentRepository.DeleteOfPost(id);
await _postRepository.DeleteAsync(id);
}
[Authorize(BloggingPermissions.Posts.Update)]
public async Task<PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input)
{
@ -171,9 +181,9 @@ namespace Volo.Blogging.Posts
}
}
private async Task<List<TagDto>> GetTagsOfPost(PostWithDetailsDto postDto)
private async Task<List<TagDto>> GetTagsOfPost(Guid id)
{
var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == postDto.Id);
var tagIds = (await _postTagRepository.GetListAsync()).Where(pt => pt.PostId == id);
var tags = await _tagRepository.GetListAsync(tagIds.Select(t => t.TagId));

@ -14,5 +14,7 @@ namespace Volo.Blogging.Comments
Task<int> GetCommentCountOfPostAsync(Guid postId);
Task<List<Comment>> GetRepliesOfComment(Guid id);
void DeleteOfPost(Guid id);
}
}

@ -1,8 +1,11 @@
using Volo.Abp.Domain.Repositories;
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
namespace Volo.Blogging.Posts
{
public interface IPostTagRepository : IBasicRepository<PostTag>
{
void DeleteOfPost(Guid id);
}
}

@ -12,5 +12,7 @@ namespace Volo.Blogging.Tagging
Task<Tag> GetByNameAsync(string name);
Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids);
void DecreaseUsageCountOfTags(List<Guid> id);
}
}

@ -35,5 +35,11 @@ namespace Volo.Blogging.Comments
return await DbSet
.Where(a => a.RepliedCommentId == id).ToListAsync();
}
public void DeleteOfPost(Guid id)
{
var recordsToDelete = DbSet.Where(pt => pt.PostId == id);
DbSet.RemoveRange(recordsToDelete);
}
}
}

@ -1,4 +1,7 @@
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Blogging.EntityFrameworkCore;
@ -9,5 +12,11 @@ namespace Volo.Blogging.Posts
public EfCorePostTagRepository(IDbContextProvider<IBloggingDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public void DeleteOfPost(Guid id)
{
var recordsToDelete = DbSet.Where(pt=>pt.PostId == id);
DbSet.RemoveRange(recordsToDelete);
}
}
}

@ -30,5 +30,15 @@ namespace Volo.Blogging.Tagging
{
return await DbSet.Where(c => ids.Contains(c.Id)).ToListAsync();
}
public void DecreaseUsageCountOfTags(List<Guid> ids)
{
var tags = DbSet.Where(t => ids.Any(id => id == t.Id));
foreach (var tag in tags)
{
tag.DecreaseUsageCount();
}
}
}
}

Loading…
Cancel
Save