/
CategoryAppService.cs
86 lines (66 loc) · 3.38 KB
/
CategoryAppService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.SharedResources.Authorization;
using EasyAbp.SharedResources.Categories.Dtos;
using EasyAbp.SharedResources.CategoryOwners;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
namespace EasyAbp.SharedResources.Categories
{
public class CategoryAppService : CrudAppService<Category, CategoryDto, Guid, GetCategoryListDto, CreateUpdateCategoryDto, CreateUpdateCategoryDto>,
ICategoryAppService
{
protected override string CreatePolicyName { get; set; } = SharedResourcesPermissions.Categories.Create;
protected override string DeletePolicyName { get; set; } = SharedResourcesPermissions.Categories.Delete;
protected override string UpdatePolicyName { get; set; } = SharedResourcesPermissions.Categories.Update;
private readonly ICategoryOwnerManager _categoryOwnerManager;
private readonly ICategoryDataPermissionProvider _categoryDataPermissionProvider;
private readonly ICategoryRepository _repository;
public CategoryAppService(
ICategoryOwnerManager categoryOwnerManager,
ICategoryDataPermissionProvider categoryDataPermissionProvider,
ICategoryRepository repository) : base(repository)
{
_categoryOwnerManager = categoryOwnerManager;
_categoryDataPermissionProvider = categoryDataPermissionProvider;
_repository = repository;
}
protected override IQueryable<Category> CreateFilteredQuery(GetCategoryListDto input)
{
var query = _repository.GetQueryable(input.OwnerUserId)
.Where(x => x.ParentCategoryId == input.RootCategoryId);
return input.CustomMark != null ? query.Where(x => x.CustomMark == input.CustomMark) : query;
}
public override async Task<CategoryDto> CreateAsync(CreateUpdateCategoryDto input)
{
var dto = await base.CreateAsync(input);
await _categoryOwnerManager.AddCategoryOwnerAsync(dto.Id, CurrentUser.GetId());
if (input.SetToCommon && await _categoryDataPermissionProvider.IsCurrentUserHasGlobalManagePermissionAsync())
{
await _categoryOwnerManager.AddCategoryOwnerAsync(dto.Id, null);
}
return dto;
}
public override async Task<CategoryDto> UpdateAsync(Guid id, CreateUpdateCategoryDto input)
{
await CheckUpdatePolicyAsync();
await _categoryDataPermissionProvider.CheckCurrentUserAllowedToManageAsync(id);
var category = await GetEntityByIdAsync(id);
if (input.SetToCommon && await _categoryDataPermissionProvider.IsCurrentUserHasGlobalManagePermissionAsync())
{
await _categoryOwnerManager.GetOrAddCategoryOwnerAsync(category.Id, null);
}
MapToEntity(input, category);
await Repository.UpdateAsync(category, autoSave: true);
return MapToGetOutputDto(category);
}
public override async Task DeleteAsync(Guid id)
{
await CheckDeletePolicyAsync();
await _categoryDataPermissionProvider.CheckCurrentUserAllowedToManageAsync(id);
await Repository.DeleteAsync(id);
await _categoryOwnerManager.RemoveAllCategoryOwnersAsync(id);
}
}
}