MiddleLableRepository.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 GxPress.Request.Navigation;
  9. using Microsoft.Extensions.Options;
  10. namespace GxPress.Repository.Implement.Navigation
  11. {
  12. public class MiddleLableRepository : IMiddleLableRepository
  13. {
  14. private readonly Repository<Entity.Navigations.MiddleLable> _repository;
  15. private readonly IMapper _mapper;
  16. private readonly string _connectionString;
  17. private readonly string _databaseTypestr;
  18. public MiddleLableRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  19. {
  20. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  21. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  22. var databaseType =
  23. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  24. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  25. _repository = new Repository<Entity.Navigations.MiddleLable>(database);
  26. _mapper = mapper;
  27. }
  28. public IDatabase Database => _repository.Database;
  29. public string TableName => _repository.TableName;
  30. public List<TableColumn> TableColumns => _repository.TableColumns;
  31. public async Task<Entity.Navigations.MiddleLable> GetAsync(int id)
  32. {
  33. return await _repository.GetAsync(id);
  34. }
  35. public async Task<Entity.Navigations.MiddleLable> GetAsync(string name)
  36. {
  37. return await _repository.GetAsync(Q.Where(nameof(Entity.Navigations.MiddleLable.Name), name));
  38. }
  39. public async Task<bool> DeleteAsync(int id)
  40. {
  41. return await _repository.DeleteAsync(id);
  42. }
  43. public async Task<int> InsertAsync(Entity.Navigations.MiddleLable note)
  44. {
  45. return await _repository.InsertAsync(note);
  46. }
  47. public async Task<bool> UpdateAsync(MiddleLableUpRequest request)
  48. {
  49. if (request.Id > 0)
  50. {
  51. var model = await GetAsync(request.Id);
  52. if (request.IsDisable > 0)
  53. model.IsDisable = request.IsDisable == 1;
  54. if (!string.IsNullOrEmpty(request.Name))
  55. model.Name = request.Name;
  56. if (request.Sort > 0)
  57. model.Sort = request.Sort;
  58. if (request.IsSearch > 0)
  59. model.IsSearch = request.IsSearch == 1;
  60. if (request.IsSlide > 0)
  61. model.IsSlide = request.IsSlide == 1;
  62. if (request.TypeId > 0)
  63. model.TypeId = request.TypeId;
  64. return await _repository.UpdateAsync(model);
  65. }
  66. return false;
  67. }
  68. public async Task<bool> UpdateAsync(SqlKata.Query query)
  69. {
  70. return await _repository.UpdateAsync(query) > 0;
  71. }
  72. public async Task<IEnumerable<Entity.Navigations.MiddleLable>> GetAllAsync()
  73. {
  74. return await _repository.GetAllAsync(Q.Where(nameof(Entity.Navigations.MiddleLable.IsDisable), false).OrderByDesc(nameof(Entity.Navigations.MiddleLable.Sort)));
  75. }
  76. }
  77. }