SlideRepository.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Entity;
  8. using GxPress.Repository.Interface;
  9. using Microsoft.Extensions.Options;
  10. using Datory;
  11. using GxPress.Request.Slide;
  12. using GxPress.Common.Page;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class SlideRepository : ISlideRepository
  16. {
  17. private readonly Repository<Slide> _repository;
  18. private readonly IMapper _mapper;
  19. public SlideRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  20. {
  21. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  22. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  23. _repository = new Repository<Slide>(database);
  24. _mapper = mapper;
  25. }
  26. public IDatabase Database => _repository.Database;
  27. public string TableName => _repository.TableName;
  28. public List<TableColumn> TableColumns => _repository.TableColumns;
  29. public async Task<bool> ExistsAsync(int id)
  30. {
  31. return await _repository.ExistsAsync(id);
  32. }
  33. public async Task<Slide> GetAsync(int id)
  34. {
  35. var slide = await _repository.GetAsync(id);
  36. slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
  37. return slide;
  38. }
  39. public async Task<PagedList<Slide>> GetListAsync(SlideSearchRequest request)
  40. {
  41. var result = new PagedList<Slide>();
  42. var query = Q.NewQuery();
  43. if (!string.IsNullOrEmpty(request.KeyWord))
  44. query.WhereLike(nameof(Slide.Title), $"%{request.KeyWord}%");
  45. if (request.TypeId > 0)
  46. query.Where(nameof(Slide.TypeId), request.TypeId);
  47. result.Total = await _repository.CountAsync(query);
  48. var slides = await _repository.GetAllAsync(query.OrderBy(nameof(Slide.Sort)).ForPage(request.Page, request.PerPage));
  49. foreach (var slide in slides)
  50. slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
  51. result.Items = slides;
  52. return result;
  53. }
  54. public async Task<IEnumerable<Slide>> GetListAsync(int typeId)
  55. {
  56. var query = Q.NewQuery();
  57. if (typeId > 0)
  58. query.Where(nameof(Slide.TypeId), typeId);
  59. query.Where(nameof(Entity.Slide.IsDisable), false);
  60. var slides = await _repository.GetAllAsync(query.OrderBy(nameof(Slide.Sort)));
  61. return slides;
  62. }
  63. /// <summary>
  64. /// 添加
  65. /// </summary>
  66. /// <param name="request"></param>
  67. /// <returns></returns>
  68. public async Task<Slide> AddAsync(Slide request)
  69. {
  70. var sort = await _repository.MaxAsync(nameof(Slide.Sort)) ?? 0;
  71. request.Sort = sort + 1;
  72. request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
  73. request.Id = await _repository.InsertAsync(request);
  74. return request;
  75. }
  76. public async Task<Slide> EditAsync(Slide request)
  77. {
  78. request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
  79. await _repository.UpdateAsync(request);
  80. return request;
  81. }
  82. public async Task<bool> DeleteAsync(int id)
  83. {
  84. return await _repository.DeleteAsync(id);
  85. }
  86. }
  87. }