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

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