RoleService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Threading.Tasks;
  2. using AutoMapper;
  3. using Admin.Core.Common.Auth;
  4. using Admin.Core.Common.Input;
  5. using Admin.Core.Common.Output;
  6. using Admin.Core.Model.Admin;
  7. using Admin.Core.Repository.Admin;
  8. using Admin.Core.Service.Admin.Role.Input;
  9. using Admin.Core.Service.Admin.Role.Output;
  10. using System.Linq;
  11. namespace Admin.Core.Service.Admin.Role
  12. {
  13. public class RoleService : IRoleService
  14. {
  15. private readonly IUser _user;
  16. private readonly IMapper _mapper;
  17. private readonly IRoleRepository _roleRepository;
  18. private readonly IRolePermissionRepository _rolePermissionRepository;
  19. public RoleService(
  20. IUser user,
  21. IMapper mapper,
  22. IRoleRepository roleRepository,
  23. IRolePermissionRepository rolePermissionRepository
  24. )
  25. {
  26. _user = user;
  27. _mapper = mapper;
  28. _roleRepository = roleRepository;
  29. _rolePermissionRepository = rolePermissionRepository;
  30. }
  31. public async Task<IResponseOutput> GetAsync(long id)
  32. {
  33. var result = await _roleRepository.GetAsync<RoleGetOutput>(id);
  34. return ResponseOutput.Ok(result);
  35. }
  36. public async Task<IResponseOutput> PageAsync(PageInput<RoleEntity> input)
  37. {
  38. var key = input.Filter?.Name;
  39. var list = await _roleRepository.Select
  40. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  41. .Count(out var total)
  42. .OrderByDescending(true, c => c.Id)
  43. .Page(input.CurrentPage, input.PageSize)
  44. .ToListAsync<RoleListOutput>();
  45. var data = new PageOutput<RoleListOutput>()
  46. {
  47. List = list,
  48. Total = total
  49. };
  50. return ResponseOutput.Ok(data);
  51. }
  52. public async Task<IResponseOutput> AddAsync(RoleAddInput input)
  53. {
  54. var entity = _mapper.Map<RoleEntity>(input);
  55. var id = (await _roleRepository.InsertAsync(entity)).Id;
  56. return ResponseOutput.Result(id > 0);
  57. }
  58. public async Task<IResponseOutput> UpdateAsync(RoleUpdateInput input)
  59. {
  60. if (!(input?.Id > 0))
  61. {
  62. return ResponseOutput.NotOk();
  63. }
  64. var entity = await _roleRepository.GetAsync(input.Id);
  65. if (!(entity?.Id > 0))
  66. {
  67. return ResponseOutput.NotOk("½ÇÉ«²»´æÔÚ£¡");
  68. }
  69. _mapper.Map(input, entity);
  70. await _roleRepository.UpdateAsync(entity);
  71. return ResponseOutput.Ok();
  72. }
  73. public async Task<IResponseOutput> DeleteAsync(long id)
  74. {
  75. var result = false;
  76. if (id > 0)
  77. {
  78. result = (await _roleRepository.DeleteAsync(m => m.Id == id)) > 0;
  79. }
  80. return ResponseOutput.Result(result);
  81. }
  82. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  83. {
  84. var result = await _roleRepository.SoftDeleteAsync(id);
  85. await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);
  86. return ResponseOutput.Result(result);
  87. }
  88. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  89. {
  90. var result = await _roleRepository.SoftDeleteAsync(ids);
  91. await _rolePermissionRepository.DeleteAsync(a => ids.Contains(a.RoleId));
  92. return ResponseOutput.Result(result);
  93. }
  94. }
  95. }