102 lines
2.8 KiB
C#
102 lines
2.8 KiB
C#
using Application;
|
|
using Application.Dto.Category;
|
|
|
|
namespace WebApi.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]/[action]")]
|
|
public class TransactionCategoryController(
|
|
ITransactionCategoryApplication categoryApplication
|
|
) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// 获取分类列表(支持按类型筛选)
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<BaseResponse<List<CategoryResponse>>> GetListAsync([FromQuery] TransactionType? type = null)
|
|
{
|
|
var categories = await categoryApplication.GetListAsync(type);
|
|
return categories.Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取分类详情
|
|
/// </summary>
|
|
[HttpGet("{id}")]
|
|
public async Task<BaseResponse<CategoryResponse>> GetByIdAsync(long id)
|
|
{
|
|
var category = await categoryApplication.GetByIdAsync(id);
|
|
return category.Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建分类
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse<long>> CreateAsync([FromBody] CreateCategoryRequest request)
|
|
{
|
|
var categoryId = await categoryApplication.CreateAsync(request);
|
|
return categoryId.Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新分类
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse> UpdateAsync([FromBody] UpdateCategoryRequest request)
|
|
{
|
|
await categoryApplication.UpdateAsync(request);
|
|
return "更新分类成功".Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除分类
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse> DeleteAsync([FromQuery] long id)
|
|
{
|
|
await categoryApplication.DeleteAsync(id);
|
|
return BaseResponse.Done();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量创建分类(用于初始化)
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse<int>> BatchCreateAsync([FromBody] List<CreateCategoryRequest> requests)
|
|
{
|
|
var count = await categoryApplication.BatchCreateAsync(requests);
|
|
return count.Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 为指定分类生成新的SVG图标
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse<string>> GenerateIconAsync([FromBody] GenerateIconRequest request)
|
|
{
|
|
var svg = await categoryApplication.GenerateIconAsync(request);
|
|
return svg.Ok<string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新分类的选中图标索引
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<BaseResponse> UpdateSelectedIconAsync([FromBody] UpdateSelectedIconRequest request)
|
|
{
|
|
await categoryApplication.UpdateSelectedIconAsync(request);
|
|
return "更新图标成功".Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除分类图标
|
|
/// </summary>
|
|
[HttpDelete("{id}")]
|
|
public async Task<BaseResponse> DeleteIconAsync(long id)
|
|
{
|
|
await categoryApplication.DeleteIconAsync(id);
|
|
return "删除图标成功".Ok();
|
|
}
|
|
}
|