ArticleGroupUserRepository.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Exceptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Request.ArticleGroupUser;
  11. using GxPress.Result.ArticleGroupUser;
  12. using Microsoft.Extensions.Options;
  13. using Datory;
  14. namespace GxPress.Repository.Implement
  15. {
  16. public class ArticleGroupUserRepository : IArticleGroupUserRepository
  17. {
  18. private readonly Repository<ArticleGroupUser> _repository;
  19. private readonly Repository<ArticleGroup> _articleGroupRepository;
  20. private readonly Repository<User> _userRepository;
  21. private readonly IMapper _mapper;
  22. public ArticleGroupUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  23. {
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<ArticleGroupUser>(database);
  27. _articleGroupRepository = new Repository<ArticleGroup>(database);
  28. _userRepository = new Repository<User>(database);
  29. _mapper = mapper;
  30. }
  31. public IDatabase Database => _repository.Database;
  32. public string TableName => _repository.TableName;
  33. public List<TableColumn> TableColumns => _repository.TableColumns;
  34. /// <summary>
  35. /// 添加
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. public async Task<int> InsertAsync(ArticleGroupUserInRequest request)
  40. {
  41. var user = await _userRepository.GetAsync(request.UserId);
  42. if (user == null)
  43. throw new BusinessException("用户不存在");
  44. var articleGroup = await _articleGroupRepository.GetAsync(request.ArticleGroupId);
  45. if (articleGroup == null)
  46. throw new BusinessException("频道不存在");
  47. //查询是否存在
  48. var articleGroupUserCount = await _repository.CountAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId)
  49. .Where(nameof(ArticleGroupUser.ArticleGroupId), request.ArticleGroupId));
  50. if (articleGroupUserCount > 0)
  51. throw new BusinessException("频道已存在");
  52. var articleGroupUserEntity = new ArticleGroupUser
  53. {
  54. ArticleGroupId = request.ArticleGroupId,
  55. UserId = request.UserId
  56. };
  57. return await _repository.InsertAsync(articleGroupUserEntity);
  58. }
  59. /// <summary>
  60. /// 删除用户频道
  61. /// </summary>
  62. /// <param name="id"></param>
  63. /// <returns></returns>
  64. public async Task<bool> DeleteAsync(int id)
  65. {
  66. var articleGroup = await _repository.GetAsync(id);
  67. if (articleGroup == null)
  68. throw new BusinessException("频道不存在");
  69. return await _repository.DeleteAsync(id);
  70. }
  71. /// <summary>
  72. /// 根据用户ID查询用户的频道
  73. /// </summary>
  74. /// <param name="request"></param>
  75. /// <returns></returns>
  76. public async Task<IEnumerable<ArticleGroupUserListResult>> GetListAsync(ArticleGroupUserSearchRequest request)
  77. {
  78. var list = await _repository.GetAllAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId));
  79. var listArray = list.Select(n => _mapper.Map<ArticleGroupUserListResult>(n)).ToList();
  80. foreach (var item in listArray)
  81. {
  82. var articleGroup = await _articleGroupRepository.GetAsync(item.ArticleGroupId);
  83. item.ArticleGroupName = articleGroup.Name;
  84. }
  85. return listArray;
  86. }
  87. }
  88. }