73 lines
2.4 KiB
C#
73 lines
2.4 KiB
C#
|
|
using Application.Dto.Icon;
|
||
|
|
using Application;
|
||
|
|
using Service.IconSearch;
|
||
|
|
|
||
|
|
namespace WebApi.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 图标管理控制器
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/icons")]
|
||
|
|
public class IconController(
|
||
|
|
IIconSearchService iconSearchService,
|
||
|
|
ILogger<IconController> logger
|
||
|
|
) : ControllerBase
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 生成搜索关键字
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="request">搜索关键字生成请求</param>
|
||
|
|
/// <returns>搜索关键字生成响应</returns>
|
||
|
|
[HttpPost("search-keywords")]
|
||
|
|
public async Task<BaseResponse<SearchKeywordsResponse>> GenerateSearchKeywordsAsync(
|
||
|
|
[FromBody] SearchKeywordsRequest request)
|
||
|
|
{
|
||
|
|
var keywords = await iconSearchService.GenerateSearchKeywordsAsync(request.CategoryName);
|
||
|
|
logger.LogInformation("为分类 {CategoryName} 生成了 {Count} 个搜索关键字",
|
||
|
|
request.CategoryName, keywords.Count);
|
||
|
|
|
||
|
|
return new SearchKeywordsResponse { Keywords = keywords }.Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 搜索图标
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="request">搜索图标请求</param>
|
||
|
|
/// <returns>图标候选列表响应</returns>
|
||
|
|
[HttpPost("search")]
|
||
|
|
public async Task<BaseResponse<List<IconCandidateDto>>> SearchIconsAsync(
|
||
|
|
[FromBody] SearchIconsRequest request)
|
||
|
|
{
|
||
|
|
var icons = await iconSearchService.SearchIconsAsync(request.Keywords, limit: 20);
|
||
|
|
logger.LogInformation("搜索到 {Count} 个图标候选", icons.Count);
|
||
|
|
|
||
|
|
var iconDtos = icons.Select(i => new IconCandidateDto
|
||
|
|
{
|
||
|
|
CollectionName = i.CollectionName,
|
||
|
|
IconName = i.IconName,
|
||
|
|
IconIdentifier = i.IconIdentifier
|
||
|
|
}).ToList();
|
||
|
|
|
||
|
|
return iconDtos.Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 更新分类图标
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="categoryId">分类ID</param>
|
||
|
|
/// <param name="request">更新分类图标请求</param>
|
||
|
|
/// <returns>操作结果</returns>
|
||
|
|
[HttpPut("categories/{categoryId}/icon")]
|
||
|
|
public async Task<BaseResponse> UpdateCategoryIconAsync(
|
||
|
|
long categoryId,
|
||
|
|
[FromBody] UpdateCategoryIconRequest request)
|
||
|
|
{
|
||
|
|
await iconSearchService.UpdateCategoryIconAsync(categoryId, request.IconIdentifier);
|
||
|
|
logger.LogInformation("更新分类 {CategoryId} 的图标为 {IconIdentifier}",
|
||
|
|
categoryId, request.IconIdentifier);
|
||
|
|
|
||
|
|
return "更新分类图标成功".Ok();
|
||
|
|
}
|
||
|
|
}
|