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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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>> GetCategoriesNamesAsync(int userId)
  26. {
  27. var user = await _userManager.FindByIdAsync(userId.ToString());
  28. var role = (await _userManager.GetRolesAsync(user))[0];
  29. if(role == "SuperAdmin") return _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync());
  30. else
  31. {
  32. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  33. var sql = @"SELECT C.Id, C.Name FROM AspNetUsers AS U INNER JOIN UserCategories AS UC ON U.Id = UC.UserId
  34. INNER JOIN Categories AS C ON UC.CategoryId = C.Id WHERE U.Id = @UserId";
  35. var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = userId });
  36. var categoriesList = categories.ToList();
  37. return categoriesList;
  38. }
  39. }
  40. public async Task<Category> GetCategoryEntityById(int id) =>
  41. await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
  42. public async Task<List<IsGrantedCategory>> GetCategories(int userId)
  43. {
  44. var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
  45. var allCategories = await _context.Categories.ToListAsync();
  46. var result = new List<IsGrantedCategory>();
  47. for (int i = 0; i < allCategories.Count; i++)
  48. {
  49. var newCategory = new IsGrantedCategory
  50. {
  51. Id = allCategories[i].Id,
  52. Name = allCategories[i].Name,
  53. };
  54. bool isGranted = false;
  55. for (int j = 0; j < grantedCategories.Count; j++)
  56. {
  57. if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
  58. {
  59. isGranted = true;
  60. break;
  61. }
  62. }
  63. newCategory.IsChecked = isGranted;
  64. result.Add(newCategory);
  65. isGranted = false;
  66. }
  67. return result;
  68. }
  69. }
  70. }