MissiveAnalyzeRepository.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Entity.Missive;
  8. using GxPress.Repository.Interface.Missive;
  9. using GxPress.Request.App.Missive;
  10. using Microsoft.Extensions.Options;
  11. using SqlKata;
  12. namespace GxPress.Repository.Implement.Missive
  13. {
  14. public class MissiveAnalyzeRepository : IMissiveAnalyzeRepository
  15. {
  16. private readonly Repository<MissiveAnalyze> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypeStr;
  20. public MissiveAnalyzeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType =
  25. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<MissiveAnalyze>(database);
  28. _mapper = mapper;
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. public async Task<int> InsertAsync(MissiveAnalyze missiveAnalyze)
  34. {
  35. return await _repository.InsertAsync(missiveAnalyze);
  36. }
  37. public async Task<MissiveAnalyze> GetAsync(int id)
  38. {
  39. return await _repository.GetAsync(id);
  40. }
  41. public async Task<MissiveAnalyze> GetAsync(Query query)
  42. {
  43. return await _repository.GetAsync(query);
  44. }
  45. public async Task<bool> UpdateAsync(MissiveAnalyze missiveAnalyze)
  46. {
  47. return await _repository.UpdateAsync(missiveAnalyze);
  48. }
  49. public async Task<bool> DeleteAsync(Query query)
  50. {
  51. return await _repository.DeleteAsync(query) > 0;
  52. }
  53. public async Task<bool> UpdateAsync(Query query)
  54. {
  55. return await _repository.UpdateAsync(query) > 0;
  56. }
  57. public async Task<IEnumerable<MissiveAnalyze>> GetAllAsync(Query query)
  58. {
  59. return await _repository.GetAllAsync(query);
  60. }
  61. public async Task<int> GetCount(Query query)
  62. {
  63. return await _repository.CountAsync(query);
  64. }
  65. /// <summary>
  66. /// 设置 点赞 转发 收藏
  67. /// </summary>
  68. /// /// <param name="request"></param>
  69. /// <returns></returns>
  70. public async Task<bool> SetMissiveAnalyzeAsync(MissiveAnalyzeRequest request)
  71. {
  72. var topicAnalyze = await _repository.GetAsync(Q
  73. .Where(nameof(MissiveAnalyze.AnalyzeType), request.MissiveAnalyzeType)
  74. .Where(nameof(MissiveAnalyze.MissiveId), request.MissiveId)
  75. .Where(nameof(MissiveAnalyze.UserId), request.UserId));
  76. if (topicAnalyze != null && topicAnalyze.Id > 0)
  77. return await _repository.DeleteAsync(topicAnalyze.Id);
  78. var missiveAnalyze = new MissiveAnalyze
  79. {
  80. AnalyzeType = request.MissiveAnalyzeType,
  81. MissiveId = request.MissiveId,
  82. UserId = request.UserId
  83. };
  84. return await InsertAsync(missiveAnalyze) > 0;
  85. }
  86. public async Task<int> CountAsync(SqlKata.Query query)
  87. {
  88. return await _repository.CountAsync(query);
  89. }
  90. }
  91. }