MediaRepository.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.Media;
  9. using GxPress.Result.Media;
  10. using Microsoft.Extensions.Options;
  11. using System.Transactions;
  12. namespace GxPress.Repository.Implement.Media
  13. {
  14. public class MediaRepository : IMediaRepository
  15. {
  16. private readonly Repository<Entity.tede2.Media.Media> _repository;
  17. private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
  18. private readonly IMapper _mapper;
  19. private readonly string _connectionString;
  20. private readonly string _databaseTypestr;
  21. public MediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  22. {
  23. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType =
  26. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Entity.tede2.Media.Media>(database);
  29. mediaLibraryRepository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. public async Task<MediaResult> GetAsync(int id)
  36. {
  37. var result = new MediaResult();
  38. //获取媒体
  39. var media = await _repository.GetAsync(id);
  40. result = _mapper.Map<MediaResult>(media);
  41. //获取媒体库资源
  42. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  43. result.MediaLibraryRequest = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  44. return result;
  45. }
  46. public async Task<bool> DeleteAsync(int id)
  47. {
  48. return await _repository.DeleteAsync(id);
  49. }
  50. /// <summary>
  51. /// 添加媒体
  52. /// </summary>
  53. /// <param name="result"></param>
  54. /// <returns></returns>
  55. public async Task<bool> InsertAsync(MediaResult result)
  56. {
  57. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  58. try
  59. {
  60. using (var transactionScope = new TransactionScope())
  61. {
  62. var id = await _repository.InsertAsync(model);
  63. if (id > 0)
  64. {
  65. foreach (var item in result.MediaLibraryRequest)
  66. {
  67. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  68. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  69. }
  70. }
  71. if(result.Id==0&&result.TeacherId==0&&!string.IsNullOrEmpty(result.Author)){
  72. //新增名师
  73. }
  74. //提交事务
  75. transactionScope.Complete();
  76. }
  77. }
  78. catch (System.Exception ex)
  79. {
  80. throw new Common.Exceptions.BusinessException(ex.Message);
  81. }
  82. return true;
  83. }
  84. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  85. {
  86. return await _repository.UpdateAsync(note);
  87. }
  88. public async Task<bool> UpdateAsync(SqlKata.Query query)
  89. {
  90. return await _repository.UpdateAsync(query) > 0;
  91. }
  92. public async Task<IEnumerable<Entity.tede2.Media.Media>> GetAllAsync(int mediaType)
  93. {
  94. return await _repository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.Media.MediaType), mediaType).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  95. }
  96. public async Task<bool> UpdateAsync(MediaResult result)
  97. {
  98. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  99. try
  100. {
  101. using (var transactionScope = new TransactionScope())
  102. {
  103. await _repository.UpdateAsync(model);
  104. //删除媒体库
  105. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  106. foreach (var item in result.MediaLibraryRequest)
  107. {
  108. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  109. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  110. }
  111. //提交事务
  112. transactionScope.Complete();
  113. }
  114. }
  115. catch (System.Exception ex)
  116. {
  117. throw new Common.Exceptions.BusinessException(ex.Message);
  118. }
  119. return true;
  120. }
  121. }
  122. }