|
|
|
@@ -15,14 +15,12 @@ namespace Diligent.WebAPI.Business.Services |
|
|
|
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) |
|
|
|
public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager) |
|
|
|
{ |
|
|
|
_context = context; |
|
|
|
_mapper = mapper; |
|
|
|
_userManager = userManager; |
|
|
|
_configuration = configuration; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId) |
|
|
|
@@ -100,6 +98,37 @@ namespace Diligent.WebAPI.Business.Services |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
public async Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId) |
|
|
|
{ |
|
|
|
var user = await _userManager.FindByIdAsync(userId.ToString()); |
|
|
|
var role = (await _userManager.GetRolesAsync(user))[0]; |
|
|
|
List<Category> categories = new(); |
|
|
|
if(role == "SuperAdmin") |
|
|
|
{ |
|
|
|
categories = await _context.Categories.Include(k => k.ParentCategory).ToListAsync(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
categories = await getCategoriesUserCanSee(user.Id); |
|
|
|
} |
|
|
|
|
|
|
|
List<TreeViewCategoryResponse> response = new(); |
|
|
|
|
|
|
|
foreach (var category in categories) |
|
|
|
{ |
|
|
|
TreeViewCategoryResponse treeViewCategory = new(); |
|
|
|
if(category.ParentCategory == null) |
|
|
|
{ |
|
|
|
treeViewCategory.Id = category.Id; |
|
|
|
treeViewCategory.Name = category.Name; |
|
|
|
treeViewCategory.TreeViewCategories = GetTreeCategoryItem(categories, category.Id); |
|
|
|
response.Add(treeViewCategory); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return response; |
|
|
|
} |
|
|
|
private List<CategoriesNamesResponse> GetCategoriesFromUserCategories(List<UserCategories> userCategories) |
|
|
|
{ |
|
|
|
var res = new List<CategoriesNamesResponse>(); |
|
|
|
@@ -111,22 +140,6 @@ namespace Diligent.WebAPI.Business.Services |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<List<CategoriesNamesResponse>> GetByParentChild(int categoryId) |
|
|
|
{ |
|
|
|
var categories = await _context.Categories.Include(x => x.ParentCategory).ToListAsync(); |
|
|
|
@@ -148,27 +161,34 @@ namespace Diligent.WebAPI.Business.Services |
|
|
|
|
|
|
|
return dto; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<TreeViewCategoryResponse>> GetAllCategories() |
|
|
|
private async Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId, int userId, string role) |
|
|
|
{ |
|
|
|
var res = await _context.Categories.Include(k => k.ParentCategory).ToListAsync(); |
|
|
|
if (role == "SuperAdmin") |
|
|
|
return _mapper.Map<List<CategoriesNamesResponse>>( |
|
|
|
await _context.Categories |
|
|
|
.Where(k => k.ParentCategory != null && k.ParentCategory.Id == parentCategoryId) |
|
|
|
.ToListAsync()); |
|
|
|
|
|
|
|
List<TreeViewCategoryResponse> response = new(); |
|
|
|
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(); |
|
|
|
|
|
|
|
foreach (var item in res) |
|
|
|
return GetCategoriesFromUserCategories(userCategories); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<List<Category>> getCategoriesUserCanSee(int userId) |
|
|
|
{ |
|
|
|
var k = await _context.UserCategories.Include(c => c.Category).ToListAsync(); |
|
|
|
List<Category> t = new(); |
|
|
|
foreach (var m in k) |
|
|
|
{ |
|
|
|
TreeViewCategoryResponse treeViewCategory = new(); |
|
|
|
if(item.ParentCategory == null) |
|
|
|
if(m.UserId == userId) |
|
|
|
{ |
|
|
|
treeViewCategory.Id = item.Id; |
|
|
|
treeViewCategory.Name = item.Name; |
|
|
|
treeViewCategory.TreeViewCategories = GetTreeCategoryItem(res, item.Id); |
|
|
|
response.Add(treeViewCategory); |
|
|
|
t.Add(m.Category); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return response; |
|
|
|
return t; |
|
|
|
} |
|
|
|
|
|
|
|
private List<TreeViewCategoryResponse> GetTreeCategoryItem(List<Category> items, int id) |