取名小程序开发
This commit is contained in:
114
DouyinApi.Api/Controllers/MiniProgram/NamingController.cs
Normal file
114
DouyinApi.Api/Controllers/MiniProgram/NamingController.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using DouyinApi.Controllers;
|
||||
using DouyinApi.IServices;
|
||||
using DouyinApi.Model.Naming;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DouyinApi.Api.Controllers.MiniProgram
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/naming")]
|
||||
[AllowAnonymous]
|
||||
public class NamingController : BaseApiController
|
||||
{
|
||||
private readonly INamingService _namingService;
|
||||
private readonly ILogger<NamingController> _logger;
|
||||
|
||||
public NamingController(INamingService namingService, ILogger<NamingController> logger)
|
||||
{
|
||||
_namingService = namingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("validate-surname")]
|
||||
public async Task<IActionResult> ValidateSurname([FromBody] SurnameValidationRequest request)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Ok(new { isValid = false, message = "请输入合法姓氏" });
|
||||
}
|
||||
|
||||
var surname = request.Surname?.Trim() ?? string.Empty;
|
||||
var isValid = await _namingService.ValidateSurnameAsync(surname);
|
||||
return Ok(new
|
||||
{
|
||||
isValid,
|
||||
message = isValid ? string.Empty : "请输入合法姓氏"
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("generate")]
|
||||
public async Task<IActionResult> Generate([FromBody] NamingRequest request)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(new { message = "参数不合法" });
|
||||
}
|
||||
|
||||
var normalizedRequest = NormalizeRequest(request);
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _namingService.GenerateNamesAsync(normalizedRequest);
|
||||
return Ok(new { results = response.Results });
|
||||
}
|
||||
catch (ArgumentException ex) when (ex.Message == "invalid_surname")
|
||||
{
|
||||
return BadRequest(new { message = "INVALID_SURNAME" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Generate naming failed");
|
||||
return StatusCode(500, new { message = "GENERATION_FAILED" });
|
||||
}
|
||||
}
|
||||
|
||||
private static NamingRequest NormalizeRequest(NamingRequest request)
|
||||
{
|
||||
var normalized = new NamingRequest
|
||||
{
|
||||
Surname = request.Surname?.Trim() ?? string.Empty,
|
||||
Gender = string.IsNullOrWhiteSpace(request.Gender) ? "male" : request.Gender.Trim().ToLowerInvariant(),
|
||||
NameLength = string.IsNullOrWhiteSpace(request.NameLength) ? "double" : request.NameLength.Trim().ToLowerInvariant(),
|
||||
BirthDate = NormalizeDate(request.BirthDate),
|
||||
BirthTime = NormalizeTime(request.BirthTime)
|
||||
};
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private static string NormalizeDate(string date)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(date))
|
||||
{
|
||||
return DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
if (DateTime.TryParse(date, out var parsed))
|
||||
{
|
||||
return parsed.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
private static string NormalizeTime(string time)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(time))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (TimeSpan.TryParse(time, out var parsed))
|
||||
{
|
||||
return DateTime.Today.Add(parsed).ToString("HH:mm", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return time;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user