♻️ feat(调整): 调整过滤器文件位置

This commit is contained in:
bunny 2024-09-11 10:31:03 +08:00
parent 4b774bdfc8
commit 544e8ab7ce
3 changed files with 14 additions and 6 deletions

View File

@ -7,6 +7,9 @@ using Microsoft.AspNetCore.Mvc.Filters;
namespace Bunny.Common.Filter; namespace Bunny.Common.Filter;
/// <summary>
/// 自定事务注解
/// </summary>
public class TransactionIAsyncActionFilter : IAsyncActionFilter public class TransactionIAsyncActionFilter : IAsyncActionFilter
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(TransactionIAsyncActionFilter)); private static readonly ILog Log = LogManager.GetLogger(typeof(TransactionIAsyncActionFilter));
@ -14,25 +17,29 @@ public class TransactionIAsyncActionFilter : IAsyncActionFilter
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ {
var actionDesc = context.ActionDescriptor as ControllerActionDescriptor; var actionDesc = context.ActionDescriptor as ControllerActionDescriptor;
var hasNotTransactionalAttribute = // 如果加上 【TransactionAttribute】 注解才执行操作
actionDesc?.MethodInfo.IsDefined(typeof(TransactionAttribute), false) ?? false; var hasNotTransactionalAttribute = actionDesc?.MethodInfo.IsDefined(typeof(TransactionAttribute), false) ?? false;
// 如果没有加上自定义注解直接跳过
if (hasNotTransactionalAttribute) if (hasNotTransactionalAttribute)
{ {
await next(); await next();
return; return;
} }
// 开启事务
using var txScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); using var txScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
var result = await next(); var result = await next();
// 没有出错,直接标记完成
if (result.Exception == null) if (result.Exception == null)
{ {
txScope.Complete(); txScope.Complete();
} }
else else
{ {
var errorMessage = // 输出执行数据库操作产生事务原因
$"执行数据库出错 用户Id[{BaseContext.GetUserId()}] token [{BaseContext.GetToken()}]:{result.Exception}"; var errorMessage = $"执行数据库出错 用户Id[{BaseContext.GetUserId()}] token [{BaseContext.GetToken()}]:{result.Exception}";
Log.Error(errorMessage); Log.Error(errorMessage);
} }
} }

View File

@ -3,7 +3,7 @@ using Bunny.Dao.Common.Result;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
namespace Bunny.Common.Attribute; namespace Bunny.Common.Filter;
/// <summary> /// <summary>
/// 全局请求表单验证拦截器 /// 全局请求表单验证拦截器
@ -12,6 +12,7 @@ public class ValidateModelStateAttribute : ActionFilterAttribute
{ {
public override void OnActionExecuting(ActionExecutingContext actionContext) public override void OnActionExecuting(ActionExecutingContext actionContext)
{ {
// 验证通过直接跳过
if (actionContext.ModelState.IsValid) return; if (actionContext.ModelState.IsValid) return;
// 表单验证失败 // 表单验证失败

View File

@ -27,7 +27,7 @@ public static partial class NetUtil
.Replace("\\**", ".*") + "$"; .Replace("\\**", ".*") + "$";
// 判断匹配规则是否成立 // 判断匹配规则是否成立
return path.IsNullOrEmpty() ? Regex.IsMatch("", antPattern) : Regex.IsMatch(path!, antPattern); return path.IsNullOrEmpty() ? Regex.IsMatch("", antPattern) : Regex.IsMatch(path, antPattern);
} }
/// <summary> /// <summary>