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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Dapper;
  2. using Diligent.WebAPI.Business.Services.Interfaces;
  3. using Diligent.WebAPI.Contracts.DTOs.Categories;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Diligent.WebAPI.Business.Services
  12. {
  13. public class CategoryService : ICategoryService
  14. {
  15. private readonly DatabaseContext _context;
  16. private readonly IMapper _mapper;
  17. private readonly UserManager<User> _userManager;
  18. private readonly IUserService _userService;
  19. public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IUserService userService)
  20. {
  21. _context = context;
  22. _mapper = mapper;
  23. _userManager = userManager;
  24. _userService = userService;
  25. }
  26. public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId)
  27. {
  28. var user = await _userManager.FindByIdAsync(userId.ToString());
  29. var role = (await _userManager.GetRolesAsync(user))[0];
  30. if(parentCategoryId != -1)
  31. {
  32. var categories = await _context.UserCategories.Where(x => x.UserId == userId && x.CategoryId == parentCategoryId).ToListAsync();
  33. if (categories.Count == 0) return null;
  34. }
  35. if (role == "SuperAdmin")
  36. if (parentCategoryId == -1)
  37. {
  38. return new CategoriesParentChild
  39. {
  40. Categories = _mapper.Map<List<CategoriesNamesResponse>>
  41. (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()),
  42. ChildParentRelations = await GetByParentChild(parentCategoryId)
  43. };
  44. }
  45. else
  46. {
  47. return new CategoriesParentChild
  48. {
  49. Categories = await GetChildCategories(parentCategoryId, userId, role),
  50. ChildParentRelations = await GetByParentChild(parentCategoryId)
  51. };
  52. }
  53. var userCategories = new List<UserCategories>();
  54. if(parentCategoryId == -1)
  55. userCategories = await _context.UserCategories
  56. .Where(k => k.UserId == userId && k.Category.ParentCategory == null)
  57. .Include(t => t.Category)
  58. .ToListAsync();
  59. else
  60. return new CategoriesParentChild
  61. {
  62. Categories = await GetChildCategories(parentCategoryId, userId, role),
  63. ChildParentRelations = await GetByParentChild(parentCategoryId)
  64. };
  65. return new CategoriesParentChild
  66. {
  67. Categories = GetCategoriesFromUserCategories(userCategories),
  68. ChildParentRelations = await GetByParentChild(parentCategoryId)
  69. };
  70. }
  71. public async Task<Category> GetCategoryEntityById(int? id) =>
  72. await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
  73. public async Task<List<IsGrantedCategory>> GetCategories(int userId)
  74. {
  75. var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
  76. var allCategories = await _context.Categories.ToListAsync();
  77. var result = new List<IsGrantedCategory>();
  78. for (int i = 0; i < allCategories.Count; i++)
  79. {
  80. var newCategory = new IsGrantedCategory
  81. {
  82. Id = allCategories[i].Id,
  83. Name = allCategories[i].Name,
  84. };
  85. bool isGranted = false;
  86. for (int j = 0; j < grantedCategories.Count; j++)
  87. {
  88. if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
  89. {
  90. isGranted = true;
  91. break;
  92. }
  93. }
  94. newCategory.IsChecked = isGranted;
  95. result.Add(newCategory);
  96. isGranted = false;
  97. }
  98. return result;
  99. }
  100. public async Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId)
  101. {
  102. var user = await _userManager.FindByIdAsync(userId.ToString());
  103. var role = (await _userManager.GetRolesAsync(user))[0];
  104. List<Category> categories = new();
  105. if(role == "SuperAdmin")
  106. {
  107. categories = await _context.Categories.Include(k => k.ParentCategory).ToListAsync();
  108. }
  109. else
  110. {
  111. categories = await getCategoriesUserCanSee(user.Id);
  112. }
  113. List<TreeViewCategoryResponse> response = new();
  114. foreach (var category in categories)
  115. {
  116. TreeViewCategoryResponse treeViewCategory = new();
  117. if(category.ParentCategory == null)
  118. {
  119. treeViewCategory.Id = category.Id;
  120. treeViewCategory.Name = category.Name;
  121. treeViewCategory.TreeViewCategories = GetTreeCategoryItem(categories, category.Id);
  122. response.Add(treeViewCategory);
  123. }
  124. }
  125. return response;
  126. }
  127. private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories)
  128. {
  129. var res = new List<CategoriesNamesResponse>();
  130. for (int i = 0; i < userCategories.Count; i++)
  131. {
  132. res.Add(_mapper.Map<CategoriesNamesResponse>(userCategories[i].Category));
  133. }
  134. return res;
  135. }
  136. private async Task<List<CategoriesNamesResponse>> GetByParentChild(int categoryId)
  137. {
  138. var categories = await _context.Categories.Include(x => x.ParentCategory).ToListAsync();
  139. var categoryById = categories.Where(x => x.Id == categoryId).FirstOrDefault();
  140. List<CategoriesNamesResponse> dto = new();
  141. while (true)
  142. {
  143. if (categoryById == null) break;
  144. dto.Add(new CategoriesNamesResponse
  145. {
  146. Id = categoryById.Id,
  147. Name = categoryById.Name
  148. });
  149. categoryById = categoryById.ParentCategory;
  150. }
  151. dto.Reverse();
  152. return dto;
  153. }
  154. private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role)
  155. {
  156. if (role == "SuperAdmin")
  157. return _mapper.Map<List<CategoriesNamesResponse>>(
  158. await _context.Categories
  159. .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId)
  160. .ToListAsync());
  161. var userCategories = await _context.UserCategories
  162. .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId)
  163. .Include(t => t.Category)
  164. .ToListAsync();
  165. return GetCategoriesFromUserCategories(userCategories);
  166. }
  167. private async Task<List<Category>> getCategoriesUserCanSee(int userId)
  168. {
  169. var k = await _context.UserCategories.Include(c => c.Category).ThenInclude(p => p.ParentCategory).Where(t => t.UserId == userId).ToListAsync();
  170. List<Category> t = new();
  171. foreach (var m in k)
  172. {
  173. Category category = m.Category;
  174. while (true)
  175. {
  176. bool s = false;
  177. for (int i = 0; i < k.Count; i++)
  178. {
  179. if(category.Id == k[i].CategoryId)
  180. {
  181. s = true;
  182. break;
  183. }
  184. }
  185. if (s && category.ParentCategory is null)
  186. {
  187. t.Add(m.Category);
  188. break;
  189. }
  190. else if(!s && category.ParentCategory is null)
  191. {
  192. break;
  193. }
  194. else if(s == true)
  195. {
  196. category = category.ParentCategory;
  197. continue;
  198. }
  199. else
  200. {
  201. break;
  202. }
  203. }
  204. }
  205. return t;
  206. }
  207. private List<TreeViewCategoryResponse> GetTreeCategoryItem(List<Category> items, int id)
  208. {
  209. List<TreeViewCategoryResponse> response = new();
  210. foreach (var item in items)
  211. {
  212. if(item.ParentCategory != null && item.ParentCategory.Id == id)
  213. {
  214. TreeViewCategoryResponse treeViewCategory = new();
  215. treeViewCategory.Id = item.Id;
  216. treeViewCategory.Name = item.Name;
  217. treeViewCategory.TreeViewCategories = GetTreeCategoryItem(items, item.Id);
  218. response.Add(treeViewCategory);
  219. }
  220. }
  221. return response;
  222. }
  223. public async Task CreateAsync(CreateCategoryDto createCategoryDto, int userId)
  224. {
  225. Category category = null;
  226. if(createCategoryDto.ParentId != null)
  227. {
  228. category = await _context.Categories.Where(x => x.Id == createCategoryDto.ParentId).FirstOrDefaultAsync();
  229. }
  230. var newCategory = new Category { Name = createCategoryDto.Name, ParentCategory = category };
  231. await _context.Categories.AddAsync(newCategory);
  232. await _context.SaveChangesAsync();
  233. await _userService.GrantCategoryToUserAsync(new GrantUserCategoryRequestDto { UserId = userId, Categories = new List<GrantCategoryToUserRequestDto> { new GrantCategoryToUserRequestDto { Id = newCategory.Id, IsChecked = true } } });
  234. }
  235. }
  236. }