Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CategoryService.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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<List<CategoriesNamesResponse>> 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. return _mapper.Map<List<CategoriesNamesResponse>>
  32. (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync());
  33. else
  34. return await GetChildCategories(parentCategoryId, userId,role);
  35. var userCategories = new List<UserCategories>();
  36. if(parentCategoryId == -1)
  37. userCategories = await _context.UserCategories
  38. .Where(k => k.UserId == userId && k.Category.ParentCategory == null)
  39. .Include(t => t.Category)
  40. .ToListAsync();
  41. else
  42. return await GetChildCategories(parentCategoryId, userId,role);
  43. return GetCategoriesFromUserCategories(userCategories);
  44. }
  45. public async Task<Category> GetCategoryEntityById(int? id) =>
  46. await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
  47. public async Task<List<IsGrantedCategory>> GetCategories(int userId)
  48. {
  49. var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
  50. var allCategories = await _context.Categories.ToListAsync();
  51. var result = new List<IsGrantedCategory>();
  52. for (int i = 0; i < allCategories.Count; i++)
  53. {
  54. var newCategory = new IsGrantedCategory
  55. {
  56. Id = allCategories[i].Id,
  57. Name = allCategories[i].Name,
  58. };
  59. bool isGranted = false;
  60. for (int j = 0; j < grantedCategories.Count; j++)
  61. {
  62. if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
  63. {
  64. isGranted = true;
  65. break;
  66. }
  67. }
  68. newCategory.IsChecked = isGranted;
  69. result.Add(newCategory);
  70. isGranted = false;
  71. }
  72. return result;
  73. }
  74. private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories)
  75. {
  76. var res = new List<CategoriesNamesResponse>();
  77. for (int i = 0; i < userCategories.Count; i++)
  78. {
  79. res.Add(_mapper.Map<CategoriesNamesResponse>(userCategories[i].Category));
  80. }
  81. return res;
  82. }
  83. private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role)
  84. {
  85. if (role == "SuperAdmin")
  86. return _mapper.Map<List<CategoriesNamesResponse>>(
  87. await _context.Categories
  88. .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId)
  89. .ToListAsync());
  90. var userCategories = await _context.UserCategories
  91. .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId)
  92. .Include(t => t.Category)
  93. .ToListAsync();
  94. return GetCategoriesFromUserCategories(userCategories);
  95. }
  96. }
  97. }