ソースを参照

Implemented history of nested folders

BE_dev
bronjaermin 2年前
コミット
c7b0dab1bb

+ 48
- 6
Diligent.WebAPI.Business/Services/CategoryService.cs ファイルの表示

@@ -25,17 +25,29 @@ namespace Diligent.WebAPI.Business.Services
_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 role = (await _userManager.GetRolesAsync(user))[0];

if (role == "SuperAdmin")
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
return await GetChildCategories(parentCategoryId, userId,role);
{
return new CategoriesParentChild
{
Categories = await GetChildCategories(parentCategoryId, userId, role),
ChildParentRelations = await GetByParentChild(parentCategoryId)
};
}

var userCategories = new List<UserCategories>();

@@ -45,9 +57,17 @@ namespace Diligent.WebAPI.Business.Services
.Include(t => t.Category)
.ToListAsync();
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) =>
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();
@@ -106,5 +126,27 @@ namespace Diligent.WebAPI.Business.Services

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 ファイルの表示

@@ -9,7 +9,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
{
public interface ICategoryService
{
Task<List<CategoriesNamesResponse>> GetRootCategories(int userId,int categoryId);
Task<CategoriesParentChild> GetRootCategories(int userId,int categoryId);
Task<Category> GetCategoryEntityById(int? id);
Task<List<IsGrantedCategory>> GetCategories(int userId);
}

+ 15
- 0
Diligent.WebAPI.Contracts/DTOs/Categories/CategoriesParentChild.cs ファイルの表示

@@ -0,0 +1,15 @@
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; }
}
}

読み込み中…
キャンセル
保存