MiddleLableNexusRepository.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Repository.Interface.Navigation;
  8. using Microsoft.Extensions.Options;
  9. using System.Transactions;
  10. using GxPress.Request.Navigation;
  11. namespace GxPress.Repository.Implement.Navigation
  12. {
  13. public class MiddleLableNexusRepository : IMiddleLableNexusRepository
  14. {
  15. private readonly Repository<Entity.Navigations.MiddleLableNexus> _repository;
  16. private readonly IMapper _mapper;
  17. public MiddleLableNexusRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  18. {
  19. var databaseType =
  20. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  21. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  22. _repository = new Repository<Entity.Navigations.MiddleLableNexus>(database);
  23. _mapper = mapper;
  24. }
  25. public IDatabase Database => _repository.Database;
  26. public string TableName => _repository.TableName;
  27. public List<TableColumn> TableColumns => _repository.TableColumns;
  28. /// <summary>
  29. /// 添加
  30. /// </summary>
  31. /// <param name="middleLableId"></param>
  32. /// <param name="ids"></param>
  33. /// <returns></returns>
  34. public async Task<bool> InsertAsync(List<MiddleLableNexusUpRequest> middleLableNexusUpRequests)
  35. {
  36. try
  37. {
  38. using (var transactionScope = new TransactionScope())
  39. {
  40. foreach (var item in middleLableNexusUpRequests)
  41. {
  42. await _repository.InsertAsync(_mapper.Map<Entity.Navigations.MiddleLableNexus>(item));
  43. }
  44. transactionScope.Complete();
  45. }
  46. }
  47. catch
  48. {
  49. return false;
  50. }
  51. return true;
  52. }
  53. /// <summary>
  54. /// 查询
  55. /// </summary>
  56. /// <param name="middleLableId"></param>
  57. /// <returns></returns>
  58. public async Task<IEnumerable<Entity.Navigations.MiddleLableNexus>> GetAllTaskAsync(int middleLableId)
  59. {
  60. var query = Q.NewQuery();
  61. query.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId);
  62. query.OrderByDesc(nameof(Entity.Navigations.MiddleLableNexus.Sort));
  63. return await _repository.GetAllAsync(query);
  64. }
  65. public async Task<bool> DeleteByMiddleLableIdAsync(int middleLableId)
  66. {
  67. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Navigations.MiddleLableNexus.MiddleLableId), middleLableId)) > 0;
  68. }
  69. public async Task<bool> UpdateAsync(int middleLableId, List<MiddleLableNexusUpRequest> middleLableNexusUpRequests)
  70. {
  71. try
  72. {
  73. await DeleteByMiddleLableIdAsync(middleLableId);
  74. await InsertAsync(middleLableNexusUpRequests);
  75. }
  76. catch
  77. {
  78. return false;
  79. }
  80. return true;
  81. }
  82. }
  83. }