|
|
|
|
|
|
|
|
_configuration = configuration; |
|
|
_configuration = configuration; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int parentCategoryId) |
|
|
|
|
|
|
|
|
public async Task<CategoriesParentChild> GetRootCategories(int userId,int parentCategoryId) |
|
|
{ |
|
|
{ |
|
|
var user = await _userManager.FindByIdAsync(userId.ToString()); |
|
|
var user = await _userManager.FindByIdAsync(userId.ToString()); |
|
|
var role = (await _userManager.GetRolesAsync(user))[0]; |
|
|
var role = (await _userManager.GetRolesAsync(user))[0]; |
|
|
|
|
|
|
|
|
if (role == "SuperAdmin") |
|
|
if (role == "SuperAdmin") |
|
|
if (parentCategoryId == -1) |
|
|
if (parentCategoryId == -1) |
|
|
return _mapper.Map<List<CategoriesNamesResponse>> |
|
|
|
|
|
(await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()); |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
return new CategoriesParentChild |
|
|
|
|
|
{ |
|
|
|
|
|
Categories = _mapper.Map<List<CategoriesNamesResponse>> |
|
|
|
|
|
(await _context.Categories.Where(k => k.ParentCategory == null).ToListAsync()), |
|
|
|
|
|
ChildParentRelations = await GetByParentChild(parentCategoryId) |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
else |
|
|
else |
|
|
return await GetChildCategories(parentCategoryId, userId,role); |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
return new CategoriesParentChild |
|
|
|
|
|
{ |
|
|
|
|
|
Categories = await GetChildCategories(parentCategoryId, userId, role), |
|
|
|
|
|
ChildParentRelations = await GetByParentChild(parentCategoryId) |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var userCategories = new List<UserCategories>(); |
|
|
var userCategories = new List<UserCategories>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.Include(t => t.Category) |
|
|
.Include(t => t.Category) |
|
|
.ToListAsync(); |
|
|
.ToListAsync(); |
|
|
else |
|
|
else |
|
|
return await GetChildCategories(parentCategoryId, userId,role); |
|
|
|
|
|
|
|
|
return new CategoriesParentChild |
|
|
|
|
|
{ |
|
|
|
|
|
Categories = await GetChildCategories(parentCategoryId, userId, role), |
|
|
|
|
|
ChildParentRelations = await GetByParentChild(parentCategoryId) |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
return GetCategoriesFromUserCategories(userCategories); |
|
|
|
|
|
|
|
|
return new CategoriesParentChild |
|
|
|
|
|
{ |
|
|
|
|
|
Categories = GetCategoriesFromUserCategories(userCategories), |
|
|
|
|
|
ChildParentRelations = await GetByParentChild(parentCategoryId) |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
public async Task<Category> GetCategoryEntityById(int? id) => |
|
|
public async Task<Category> GetCategoryEntityById(int? id) => |
|
|
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); |
|
|
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return GetCategoriesFromUserCategories(userCategories); |
|
|
return GetCategoriesFromUserCategories(userCategories); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task<List<CategoriesNamesResponse>> GetByParentChild(int categoryId) |
|
|
|
|
|
{ |
|
|
|
|
|
var categories = await _context.Categories.Include(x => x.ParentCategory).ToListAsync(); |
|
|
|
|
|
var categoryById = categories.Where(x => x.Id == categoryId).FirstOrDefault(); |
|
|
|
|
|
|
|
|
|
|
|
List<CategoriesNamesResponse> dto = new(); |
|
|
|
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
if (categoryById == null) break; |
|
|
|
|
|
dto.Add(new CategoriesNamesResponse |
|
|
|
|
|
{ |
|
|
|
|
|
Id = categoryById.Id, |
|
|
|
|
|
Name = categoryById.Name |
|
|
|
|
|
}); |
|
|
|
|
|
categoryById = categoryById.ParentCategory; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dto.Reverse(); |
|
|
|
|
|
|
|
|
|
|
|
return dto; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |