RoleGroupRepository.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.RoleGroup;
  10. using GxPress.Result.RoleGroup;
  11. using Microsoft.Extensions.Options;
  12. using Datory;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class RoleGroupRepository : IRoleGroupRepository
  16. {
  17. private readonly Repository<RoleGroup> _repository;
  18. public RoleGroupRepository(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<RoleGroup>(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(RoleGroup roleGroup)
  28. {
  29. return await _repository.InsertAsync(roleGroup);
  30. }
  31. public async Task<bool> IsNameExistsAsync(string name)
  32. {
  33. return await _repository.ExistsAsync(Q.Where(nameof(RoleGroup.Name), name));
  34. }
  35. public async Task<RoleGroup> GetAsync(int id)
  36. {
  37. return await _repository.GetAsync(id);
  38. }
  39. public async Task<bool> UpdateAsync(RoleGroup roleGroup)
  40. {
  41. return await _repository.UpdateAsync(roleGroup);
  42. }
  43. public async Task<IEnumerable<RoleGroup>> GetListAsync()
  44. {
  45. return await _repository.GetAllAsync();
  46. }
  47. /// <summary>
  48. /// ����
  49. /// </summary>
  50. /// <param name="request"></param>
  51. /// <returns></returns>
  52. public async Task<RoleGroup> AddAsync(RoleGroupAddRequest request)
  53. {
  54. var exist = await IsNameExistsAsync(request.Name);
  55. if (exist) throw new BusinessException("��ɫ�������Ѵ���");
  56. var group = new RoleGroup
  57. {
  58. Name = request.Name,
  59. Sort = request.Sort
  60. };
  61. group.Id = await InsertAsync(group);
  62. return group;
  63. }
  64. /// <summary>
  65. /// �޸�
  66. /// </summary>
  67. /// <param name="id"></param>
  68. /// <param name="request"></param>
  69. /// <returns></returns>
  70. public async Task<bool> UpdateAsync(int id, RoleGroupAddRequest request)
  71. {
  72. var exist = await IsNameExistsAsync(request.Name);
  73. if (exist) throw new BusinessException("��ɫ�������Ѵ���");
  74. var group = await GetAsync(id);
  75. if (group == null) throw new BusinessException("��ɫ�鲻����");
  76. group.Name = request.Name;
  77. group.Sort = request.Sort;
  78. return await UpdateAsync(group);
  79. }
  80. /// <summary>
  81. /// ɾ��
  82. /// </summary>
  83. /// <param name="id"></param>
  84. /// <returns></returns>
  85. public async Task<bool> DeleteAsync(int id)
  86. {
  87. var group = await GetAsync(id);
  88. if (group == null) throw new BusinessException("��ɫ�鲻����");
  89. return await _repository.DeleteAsync(id);
  90. }
  91. /// <summary>
  92. /// ��ȡ�б�
  93. /// </summary>
  94. /// <returns></returns>
  95. public async Task<List<RoleGroupDetailResult>> GetDetailListAsync()
  96. {
  97. var list = await GetListAsync();
  98. var result = list.Select(item => new RoleGroupDetailResult
  99. {
  100. Id = item.Id,
  101. Name = item.Name,
  102. Sort = item.Sort
  103. }).ToList();
  104. return result;
  105. }
  106. }
  107. }