79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using DouyinApi.Controllers;
|
|
using DouyinApi.IServices;
|
|
using DouyinApi.Model.DailyFortune;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace DouyinApi.Api.Controllers.MiniProgram;
|
|
|
|
[Produces("application/json")]
|
|
[Route("api/daily-fortune")]
|
|
[AllowAnonymous]
|
|
public class DailyFortuneController : BaseApiController
|
|
{
|
|
private readonly IDailyFortuneService _dailyFortuneService;
|
|
private readonly IContentSecurityService _contentSecurityService;
|
|
private readonly ILogger<DailyFortuneController> _logger;
|
|
|
|
public DailyFortuneController(
|
|
IDailyFortuneService dailyFortuneService,
|
|
IContentSecurityService contentSecurityService,
|
|
ILogger<DailyFortuneController> logger)
|
|
{
|
|
_dailyFortuneService = dailyFortuneService;
|
|
_contentSecurityService = contentSecurityService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost("analyze")]
|
|
public async Task<IActionResult> Analyze([FromBody] DailyFortuneRequest request)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(new { message = "invalid_request" });
|
|
}
|
|
|
|
var normalizedCity = request.BirthCity?.Trim() ?? string.Empty;
|
|
if (string.IsNullOrWhiteSpace(normalizedCity))
|
|
{
|
|
return BadRequest(new { message = "invalid_birth_city" });
|
|
}
|
|
|
|
var securityText = string.Join(" ", new[]
|
|
{
|
|
normalizedCity,
|
|
request.BirthProvince ?? string.Empty,
|
|
request.BirthDate ?? string.Empty,
|
|
request.BirthTime ?? string.Empty
|
|
}).Trim();
|
|
|
|
var security = await _contentSecurityService.CheckTextAsync(securityText);
|
|
if (!security.IsSafe)
|
|
{
|
|
return BadRequest(new { message = "CONTENT_RISK" });
|
|
}
|
|
|
|
try
|
|
{
|
|
var response = await _dailyFortuneService.AnalyzeAsync(request);
|
|
return Ok(response);
|
|
}
|
|
catch (ArgumentException ex) when (ex.Message == "invalid_birthdate")
|
|
{
|
|
return BadRequest(new { message = "BIRTHDATE_INVALID" });
|
|
}
|
|
catch (ArgumentException ex) when (ex.Message == "invalid_birth_city")
|
|
{
|
|
return BadRequest(new { message = "invalid_birth_city" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Daily fortune analyze failed");
|
|
return StatusCode(500, new { message = "GENERATION_FAILED" });
|
|
}
|
|
}
|
|
}
|