TeacherRepository.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Page;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.Teacher;
  9. using GxPress.Request.TeacherRequest;
  10. using GxPress.Result.Teacher;
  11. using Microsoft.Extensions.Options;
  12. using SqlKata;
  13. namespace GxPress.Repository.Implement.Teacher
  14. {
  15. public class TeacherRepository : ITeacherRepository
  16. {
  17. private readonly Repository<Entity.tede2.Teacher.Teacher> _repository;
  18. private readonly IMapper _mapper;
  19. private readonly string _connectionString;
  20. private readonly string _databaseTypestr;
  21. public TeacherRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  22. {
  23. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType =
  26. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Entity.tede2.Teacher.Teacher>(database);
  29. _mapper = mapper;
  30. }
  31. public IDatabase Database => _repository.Database;
  32. public string TableName => _repository.TableName;
  33. public List<TableColumn> TableColumns => _repository.TableColumns;
  34. public async Task<bool> DeleteAsync(int id)
  35. {
  36. return await _repository.DeleteAsync(id);
  37. }
  38. public async Task<PagedList<Entity.tede2.Teacher.Teacher>> GetAllAsync(TeacherRequest request)
  39. {
  40. var result = new PagedList<Entity.tede2.Teacher.Teacher>();
  41. var query = Q.NewQuery();
  42. if (!string.IsNullOrEmpty(request.KeyWord))
  43. query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
  44. result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.CreatedDate)).ForPage(request.Page, request.PerPage));
  45. result.Total = await _repository.CountAsync(query);
  46. return result;
  47. }
  48. public async Task<IEnumerable<Entity.tede2.Teacher.Teacher>> GetSearchAllAsync(TeacherRequest request)
  49. {
  50. var query = Q.NewQuery();
  51. query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
  52. var result = await _repository.GetAllAsync(query);
  53. return result;
  54. }
  55. public async Task<Entity.tede2.Teacher.Teacher> GetAsync(int id)
  56. {
  57. return await _repository.GetAsync(id);
  58. }
  59. public async Task<int> InsertAsync(Entity.tede2.Teacher.Teacher note)
  60. {
  61. return await _repository.InsertAsync(note);
  62. }
  63. public async Task<bool> UpdateAsync(Entity.tede2.Teacher.Teacher note)
  64. {
  65. return await _repository.UpdateAsync(note);
  66. }
  67. public async Task<bool> UpdateAsync(Query query)
  68. {
  69. return await _repository.UpdateAsync(query) > 0;
  70. }
  71. public async Task<IEnumerable<TeacherResult>> GetTeacherResult()
  72. {
  73. var query = Q.NewQuery();
  74. query.Select(nameof(Entity.tede2.Teacher.Teacher.Id));
  75. query.Select(nameof(Entity.tede2.Teacher.Teacher.Name));
  76. query.Select(nameof(Entity.tede2.Teacher.Teacher.Summary));
  77. query.Select(nameof(Entity.tede2.Teacher.Teacher.ImageUrl));
  78. query.Select(nameof(Entity.tede2.Teacher.Teacher.CollectCount));
  79. query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.Sort));
  80. var result = await _repository.GetAllAsync<TeacherResult>(query);
  81. foreach (var item in result)
  82. {
  83. item.ImageUrl = StringUtils.AddDomain(item.ImageUrl);
  84. }
  85. return result;
  86. }
  87. public async Task<PagedList<TeacherResult>> GetTeacherResult(TeacherRequest request)
  88. {
  89. var result = new PagedList<TeacherResult>();
  90. var query = Q.NewQuery();
  91. query.Select(nameof(Entity.tede2.Teacher.Teacher.Id));
  92. query.Select(nameof(Entity.tede2.Teacher.Teacher.Name));
  93. query.Select(nameof(Entity.tede2.Teacher.Teacher.Summary));
  94. query.Select(nameof(Entity.tede2.Teacher.Teacher.ImageUrl));
  95. query.Select(nameof(Entity.tede2.Teacher.Teacher.CollectCount));
  96. result.Total = await _repository.CountAsync(query);
  97. query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.Sort));
  98. query.ForPage(request.Page, request.PerPage);
  99. result.Items = await _repository.GetAllAsync<TeacherResult>(query);
  100. foreach (var item in result.Items)
  101. {
  102. item.ImageUrl = StringUtils.AddDomain(item.ImageUrl);
  103. }
  104. return result;
  105. }
  106. }
  107. }