ArticleLabelRepository.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Dapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.ArticleLabel;
  9. using GxPress.Result.App.ArticleLabel;
  10. using Microsoft.Extensions.Options;
  11. using SqlKata;
  12. namespace GxPress.Repository.Implement.ArticleLabel
  13. {
  14. public class ArticleLabelRepository : IArticleLabelRepository
  15. {
  16. private readonly Repository<Entity.ArticleLabel.ArticleLabel> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypeStr;
  20. public ArticleLabelRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<Entity.ArticleLabel.ArticleLabel>(database);
  27. _mapper = mapper;
  28. }
  29. public IDatabase Database => _repository.Database;
  30. public string TableName => _repository.TableName;
  31. public List<TableColumn> TableColumns => _repository.TableColumns;
  32. public async Task<bool> DeleteAsync(Query query)
  33. {
  34. return await _repository.DeleteAsync(query) > 0;
  35. }
  36. public async Task<IEnumerable<Entity.ArticleLabel.ArticleLabel>> GetAllAsync(Query query)
  37. {
  38. return await _repository.GetAllAsync(query);
  39. }
  40. public async Task<Entity.ArticleLabel.ArticleLabel> GetAsync(int id)
  41. {
  42. return await _repository.GetAsync(id);
  43. }
  44. public async Task<Entity.ArticleLabel.ArticleLabel> GetAsync(SqlKata.Query query)
  45. {
  46. return await _repository.GetAsync(query);
  47. }
  48. public async Task<int> InsertAsync(Entity.ArticleLabel.ArticleLabel articleLabel)
  49. {
  50. return await _repository.InsertAsync(articleLabel);
  51. }
  52. public async Task<bool> UpdateAsync(Entity.ArticleLabel.ArticleLabel articleLabel)
  53. {
  54. return await _repository.UpdateAsync(articleLabel);
  55. }
  56. public async Task<bool> UpdateAsync(Query query)
  57. {
  58. return await _repository.UpdateAsync(query) > 0;
  59. }
  60. /// <summary>
  61. /// 根据文章ID获取文章标签
  62. /// </summary>
  63. /// <param name="articleId"></param>
  64. /// <returns></returns>
  65. public async Task<IEnumerable<ArticleInnerLabelResult>> GetArticleLabelsAsync(int articleId)
  66. {
  67. var sql = $"select a.*,b.LabelName from tede_article_inner_label a inner join tede_article_label b on a.ArticleLabelId=b.Id where a.ArticleId={articleId}";
  68. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  69. var database = new Database(databaseType, _connectionString);
  70. var connection = database.GetConnection();
  71. var items = await connection
  72. .QueryAsync<ArticleInnerLabelResult, Entity.ArticleLabel.ArticleLabel, ArticleInnerLabelResult>(sql,
  73. (articleInnerLabelResult, articleLabel) =>
  74. {
  75. articleInnerLabelResult.LabelName = articleLabel != null ? articleLabel.LabelName : "";
  76. return articleInnerLabelResult;
  77. },
  78. splitOn: "LabelName");
  79. return items;
  80. }
  81. }
  82. }