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 _userManager; private readonly IConfiguration _configuration; public CategoryService(DatabaseContext context, IMapper mapper, UserManager userManager, IConfiguration configuration) { _context = context; _mapper = mapper; _userManager = userManager; _configuration = configuration; } public async Task> 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> (await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()); else return await GetChildCategories(parentCategoryId, userId,role); var userCategories = new List(); 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 GetCategoryEntityById(int? id) => await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); public async Task> GetCategories(int userId) { var grantedCategories = await _context.UserCategories.Where(k => k.UserId == userId).ToListAsync(); var allCategories = await _context.Categories.ToListAsync(); var result = new List(); 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 GetCategoriesFromUserCategories(List userCategories) { var res = new List(); for (int i = 0; i < userCategories.Count; i++) { res.Add(_mapper.Map(userCategories[i].Category)); } return res; } private async Task> GetChildCategories(int parentCategoryId, int userId, string role) { if (role == "SuperAdmin") return _mapper.Map>( 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); } } }