You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CategoryService.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Dapper;
  2. using Diligent.WebAPI.Contracts.DTOs.Categories;
  3. using Microsoft.Extensions.Configuration;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Diligent.WebAPI.Business.Services
  11. {
  12. public class CategoryService : ICategoryService
  13. {
  14. private readonly DatabaseContext _context;
  15. private readonly IMapper _mapper;
  16. private readonly UserManager<User> _userManager;
  17. private readonly IConfiguration _configuration;
  18. public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IConfiguration configuration)
  19. {
  20. _context = context;
  21. _mapper = mapper;
  22. _userManager = userManager;
  23. _configuration = configuration;
  24. }
  25. public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId)
  26. {
  27. var user = await _userManager.FindByIdAsync(userId.ToString());
  28. var role = (await _userManager.GetRolesAsync(user))[0];
  29. if (role == "SuperAdmin")
  30. if (parentCategoryId == -1)
  31. {
  32. return new CategoriesParentChild
  33. {
  34. Categories = _mapper.Map<List<CategoriesNamesResponse>>
  35. (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()),
  36. ChildParentRelations = await GetByParentChild(parentCategoryId)
  37. };
  38. }
  39. else
  40. {
  41. return new CategoriesParentChild
  42. {
  43. Categories = await GetChildCategories(parentCategoryId, userId, role),
  44. ChildParentRelations = await GetByParentChild(parentCategoryId)
  45. };
  46. }
  47. var userCategories = new List<UserCategories>();
  48. if(parentCategoryId == -1)
  49. userCategories = await _context.UserCategories
  50. .Where(k => k.UserId == userId && k.Category.ParentCategory == null)
  51. .Include(t => t.Category)
  52. .ToListAsync();
  53. else
  54. return new CategoriesParentChild
  55. {
  56. Categories = await GetChildCategories(parentCategoryId, userId, role),
  57. ChildParentRelations = await GetByParentChild(parentCategoryId)
  58. };
  59. return new CategoriesParentChild
  60. {
  61. Categories = GetCategoriesFromUserCategories(userCategories),
  62. ChildParentRelations = await GetByParentChild(parentCategoryId)
  63. };
  64. }
  65. public async Task<Category> GetCategoryEntityById(int? id) =>
  66. await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
  67. public async Task<List<IsGrantedCategory>> GetCategories(int userId)
  68. {
  69. var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
  70. var allCategories = await _context.Categories.ToListAsync();
  71. var result = new List<IsGrantedCategory>();
  72. for (int i = 0; i < allCategories.Count; i++)
  73. {
  74. var newCategory = new IsGrantedCategory
  75. {
  76. Id = allCategories[i].Id,
  77. Name = allCategories[i].Name,
  78. };
  79. bool isGranted = false;
  80. for (int j = 0; j < grantedCategories.Count; j++)
  81. {
  82. if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
  83. {
  84. isGranted = true;
  85. break;
  86. }
  87. }
  88. newCategory.IsChecked = isGranted;
  89. result.Add(newCategory);
  90. isGranted = false;
  91. }
  92. return result;
  93. }
  94. private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories)
  95. {
  96. var res = new List<CategoriesNamesResponse>();
  97. for (int i = 0; i < userCategories.Count; i++)
  98. {
  99. res.Add(_mapper.Map<CategoriesNamesResponse>(userCategories[i].Category));
  100. }
  101. return res;
  102. }
  103. private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role)
  104. {
  105. if (role == "SuperAdmin")
  106. return _mapper.Map<List<CategoriesNamesResponse>>(
  107. await _context.Categories
  108. .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId)
  109. .ToListAsync());
  110. var userCategories = await _context.UserCategories
  111. .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId)
  112. .Include(t => t.Category)
  113. .ToListAsync();
  114. return GetCategoriesFromUserCategories(userCategories);
  115. }
  116. private async Task<List<CategoriesNamesResponse>> GetByParentChild(int categoryId)
  117. {
  118. var categories = await _context.Categories.Include(x => x.ParentCategory).ToListAsync();
  119. var categoryById = categories.Where(x => x.Id == categoryId).FirstOrDefault();
  120. List<CategoriesNamesResponse> dto = new();
  121. while (true)
  122. {
  123. if (categoryById == null) break;
  124. dto.Add(new CategoriesNamesResponse
  125. {
  126. Id = categoryById.Id,
  127. Name = categoryById.Name
  128. });
  129. categoryById = categoryById.ParentCategory;
  130. }
  131. dto.Reverse();
  132. return dto;
  133. }
  134. }
  135. }