fengling-risk-control/Fengling.RiskControl.Web/Endpoints/LotteryEndpoints.cs

98 lines
2.9 KiB
C#

using FastEndpoints;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Fengling.RiskControl.Application.Services;
using Fengling.RiskControl.Domain.Aggregates.LotteryActivities;
namespace Fengling.RiskControl.Web.Endpoints;
public class ParticipateLotteryEndpoint : Endpoint<ParticipateLotteryRequest, LotteryParticipationResult>
{
private readonly IMediator _mediator;
public ParticipateLotteryEndpoint(IMediator mediator)
{
_mediator = mediator;
}
public override void Configure()
{
Post("/api/v1/lottery/participate");
Summary(s => {
s.Summary = "参与抽奖";
s.Description = "会员参与抽奖活动,系统自动进行风险评估";
});
}
public override async Task HandleAsync(ParticipateLotteryRequest req, CancellationToken ct)
{
var result = await _mediator.Send(new LotteryParticipationRequest
{
MemberId = req.MemberId,
ActivityType = req.ActivityType,
StakePoints = req.StakePoints,
IpAddress = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown",
DeviceId = HttpContext.Request.Headers["X-Device-ID"].FirstOrDefault()
}, ct);
Response = (LotteryParticipationResult)result!;
}
}
public class ParticipateLotteryRequest
{
public long MemberId { get; set; }
public string ActivityType { get; set; } = string.Empty;
public int StakePoints { get; set; }
}
public class GetLotteryHistoryEndpoint : Endpoint<GetLotteryHistoryRequest, IEnumerable<LotteryActivityResponse>>
{
private readonly IMediator _mediator;
public GetLotteryHistoryEndpoint(IMediator mediator)
{
_mediator = mediator;
}
public override void Configure()
{
Get("/api/v1/lottery/history/{MemberId}");
}
public override async Task HandleAsync(GetLotteryHistoryRequest req, CancellationToken ct)
{
var activities = await _mediator.Send(new GetMemberLotteriesQuery { MemberId = req.MemberId }, ct);
Response = activities.Select(a => new LotteryActivityResponse
{
Id = a.Id,
ActivityType = a.ActivityType,
StakePoints = a.StakePoints,
WinAmount = a.WinAmount,
Status = a.Status.ToString(),
CreatedAt = a.CreatedAt
});
}
}
public class GetLotteryHistoryRequest
{
[Microsoft.AspNetCore.Mvc.FromRoute]
public long MemberId { get; set; }
}
public record GetMemberLotteriesQuery : IRequest<IEnumerable<LotteryActivityResponse>>
{
public long MemberId { get; init; }
}
public record LotteryActivityResponse
{
public long Id { get; init; }
public string ActivityType { get; init; } = string.Empty;
public int StakePoints { get; init; }
public int? WinAmount { get; init; }
public string Status { get; init; } = string.Empty;
public DateTime CreatedAt { get; init; }
}