| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using Dapper;
- using Diligent.WebAPI.Contracts.DTOs.Categories;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class CategoryService : ICategoryService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly UserManager<User> _userManager;
- private readonly IConfiguration _configuration;
-
- public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IConfiguration configuration)
- {
- _context = context;
- _mapper = mapper;
- _userManager = userManager;
- _configuration = configuration;
- }
-
- public async Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int parentCategoryId)
- {
- var user = await _userManager.FindByIdAsync(userId.ToString());
- var role = (await _userManager.GetRolesAsync(user))[0];
-
- if (role == "SuperAdmin")
- if (parentCategoryId == -1)
- return _mapper.Map<List<CategoriesNamesResponse>>
- (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync());
- else
- return await GetChildCategories(parentCategoryId, userId,role);
-
- var userCategories = new List<UserCategories>();
-
- if(parentCategoryId == -1)
- userCategories = await _context.UserCategories
- .Where(k => k.UserId == userId && k.Category.ParentCategory == null)
- .Include(t => t.Category)
- .ToListAsync();
- else
- return await GetChildCategories(parentCategoryId, userId,role);
-
- return GetCategoriesFromUserCategories(userCategories);
- }
- public async Task<Category> GetCategoryEntityById(int? id) =>
- await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
-
- public async Task<List<IsGrantedCategory>> GetCategories(int userId)
- {
- var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync();
- var allCategories = await _context.Categories.ToListAsync();
- var result = new List<IsGrantedCategory>();
- for (int i = 0; i < allCategories.Count; i++)
- {
- var newCategory = new IsGrantedCategory
- {
- Id = allCategories[i].Id,
- Name = allCategories[i].Name,
- };
- bool isGranted = false;
- for (int j = 0; j < grantedCategories.Count; j++)
- {
- if(grantedCategories[j].CategoryId == allCategories[i].Id && userId == grantedCategories[j].UserId)
- {
- isGranted = true;
- break;
- }
- }
- newCategory.IsChecked = isGranted;
- result.Add(newCategory);
- isGranted = false;
- }
-
- return result;
- }
- private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories)
- {
- var res = new List<CategoriesNamesResponse>();
-
- for (int i = 0; i < userCategories.Count; i++)
- {
- res.Add(_mapper.Map<CategoriesNamesResponse>(userCategories[i].Category));
- }
-
- return res;
- }
- private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role)
- {
- if (role == "SuperAdmin")
- return _mapper.Map<List<CategoriesNamesResponse>>(
- await _context.Categories
- .Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId)
- .ToListAsync());
-
- var userCategories = await _context.UserCategories
- .Where(k => k.UserId == userId && k.Category != null && k.Category.ParentCategory != null && k.Category.ParentCategory.Id == parentCategoryId)
- .Include(t => t.Category)
- .ToListAsync();
-
- return GetCategoriesFromUserCategories(userCategories);
- }
- }
- }
|