SlideRepository.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. namespace GxPress.Repository.Implement
  12. {
  13. public class SlideRepository : ISlideRepository
  14. {
  15. private readonly Repository<Slide> _repository;
  16. private readonly IMapper _mapper;
  17. public SlideRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  18. {
  19. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  20. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  21. _repository = new Repository<Slide>(database);
  22. _mapper = mapper;
  23. }
  24. public IDatabase Database => _repository.Database;
  25. public string TableName => _repository.TableName;
  26. public List<TableColumn> TableColumns => _repository.TableColumns;
  27. public async Task<bool> ExistsAsync(int id)
  28. {
  29. return await _repository.ExistsAsync(id);
  30. }
  31. public async Task<Slide> GetAsync(int id)
  32. {
  33. var slide = await _repository.GetAsync(id);
  34. slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
  35. return slide;
  36. }
  37. public async Task<IEnumerable<Slide>> GetListAsync()
  38. {
  39. var slides= await _repository.GetAllAsync(Q.OrderBy(nameof(Slide.Sort)));
  40. var slideList = slides.ToList();
  41. foreach (var slide in slideList)
  42. {
  43. slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
  44. }
  45. return slideList;
  46. }
  47. /// <summary>
  48. /// 添加
  49. /// </summary>
  50. /// <param name="request"></param>
  51. /// <returns></returns>
  52. public async Task<Slide> AddAsync(Slide request)
  53. {
  54. var sort = await _repository.MaxAsync(nameof(Slide.Sort)) ?? 0;
  55. request.Sort = sort + 1;
  56. request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
  57. request.Id = await _repository.InsertAsync(request);
  58. return request;
  59. }
  60. public async Task<Slide> EditAsync(Slide request)
  61. {
  62. request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
  63. await _repository.UpdateAsync(request);
  64. return request;
  65. }
  66. public async Task<bool> DeleteAsync(int id)
  67. {
  68. return await _repository.DeleteAsync(id);
  69. }
  70. }
  71. }