private async Task <IList <int> > GetCategoryIdsInPostAsync(Post post) { var filter = new PostCategoryFilterSpecification(post); var postCategories = await postsCategoriesRepository.ListAsync(filter); return(postCategories.Select(pc => pc.CategoryId).ToList()); }
private async Task SyncPostCategories(Post post, IList <int> categoryIds) { var current = await GetCategoryIdsInPostAsync(post); var needRemoveIds = current.Where(i => !categoryIds.Contains(i)); if (!needRemoveIds.IsNullOrEmpty()) { var spec = new PostCategoryFilterSpecification(post, needRemoveIds.ToList()); var removeItems = await postsCategoriesRepository.ListAsync(spec); postsCategoriesRepository.DeleteRange(removeItems); } var needToAdd = categoryIds.Where(i => !current.Contains(i)); if (!needToAdd.IsNullOrEmpty()) { foreach (var newCategoryId in needToAdd) { await postsCategoriesRepository.AddAsync(new PostCategory { PostId = post.Id, CategoryId = newCategoryId }); } } }