SystemRoleMenusRepository.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Generic;
  2. using AutoMapper;
  3. using Datory;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Repository.Interface.SystemRoleMenus;
  7. using Microsoft.Extensions.Options;
  8. using System.Transactions;
  9. using System.Threading.Tasks;
  10. using GxPress.Request.Role;
  11. namespace GxPress.Repository.Implement.SystemRoleMenus
  12. {
  13. public class SystemRoleMenusRepository : ISystemRoleMenusRepository
  14. {
  15. private readonly Repository<Entity.SystemRole.SystemRoleMenus> _repository;
  16. private readonly IMapper _mapper;
  17. public SystemRoleMenusRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  18. {
  19. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  20. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  21. _repository = new Repository<Entity.SystemRole.SystemRoleMenus>(database);
  22. _mapper = mapper;
  23. }
  24. public IDatabase Database => _repository.Database;
  25. public string TableName => _repository.TableName;
  26. public List<TableColumn> TableColumns => _repository.TableColumns;
  27. public async Task<IEnumerable<Entity.SystemRole.SystemRoleMenus>> GetAllAsync(int roleId)
  28. {
  29. return await _repository.GetAllAsync(Q.Where(nameof(Entity.SystemRole.SystemRoleMenus.RoleId), roleId));
  30. }
  31. /// <summary>
  32. /// 添加
  33. /// </summary>
  34. /// <param name="models"></param>
  35. /// <returns></returns>
  36. public async Task<bool> InsertAsync(SystemRoleMenusInRequest models)
  37. {
  38. try
  39. {
  40. using (var transactionScope = new TransactionScope())
  41. {
  42. //删除
  43. await _repository.DeleteAsync(Q.Where(nameof(Entity.SystemRole.SystemRoleMenus.RoleId), models.RoleId));
  44. foreach (var item in models.SystemRoleMenuInRequest)
  45. {
  46. var model = new Entity.SystemRole.SystemRoleMenus();
  47. model.RoleId = models.RoleId;
  48. model.MenuId = item.MenuId;
  49. await _repository.InsertAsync(model);
  50. }
  51. transactionScope.Complete();
  52. }
  53. }
  54. catch (System.Exception ex)
  55. {
  56. throw new Common.Exceptions.BusinessException(ex.Message);
  57. }
  58. return true;
  59. }
  60. public async Task<bool> DeleteAsync(int roleId)
  61. {
  62. return await _repository.DeleteAsync(Q.Where(nameof(Entity.SystemRole.SystemRoleMenus.RoleId), roleId)) > 0;
  63. }
  64. }
  65. }