minimal api 测试准备
This commit is contained in:
@@ -2,42 +2,30 @@
|
||||
using IRaCIS.Core.Application.Service.BusinessFilter;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Panda.DynamicWebApi;
|
||||
using Panda.DynamicWebApi.Attributes;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
|
||||
namespace IRaCIS.Core.Application.Service
|
||||
namespace IRaCIS.Core.Application.Service;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[TypeFilter(typeof(UnifiedApiResultFilter))]
|
||||
[Authorize, DynamicWebApi]
|
||||
public class BaseService : IDynamicWebApi
|
||||
{
|
||||
|
||||
#pragma warning disable CS8618
|
||||
|
||||
|
||||
#region 非泛型版本
|
||||
|
||||
[TypeFilter(typeof(UnifiedApiResultFilter))]
|
||||
[Authorize, DynamicWebApi]
|
||||
public class BaseService : IBaseService, IDynamicWebApi
|
||||
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
||||
{
|
||||
|
||||
|
||||
public static IResponseOutput Null404NotFound<TEntity>(TEntity? businessObject) where TEntity : class
|
||||
{
|
||||
return new ResponseOutput<string>()
|
||||
.NotOk($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused", code: ApiResponseCodeEnum.DataNotExist);
|
||||
}
|
||||
return new ResponseOutput<string>()
|
||||
.NotOk($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused", code: ApiResponseCodeEnum.DataNotExist);
|
||||
}
|
||||
|
||||
|
||||
public interface IBaseService
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace IRaCIS.Core.Application.Service.BusinessFilter;
|
||||
|
||||
|
||||
#region 控制器流程
|
||||
/// <summary>
|
||||
/// 统一返回前端数据包装,之前在控制器包装,现在修改为动态Api 在ResultFilter这里包装,减少重复冗余代码
|
||||
/// by zhouhang 2021.09.12 周末
|
||||
@@ -120,3 +123,59 @@ public class UnifiedApiResultFilter : Attribute, IAsyncResultFilter
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region minimalapi 流程
|
||||
|
||||
|
||||
public class UnifiedApiResultEndpointFilter : IEndpointFilter
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public UnifiedApiResultEndpointFilter(ILogger<UnifiedApiResultFilter> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
// 调用下一个过滤器或端点处理程序
|
||||
var result = await next(context);
|
||||
|
||||
// 检查返回值类型
|
||||
if (result is null)
|
||||
{
|
||||
return ResponseOutput.NotOk("No data found."); // 处理 null 返回
|
||||
}
|
||||
|
||||
// 如果返回的是元组
|
||||
if (result is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
if (result is IResponseOutput)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 对于其他情况,直接返回结果
|
||||
return ResponseOutput.Ok(result);
|
||||
}
|
||||
|
||||
private IResponseOutput WrapResponse(object? value)
|
||||
{
|
||||
// 处理元组的情况
|
||||
if (value is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
// 包装单个对象
|
||||
return ResponseOutput.Ok(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -9532,6 +9532,16 @@
|
||||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.ReadingQuestionTrialView.ExportIdentification">
|
||||
<summary>
|
||||
导出标识
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.ReadingQuestionTrialView.ExportResult">
|
||||
<summary>
|
||||
导出结果
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.ReadingQuestionTrialView.GroupName">
|
||||
<summary>
|
||||
分组
|
||||
@@ -10132,6 +10142,16 @@
|
||||
图片数量
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionSystemInDto.ExportIdentification">
|
||||
<summary>
|
||||
导出标识
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionSystemInDto.ExportResult">
|
||||
<summary>
|
||||
导出结果
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionSystemInDto.ValueType">
|
||||
<summary>
|
||||
数值类型
|
||||
@@ -10327,6 +10347,16 @@
|
||||
Id
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionTrialInDto.ExportIdentification">
|
||||
<summary>
|
||||
导出标识
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionTrialInDto.ExportResult">
|
||||
<summary>
|
||||
导出结果
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:IRaCIS.Core.Application.Service.Reading.Dto.AddOrUpdateReadingQuestionTrialInDto.HighlightAnswer">
|
||||
<summary>
|
||||
高亮问题的答案
|
||||
|
||||
@@ -3,6 +3,7 @@ using IRaCIS.Application.Contracts;
|
||||
using IRaCIS.Core.Application.BusinessFilter;
|
||||
using IRaCIS.Core.Application.Contracts;
|
||||
using IRaCIS.Core.Application.Helper;
|
||||
using IRaCIS.Core.Application.Service.BusinessFilter;
|
||||
using IRaCIS.Core.Application.ViewModel;
|
||||
using IRaCIS.Core.Domain;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
@@ -34,39 +35,32 @@ namespace IRaCIS.Core.Application.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// minimal api 测试
|
||||
/// 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html
|
||||
/// 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0
|
||||
/// </summary>
|
||||
[ApiExplorerSettings(GroupName = "Institution")]
|
||||
|
||||
public class TestMinimalApiService : ServiceBase
|
||||
{
|
||||
|
||||
public TestMinimalApiService()
|
||||
{
|
||||
RouteHandlerBuilder = t => {
|
||||
t.WithOpenApi();
|
||||
};
|
||||
RouteHandlerBuilder = t =>
|
||||
{
|
||||
t.RequireAuthorization().WithGroupName("Institution").AddEndpointFilter<UnifiedApiResultEndpointFilter>();
|
||||
};
|
||||
}
|
||||
|
||||
[RoutePattern(HttpMethod = "post")]
|
||||
public Task<List<string>> GetProjectList1Async()
|
||||
{
|
||||
var list = new List<string>()
|
||||
{
|
||||
"Auth",
|
||||
"DCC",
|
||||
"PM"
|
||||
};
|
||||
{
|
||||
"Auth",
|
||||
"DCC",
|
||||
"PM"
|
||||
};
|
||||
return Task.FromResult(list);
|
||||
}
|
||||
|
||||
public List<string> GetProjectList()
|
||||
{
|
||||
var list = new List<string>()
|
||||
{
|
||||
"Auth",
|
||||
"DCC",
|
||||
"PM"
|
||||
};
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user