Просмотр исходного кода

Implemented history of nested folders

BE_dev
bronjaermin 2 лет назад
Родитель
Сommit
c7b0dab1bb

+ 48
- 6
Diligent.WebAPI.Business/Services/CategoryService.cs Просмотреть файл

_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;
}
} }
} }

+ 1
- 1
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Просмотреть файл

{ {
public interface ICategoryService public interface ICategoryService
{ {
Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int categoryId);
Task<CategoriesParentChild> GetRootCategories(int userId,int categoryId);
Task<Category> GetCategoryEntityById(int? id); Task<Category> GetCategoryEntityById(int? id);
Task<List<IsGrantedCategory>> GetCategories(int userId); Task<List<IsGrantedCategory>> GetCategories(int userId);
} }

+ 15
- 0
Diligent.WebAPI.Contracts/DTOs/Categories/CategoriesParentChild.cs Просмотреть файл

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Contracts.DTOs.Categories
{
public class CategoriesParentChild
{
public List<CategoriesNamesResponse> Categories { get; set; }

public List<CategoriesNamesResponse> ChildParentRelations { get; set; }
}
}

Загрузка…
Отмена
Сохранить