123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Page;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.Teacher;
- using GxPress.Request.TeacherRequest;
- using GxPress.Result.Teacher;
- using Microsoft.Extensions.Options;
- using SqlKata;
- namespace GxPress.Repository.Implement.Teacher
- {
- public class TeacherRepository : ITeacherRepository
- {
- private readonly Repository<Entity.tede2.Teacher.Teacher> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public TeacherRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType =
- StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.tede2.Teacher.Teacher>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- public async Task<PagedList<Entity.tede2.Teacher.Teacher>> GetAllAsync(TeacherRequest request)
- {
- var result = new PagedList<Entity.tede2.Teacher.Teacher>();
- var query = Q.NewQuery();
- if (!string.IsNullOrEmpty(request.KeyWord))
- query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
- result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.CreatedDate)).ForPage(request.Page, request.PerPage));
- result.Total = await _repository.CountAsync(query);
- return result;
- }
- public async Task<IEnumerable<Entity.tede2.Teacher.Teacher>> GetSearchAllAsync(TeacherRequest request)
- {
- var query = Q.NewQuery();
- query.WhereLike(nameof(Entity.tede2.Teacher.Teacher.Name), $"%{request.KeyWord}%");
- var result = await _repository.GetAllAsync(query);
- return result;
- }
- public async Task<Entity.tede2.Teacher.Teacher> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<int> InsertAsync(Entity.tede2.Teacher.Teacher note)
- {
- return await _repository.InsertAsync(note);
- }
- public async Task<bool> UpdateAsync(Entity.tede2.Teacher.Teacher note)
- {
- return await _repository.UpdateAsync(note);
- }
- public async Task<bool> UpdateAsync(Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- public async Task<IEnumerable<TeacherResult>> GetTeacherResult()
- {
- var query = Q.NewQuery();
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Id));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Name));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Summary));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.ImageUrl));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.CollectCount));
- query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.Sort));
- var result = await _repository.GetAllAsync<TeacherResult>(query);
- foreach (var item in result)
- {
- item.ImageUrl = StringUtils.AddDomain(item.ImageUrl);
- }
- return result;
- }
- public async Task<PagedList<TeacherResult>> GetTeacherResult(TeacherRequest request)
- {
- var result = new PagedList<TeacherResult>();
- var query = Q.NewQuery();
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Id));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Name));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.Summary));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.ImageUrl));
- query.Select(nameof(Entity.tede2.Teacher.Teacher.CollectCount));
- result.Total = await _repository.CountAsync(query);
- query.OrderByDesc(nameof(Entity.tede2.Teacher.Teacher.Sort));
- query.ForPage(request.Page, request.PerPage);
- result.Items = await _repository.GetAllAsync<TeacherResult>(query);
- foreach (var item in result.Items)
- {
- item.ImageUrl = StringUtils.AddDomain(item.ImageUrl);
- }
- return result;
- }
- }
- }
|