using Microsoft.AspNetCore.Mvc;
using MyHomePage.Api.Common;
using MyHomePage.Api.Models.Dtos;
using MyHomePage.Api.Services;
namespace MyHomePage.Api.Controllers;
/// 搜索引擎管理。
[ApiController]
[Route("api/search-engines")]
public class SearchEnginesController : ControllerBase
{
private readonly ISearchEngineService _service;
public SearchEnginesController(ISearchEngineService service) => _service = service;
[HttpGet]
public async Task>> List() =>
await ApiResponse>.OkListAsync(_service.ListAsync());
[HttpGet("{id:int}")]
public async Task> GetById(int id)
{
var dto = await _service.GetByIdAsync(id)
?? throw new BusinessException("搜索引擎不存在", 404);
return ApiResponse.Ok(dto);
}
[HttpPost]
public async Task> Create([FromBody] SearchEngineUpsertRequest request) =>
ApiResponse.Ok(await _service.CreateAsync(request));
[HttpPut("{id:int}")]
public async Task> Update(int id, [FromBody] SearchEngineUpsertRequest request) =>
ApiResponse.Ok(await _service.UpdateAsync(id, request));
[HttpDelete("{id:int}")]
public async Task Delete(int id)
{
await _service.DeleteAsync(id);
return ApiResponse.Ok();
}
/// 将指定 ID 的引擎设为默认(唯一)
[HttpPut("{id:int}/default")]
public async Task> SetDefault(int id) =>
ApiResponse.Ok(await _service.SetDefaultAsync(id));
}