- GeradoresService: cada tipo passa a ser um IGenerator (NIF, NIPC, NISS, CC, passaporte, IBAN, NIB, cartões, referência MB, pessoa, telefone, código postal, email, matrícula, EAN, UUID) registado em GeneratorRegistry; remove DAL/EF - GeradoresWS: minimal APIs em /api/v1 com OpenAPI + Scalar e health check - UnitTest: testes de ida e volta, valores conhecidos, detecção e API - geradoresfe: UI construída a partir dos metadados da API, validador livre, exportação CSV/JSON/TXT, nginx com API_UPSTREAM configurável - Docker, docker-compose e Jenkinsfile actualizados Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using System.Text.RegularExpressions;
|
|
using GeradoresService.Core;
|
|
using GeradoresService.Data;
|
|
|
|
namespace GeradoresService.Generators.Contactos;
|
|
|
|
/// <summary>Código postal português (NNNN-NNN). Apenas formato.</summary>
|
|
public sealed partial class CodigoPostalGenerator : GeneratorBase
|
|
{
|
|
public override string Id => "codigo-postal";
|
|
public override string Name => "Código postal";
|
|
public override string Category => Categories.Contactos;
|
|
public override string Description => "Código postal português (CP4-CP3) com localidade.";
|
|
public override bool HasChecksum => false;
|
|
|
|
public override GeneratedItem Generate(GeneratorContext context)
|
|
{
|
|
var (cp, locality, district) = Random(context);
|
|
return new GeneratedItem(cp, new Dictionary<string, string>
|
|
{
|
|
["Código postal"] = cp,
|
|
["Localidade"] = locality,
|
|
["Distrito"] = district,
|
|
});
|
|
}
|
|
|
|
public static (string Cp, string Locality, string District) Random(GeneratorContext context)
|
|
{
|
|
var l = context.Pick(PtData.Localities);
|
|
return ($"{l.Cp4}-{1 + context.Random.Next(999):D3}", l.Locality, l.District);
|
|
}
|
|
|
|
public override ValidationResult Validate(string value)
|
|
{
|
|
var m = Pattern().Match(value.Trim());
|
|
if (!m.Success)
|
|
return ValidationResult.WrongFormat("Formato esperado: NNNN-NNN (ex.: 1000-001).");
|
|
var cp = $"{m.Groups[1].Value}-{m.Groups[2].Value}";
|
|
return ValidationResult.Valid(cp, "Formato de código postal válido.");
|
|
}
|
|
|
|
[GeneratedRegex(@"^([1-9]\d{3})[-\s]?(\d{3})$")]
|
|
private static partial Regex Pattern();
|
|
}
|