TeacherRepository.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  42. result.Total = await _repository.CountAsync(query);
  43. return result;
  44. }
  45. public async Task<IEnumerable<Entity.tede2.Teacher.Teacher>> GetSearchAllAsync(TeacherRequest request)
  46. {
  47. var query = Q.NewQuery();
  48. query.Where(Q.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name),$"%{request.KeyWord}%"));
  49. var result = await _repository.GetAllAsync(query);
  50. return result;
  51. }
  52. public async Task<Entity.tede2.Teacher.Teacher> GetAsync(int id)
  53. {
  54. return await _repository.GetAsync(id);
  55. }
  56. public async Task<int> InsertAsync(Entity.tede2.Teacher.Teacher note)
  57. {
  58. return await _repository.InsertAsync(note);
  59. }
  60. public async Task<bool> UpdateAsync(Entity.tede2.Teacher.Teacher note)
  61. {
  62. return await _repository.UpdateAsync(note);
  63. }
  64. public async Task<bool> UpdateAsync(Query query)
  65. {
  66. return await _repository.UpdateAsync(query) > 0;
  67. }
  68. }
  69. }