RoleRepository.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Entity;
  8. using GxPress.Repository.Interface;
  9. using GxPress.Request.Role;
  10. using GxPress.Result.Role;
  11. using Microsoft.Extensions.Options;
  12. using Datory;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class RoleRepository : IRoleRepository
  16. {
  17. private readonly Repository<Role> _repository;
  18. public RoleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
  19. {
  20. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  21. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  22. _repository = new Repository<Role>(database);
  23. }
  24. public IDatabase Database => _repository.Database;
  25. public string TableName => _repository.TableName;
  26. public List<TableColumn> TableColumns => _repository.TableColumns;
  27. public async Task<int> InsertAsync(Role role)
  28. {
  29. return await _repository.InsertAsync(role);
  30. }
  31. public async Task<bool> IsNameExistsAsync(string name)
  32. {
  33. return await _repository.ExistsAsync(Q.Where(nameof(Role.Name), name));
  34. }
  35. public async Task<Role> GetAsync(int id)
  36. {
  37. return await _repository.GetAsync(id);
  38. }
  39. public async Task<string> GetNameAsync(int id)
  40. {
  41. if (id <= 0) return string.Empty;
  42. return await _repository.GetAsync<string>(Q
  43. .Select(nameof(Role.Name))
  44. .Where(nameof(Role.Id), id)
  45. );
  46. }
  47. public async Task<IEnumerable<Role>> GetListAsync(int groupId)
  48. {
  49. return await _repository.GetAllAsync(Q.Where(nameof(Role.GroupId), groupId).OrderBy(nameof(Role.Sort)));
  50. }
  51. /// <summary>
  52. /// 添加
  53. /// </summary>
  54. /// <param name="request"></param>
  55. /// <returns></returns>
  56. public async Task<RoleDetailResult> AddAsync(RoleAddRequest request)
  57. {
  58. var role = new Role
  59. {
  60. GroupId = request.GroupId,
  61. Name = request.Name,
  62. Sort = request.Sort
  63. };
  64. role.Id = await InsertAsync(role);
  65. return new RoleDetailResult
  66. {
  67. Id = role.Id,
  68. Name = role.Name,
  69. GroupId = role.GroupId,
  70. Sort = role.Sort
  71. };
  72. }
  73. /// <summary>
  74. /// 修改
  75. /// </summary>
  76. /// <param name="id"></param>
  77. /// <param name="request"></param>
  78. /// <returns></returns>
  79. public async Task<bool> UpdateAsync(int id, RoleAddRequest request)
  80. {
  81. var role = await GetAsync(id);
  82. if (role == null) throw new BusinessException("该角色不存在");
  83. role.Name = request.Name;
  84. role.GroupId = request.GroupId;
  85. role.Sort = request.Sort;
  86. return await _repository.UpdateAsync(role);
  87. }
  88. /// <summary>
  89. /// 删除
  90. /// </summary>
  91. /// <param name="id"></param>
  92. /// <returns></returns>
  93. public async Task<bool> DeleteAsync(int id)
  94. {
  95. var role = await GetAsync(id);
  96. if (role == null) throw new BusinessException("该角色不存在");
  97. return await _repository.DeleteAsync(id);
  98. }
  99. /// <summary>
  100. /// 详情
  101. /// </summary>
  102. /// <param name="id"></param>
  103. /// <returns></returns>
  104. public async Task<RoleDetailResult> GetDetailAsync(int id)
  105. {
  106. var role = await GetAsync(id);
  107. if (role == null) throw new BusinessException("该角色不存在");
  108. return new RoleDetailResult
  109. {
  110. Id = role.Id,
  111. Name = role.Name,
  112. GroupId = role.GroupId
  113. };
  114. }
  115. /// <summary>
  116. /// 列表
  117. /// </summary>
  118. /// <param name="groupId"></param>
  119. /// <returns></returns>
  120. public async Task<List<RoleDetailResult>> GetDetailListAsync(int groupId)
  121. {
  122. var list = await GetListAsync(groupId);
  123. var result = list.Select(item => new RoleDetailResult
  124. {
  125. Id = item.Id,
  126. Name = item.Name,
  127. GroupId = item.GroupId,
  128. Sort = item.Sort
  129. }).ToList();
  130. return result;
  131. }
  132. }
  133. }