101 lines
3.1 KiB
C#
101 lines
3.1 KiB
C#
using Azure;
|
||
using Azure.Core;
|
||
using FastEndpoints;
|
||
using MoviconWebApi.API.ClearDataQrApi;
|
||
using MoviconWebApi.Common;
|
||
|
||
namespace MoviconWebApi.API.ClearDataApi
|
||
{
|
||
/// <summary>
|
||
/// 获取清洗数据端点
|
||
/// </summary>
|
||
public class Endpoint : Endpoint<ClearDataRequest, ApiResponse<List<ClearDataResponse>>>
|
||
{
|
||
private readonly IFreeSql _freeSql;
|
||
|
||
/// <summary>
|
||
/// 构造函数,注入FreeSql实例
|
||
/// </summary>
|
||
/// <param name="freeSql">FreeSql实例</param>
|
||
public Endpoint(IFreeSql freeSql)
|
||
{
|
||
_freeSql = freeSql;
|
||
}
|
||
|
||
public override void Configure()
|
||
{
|
||
// 配置路由,支持GET和POST两种方式
|
||
//Post("/cleardata/list");
|
||
Get("/cleardata/listbycode");
|
||
|
||
// 允许匿名访问(根据实际需求可以改为需要认证)
|
||
AllowAnonymous();
|
||
|
||
// 配置摘要信息(用于Swagger文档)
|
||
Summary(s =>
|
||
{
|
||
s.Summary = "获取清洗数据列表";
|
||
s.Description = "根据设备编号、开始时间、结束时间查询清洗数据";
|
||
s.Response< ApiResponse<List<ClearDataResponse>>>(200, "成功返回清洗数据列表");
|
||
s.Response(404, "未找到数据");
|
||
s.Response(500, "服务器内部错误");
|
||
});
|
||
}
|
||
|
||
public override async Task HandleAsync(ClearDataRequest request, CancellationToken ct)
|
||
{
|
||
try
|
||
{
|
||
// 调用Data层方法获取数据
|
||
var dataList = await Data.GetClearData(request, _freeSql);
|
||
|
||
if (dataList == null || dataList.Count == 0)
|
||
{
|
||
// 未找到数据,返回404
|
||
//await Send.NotFoundAsync(ct);
|
||
//Response = dataList ?? new List<ClearDataQrResponse>();
|
||
|
||
//await Send.OkAsync(new List<ClearDataResponse>(), ct);
|
||
|
||
// 没有数据
|
||
Response = ApiResponse<List<ClearDataResponse>>.Success(
|
||
new List<ClearDataResponse>(),
|
||
"暂无数据"
|
||
);
|
||
|
||
}
|
||
else
|
||
{
|
||
//await Send.OkAsync(dataList, ct);
|
||
|
||
// 现在的代码
|
||
Response = ApiResponse<List<ClearDataResponse>>.Success(
|
||
dataList,
|
||
"查询成功"
|
||
);
|
||
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 记录错误日志
|
||
Logger.LogError(ex, "获取清洗数据失败");
|
||
|
||
// 返回错误响应
|
||
Response = ApiResponse<List<ClearDataResponse>>.Error(
|
||
"500",
|
||
"服务器内部错误"
|
||
);
|
||
|
||
//// 记录错误日志
|
||
//Logger.LogError(ex, "获取清洗数据失败");
|
||
|
||
//// 返回500错误
|
||
//await Send.ErrorsAsync(500, ct);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
}
|