功能添加
This commit is contained in:
@@ -8,36 +8,23 @@ public class TransactionCategoryController(
|
||||
) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取分类树(支持按类型筛选)
|
||||
/// 获取分类列表(支持按类型筛选)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategoryTreeDto>>> GetTreeAsync([FromQuery] TransactionType? type = null)
|
||||
public async Task<BaseResponse<List<TransactionCategory>>> GetListAsync([FromQuery] TransactionType? type = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tree = await categoryRepository.GetCategoryTreeAsync(type);
|
||||
return new BaseResponse<List<TransactionCategoryTreeDto>>
|
||||
List<TransactionCategory> categories;
|
||||
if (type.HasValue)
|
||||
{
|
||||
Success = true,
|
||||
Data = tree
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取分类树失败");
|
||||
return BaseResponse<List<TransactionCategoryTreeDto>>.Fail($"获取分类树失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取顶级分类列表(按类型)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategory>>> GetTopLevelAsync([FromQuery] TransactionType type)
|
||||
{
|
||||
try
|
||||
{
|
||||
var categories = await categoryRepository.GetTopLevelCategoriesByTypeAsync(type);
|
||||
categories = await categoryRepository.GetCategoriesByTypeAsync(type.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
categories = (await categoryRepository.GetAllAsync()).ToList();
|
||||
}
|
||||
|
||||
return new BaseResponse<List<TransactionCategory>>
|
||||
{
|
||||
Success = true,
|
||||
@@ -46,30 +33,8 @@ public class TransactionCategoryController(
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取顶级分类失败, Type: {Type}", type);
|
||||
return BaseResponse<List<TransactionCategory>>.Fail($"获取顶级分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取子分类列表
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<List<TransactionCategory>>> GetChildrenAsync([FromQuery] long parentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var categories = await categoryRepository.GetChildCategoriesAsync(parentId);
|
||||
return new BaseResponse<List<TransactionCategory>>
|
||||
{
|
||||
Success = true,
|
||||
Data = categories
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取子分类失败, ParentId: {ParentId}", parentId);
|
||||
return BaseResponse<List<TransactionCategory>>.Fail($"获取子分类失败: {ex.Message}");
|
||||
logger.LogError(ex, "获取分类列表失败");
|
||||
return BaseResponse<List<TransactionCategory>>.Fail($"获取分类列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,21 +74,16 @@ public class TransactionCategoryController(
|
||||
try
|
||||
{
|
||||
// 检查同名分类
|
||||
var existing = await categoryRepository.GetByNameAndParentAsync(dto.Name, dto.ParentId, dto.Type);
|
||||
var existing = await categoryRepository.GetByNameAndTypeAsync(dto.Name, dto.Type);
|
||||
if (existing != null)
|
||||
{
|
||||
return BaseResponse<long>.Fail("同级已存在相同名称的分类");
|
||||
return BaseResponse<long>.Fail("已存在相同名称的分类");
|
||||
}
|
||||
|
||||
var category = new TransactionCategory
|
||||
{
|
||||
Name = dto.Name,
|
||||
ParentId = dto.ParentId,
|
||||
Type = dto.Type,
|
||||
Level = dto.Level,
|
||||
SortOrder = dto.SortOrder,
|
||||
Icon = dto.Icon,
|
||||
Remark = dto.Remark
|
||||
Type = dto.Type
|
||||
};
|
||||
|
||||
var result = await categoryRepository.AddAsync(category);
|
||||
@@ -164,18 +124,14 @@ public class TransactionCategoryController(
|
||||
// 如果修改了名称,检查同名
|
||||
if (category.Name != dto.Name)
|
||||
{
|
||||
var existing = await categoryRepository.GetByNameAndParentAsync(dto.Name, category.ParentId, category.Type);
|
||||
var existing = await categoryRepository.GetByNameAndTypeAsync(dto.Name, category.Type);
|
||||
if (existing != null && existing.Id != dto.Id)
|
||||
{
|
||||
return BaseResponse.Fail("同级已存在相同名称的分类");
|
||||
return BaseResponse.Fail("已存在相同名称的分类");
|
||||
}
|
||||
}
|
||||
|
||||
category.Name = dto.Name;
|
||||
category.SortOrder = dto.SortOrder;
|
||||
category.Icon = dto.Icon;
|
||||
category.IsEnabled = dto.IsEnabled;
|
||||
category.Remark = dto.Remark;
|
||||
category.UpdateTime = DateTime.Now;
|
||||
|
||||
var success = await categoryRepository.UpdateAsync(category);
|
||||
@@ -203,13 +159,6 @@ public class TransactionCategoryController(
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查是否有子分类
|
||||
var children = await categoryRepository.GetChildCategoriesAsync(id);
|
||||
if (children.Any())
|
||||
{
|
||||
return BaseResponse.Fail("该分类下存在子分类,无法删除");
|
||||
}
|
||||
|
||||
// 检查是否被使用
|
||||
var inUse = await categoryRepository.IsCategoryInUseAsync(id);
|
||||
if (inUse)
|
||||
@@ -245,12 +194,7 @@ public class TransactionCategoryController(
|
||||
var categories = dtoList.Select(dto => new TransactionCategory
|
||||
{
|
||||
Name = dto.Name,
|
||||
ParentId = dto.ParentId,
|
||||
Type = dto.Type,
|
||||
Level = dto.Level,
|
||||
SortOrder = dto.SortOrder,
|
||||
Icon = dto.Icon,
|
||||
Remark = dto.Remark
|
||||
Type = dto.Type
|
||||
}).ToList();
|
||||
|
||||
var result = await categoryRepository.AddRangeAsync(categories);
|
||||
@@ -280,12 +224,7 @@ public class TransactionCategoryController(
|
||||
/// </summary>
|
||||
public record CreateCategoryDto(
|
||||
string Name,
|
||||
long ParentId,
|
||||
TransactionType Type,
|
||||
int Level,
|
||||
int SortOrder = 0,
|
||||
string? Icon = null,
|
||||
string? Remark = null
|
||||
TransactionType Type
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
@@ -293,9 +232,5 @@ public record CreateCategoryDto(
|
||||
/// </summary>
|
||||
public record UpdateCategoryDto(
|
||||
long Id,
|
||||
string Name,
|
||||
int SortOrder,
|
||||
string? Icon,
|
||||
bool IsEnabled,
|
||||
string? Remark
|
||||
string Name
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user