Преглед на файлове

permissions on categories for users

BE_dev
Dzenis Hadzifejzovic преди 2 години
родител
ревизия
c84d158bdf

+ 53
- 33
Diligent.WebAPI.Business/Services/CategoryService.cs Целия файл

@@ -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)

+ 1
- 1
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Целия файл

@@ -12,6 +12,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
Task<CategoriesParentChild> GetRootCategories(int userId,int categoryId);
Task<Category> GetCategoryEntityById(int? id);
Task<List<IsGrantedCategory>> GetCategories(int userId);
Task<List<TreeViewCategoryResponse>> GetAllCategories();
Task<List<TreeViewCategoryResponse>> GetAllCategories(int userId);
}
}

+ 7
- 3
Diligent.WebAPI.Host/Controllers/V1/CategoriesController.cs Целия файл

@@ -18,7 +18,7 @@ namespace Diligent.WebAPI.Host.Controllers.V1
public async Task<IActionResult> GetRootCategories(int parentCategoryId = -1)
{
User? user = (User?)HttpContext.Items["User"];
return Ok(await _categoryService.GetRootCategories(8,parentCategoryId));
return Ok(await _categoryService.GetRootCategories(user.Id,parentCategoryId));
}

[HttpGet("granted-categories")]
@@ -26,7 +26,11 @@ namespace Diligent.WebAPI.Host.Controllers.V1
Ok(await _categoryService.GetCategories(userId));

[HttpGet("all-categories")]
public async Task<IActionResult> GetAllCategories() =>
Ok(await _categoryService.GetAllCategories());
public async Task<IActionResult> GetAllCategories()
{
User? user = (User?)HttpContext.Items["User"];
return Ok(await _categoryService.GetAllCategories(user.Id));
}
}
}

Loading…
Отказ
Запис