87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using FastEndpoints;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Fengling.RiskControl.Domain.Aggregates.RiskRules;
|
|
|
|
namespace Fengling.RiskControl.Web.Endpoints;
|
|
|
|
public class CreateRiskRuleEndpoint : Endpoint<CreateRiskRuleRequest, RiskRuleResponse>
|
|
{
|
|
private readonly IMediator _mediator;
|
|
|
|
public CreateRiskRuleEndpoint(IMediator mediator)
|
|
{
|
|
_mediator = mediator;
|
|
}
|
|
|
|
public override void Configure()
|
|
{
|
|
Post("/api/v1/risk/rules");
|
|
Roles("Admin");
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateRiskRuleRequest req, CancellationToken ct)
|
|
{
|
|
if (!Enum.IsDefined(typeof(RiskRuleType), req.RuleType))
|
|
{
|
|
ThrowError($"无效的规则类型: {req.RuleType}");
|
|
}
|
|
if (!Enum.IsDefined(typeof(RiskRuleAction), req.Action))
|
|
{
|
|
ThrowError($"无效的规则动作: {req.Action}");
|
|
}
|
|
|
|
var rule = await _mediator.Send(new CreateRiskRuleCommand
|
|
{
|
|
Name = req.Name,
|
|
Description = req.Description,
|
|
RuleType = (RiskRuleType)req.RuleType,
|
|
Action = (RiskRuleAction)req.Action,
|
|
ConfigJson = req.ConfigJson,
|
|
Priority = req.Priority
|
|
}, ct);
|
|
|
|
Response = new RiskRuleResponse
|
|
{
|
|
Id = rule.Id,
|
|
Name = rule.Name,
|
|
Description = rule.Description,
|
|
RuleType = ((int)rule.RuleType).ToString(),
|
|
Action = ((int)rule.Action).ToString(),
|
|
IsActive = rule.IsActive,
|
|
CreatedAt = rule.CreatedAt
|
|
};
|
|
}
|
|
}
|
|
|
|
public class CreateRiskRuleRequest
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Description { get; set; } = string.Empty;
|
|
public int RuleType { get; set; }
|
|
public int Action { get; set; }
|
|
public string ConfigJson { get; set; } = string.Empty;
|
|
public int Priority { get; set; }
|
|
}
|
|
|
|
public record CreateRiskRuleCommand : IRequest<RiskRule>
|
|
{
|
|
public string Name { get; init; } = string.Empty;
|
|
public string Description { get; init; } = string.Empty;
|
|
public RiskRuleType RuleType { get; init; }
|
|
public RiskRuleAction Action { get; init; }
|
|
public string ConfigJson { get; init; } = string.Empty;
|
|
public int Priority { get; init; }
|
|
}
|
|
|
|
public record RiskRuleResponse
|
|
{
|
|
public long Id { get; init; }
|
|
public string Name { get; init; } = string.Empty;
|
|
public string Description { get; init; } = string.Empty;
|
|
public string RuleType { get; init; } = string.Empty;
|
|
public string Action { get; init; } = string.Empty;
|
|
public bool IsActive { get; init; }
|
|
public DateTime CreatedAt { get; init; }
|
|
}
|