TeacherRepository.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Microsoft.Extensions.Options;
  11. using SqlKata;
  12. namespace GxPress.Repository.Implement.Teacher
  13. {
  14. public class TeacherRepository : ITeacherRepository
  15. {
  16. private readonly Repository<Entity.tede2.Teacher.Teacher> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypestr;
  20. public TeacherRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType =
  25. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<Entity.tede2.Teacher.Teacher>(database);
  28. _mapper = mapper;
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. public async Task<bool> DeleteAsync(int id)
  34. {
  35. return await _repository.DeleteAsync(id);
  36. }
  37. public async Task<PagedList<Entity.tede2.Teacher.Teacher>> GetAllAsync(TeacherRequest request)
  38. {
  39. var result = new PagedList<Entity.tede2.Teacher.Teacher>();
  40. var query = Q.NewQuery();
  41. if (!string.IsNullOrEmpty(request.KeyWord))
  42. query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
  43. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  44. result.Total = await _repository.CountAsync(query);
  45. return result;
  46. }
  47. public async Task<IEnumerable<Entity.tede2.Teacher.Teacher>> GetSearchAllAsync(TeacherRequest request)
  48. {
  49. var query = Q.NewQuery();
  50. query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
  51. var result = await _repository.GetAllAsync(query);
  52. return result;
  53. }
  54. public async Task<Entity.tede2.Teacher.Teacher> GetAsync(int id)
  55. {
  56. return await _repository.GetAsync(id);
  57. }
  58. public async Task<int> InsertAsync(Entity.tede2.Teacher.Teacher note)
  59. {
  60. return await _repository.InsertAsync(note);
  61. }
  62. public async Task<bool> UpdateAsync(Entity.tede2.Teacher.Teacher note)
  63. {
  64. return await _repository.UpdateAsync(note);
  65. }
  66. public async Task<bool> UpdateAsync(Query query)
  67. {
  68. return await _repository.UpdateAsync(query) > 0;
  69. }
  70. }
  71. }