ArticleGroupRepository.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using GxPress.Common.AppOptions;
  4. using GxPress.Common.Exceptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity;
  7. using GxPress.Repository.Interface;
  8. using GxPress.Request.ArticleGroup;
  9. using Microsoft.Extensions.Options;
  10. using Datory;
  11. using Dapper;
  12. namespace GxPress.Repository.Implement
  13. {
  14. public class ArticleGroupRepository : IArticleGroupRepository
  15. {
  16. private readonly Repository<ArticleGroup> _repository;
  17. private readonly string _connectionString;
  18. private readonly string _databaseTypeStr;
  19. public ArticleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
  20. {
  21. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  22. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  23. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  24. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  25. _repository = new Repository<ArticleGroup>(database);
  26. }
  27. public IDatabase Database => _repository.Database;
  28. public string TableName => _repository.TableName;
  29. public List<TableColumn> TableColumns => _repository.TableColumns;
  30. public async Task<bool> ExistsAsync(int id)
  31. {
  32. return await _repository.ExistsAsync(id);
  33. }
  34. public async Task<bool> IsNameExistsAsync(string name)
  35. {
  36. return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
  37. }
  38. public async Task<int> InsertAsync(ArticleGroup processGroup)
  39. {
  40. return await _repository.InsertAsync(processGroup);
  41. }
  42. public async Task<ArticleGroup> GetAsync(int id)
  43. {
  44. return await _repository.GetAsync(id);
  45. }
  46. public async Task<bool> UpdateAsync(ArticleGroup processGroup)
  47. {
  48. return await _repository.UpdateAsync(processGroup);
  49. }
  50. public async Task<IEnumerable<ArticleGroup>> GetListAsync(int userId)
  51. {
  52. string sql = $@"select * from ccpph.tede_article_group where Id not in(SELECT ArticleGroupId FROM ccpph.tede_article_group_user where UserId={userId})";
  53. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  54. var database = new Database(databaseType, _connectionString);
  55. var connection = database.GetConnection();
  56. var result = await connection.QueryAsync<ArticleGroup>(sql);
  57. return result;
  58. }
  59. /// <summary>
  60. /// 添加
  61. /// </summary>
  62. /// <param name="request"></param>
  63. /// <returns></returns>
  64. public async Task<ArticleGroup> AddAsync(ArticleGroupAddRequest request)
  65. {
  66. var exist = await IsNameExistsAsync(request.Name);
  67. if (exist) throw new BusinessException("分组名不能重复");
  68. var group = new ArticleGroup
  69. {
  70. Name = request.Name,
  71. Sort = request.Sort
  72. };
  73. group.Id = await InsertAsync(group);
  74. return group;
  75. }
  76. /// <summary>
  77. /// 修改
  78. /// </summary>
  79. /// <param name="id"></param>
  80. /// <param name="request"></param>
  81. /// <returns></returns>
  82. public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
  83. {
  84. var group = await GetAsync(id);
  85. if (group == null) throw new BusinessException("该分组不存在");
  86. if (group.Name != request.Name)
  87. {
  88. var exist = await IsNameExistsAsync(request.Name);
  89. if (exist) throw new BusinessException("分组名不能重复");
  90. }
  91. group.Name = request.Name;
  92. group.Sort = request.Sort;
  93. return await UpdateAsync(group);
  94. }
  95. /// <summary>
  96. /// 删除
  97. /// </summary>
  98. /// <param name="id"></param>
  99. /// <returns></returns>
  100. public async Task<bool> DeleteAsync(int id)
  101. {
  102. var group = await GetAsync(id);
  103. if (group == null) throw new BusinessException("该文章组不存在");
  104. return await _repository.DeleteAsync(id);
  105. }
  106. }
  107. }