89 lines
2.3 KiB
C#
89 lines
2.3 KiB
C#
|
|
using FateMaster.API.Data;
|
||
|
|
using FateMaster.API.Models;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace FateMaster.API.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class DivinationController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
private readonly ILogger<DivinationController> _logger;
|
||
|
|
|
||
|
|
public DivinationController(
|
||
|
|
ApplicationDbContext context,
|
||
|
|
ILogger<DivinationController> logger)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取价格配置
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("prices")]
|
||
|
|
public async Task<ActionResult<List<PriceConfig>>> GetPrices()
|
||
|
|
{
|
||
|
|
var prices = await _context.PriceConfigs
|
||
|
|
.Where(p => p.IsEnabled)
|
||
|
|
.ToListAsync();
|
||
|
|
return Ok(prices);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 提交卜卦请求
|
||
|
|
/// </summary>
|
||
|
|
[HttpPost("submit")]
|
||
|
|
public async Task<ActionResult<DivinationRecord>> Submit([FromBody] SubmitRequest request)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var record = new DivinationRecord
|
||
|
|
{
|
||
|
|
Type = request.Type,
|
||
|
|
InputData = request.InputData,
|
||
|
|
PaymentStatus = "pending",
|
||
|
|
PaymentMethod = request.PaymentMethod,
|
||
|
|
Amount = request.Amount,
|
||
|
|
ClientIp = HttpContext.Connection.RemoteIpAddress?.ToString(),
|
||
|
|
Language = request.Language ?? "zh-CN"
|
||
|
|
};
|
||
|
|
|
||
|
|
_context.DivinationRecords.Add(record);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
|
||
|
|
return Ok(record);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "Error submitting divination request");
|
||
|
|
return StatusCode(500, new { message = "提交失败" });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取卜卦结果
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("{id}")]
|
||
|
|
public async Task<ActionResult<DivinationRecord>> GetResult(long id)
|
||
|
|
{
|
||
|
|
var record = await _context.DivinationRecords.FindAsync(id);
|
||
|
|
if (record == null)
|
||
|
|
{
|
||
|
|
return NotFound();
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(record);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public record SubmitRequest(
|
||
|
|
string Type,
|
||
|
|
string InputData,
|
||
|
|
string? PaymentMethod,
|
||
|
|
decimal Amount,
|
||
|
|
string? Language
|
||
|
|
);
|