1
0

RoleService.cs 3.3 KB

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