From be60a4df8ede56f8cb8e2a78b7523d826456b4e3 Mon Sep 17 00:00:00 2001 From: "SECUNDIS\\masantos" Date: Wed, 10 Apr 2024 19:42:13 +0100 Subject: [PATCH] =?UTF-8?q?Gera=C3=A7=C3=B5es=20NIF=20NISS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 25 ++++++ Geradores.sln | 12 +++ GeradoresService/GeradoresService.csproj | 9 ++ GeradoresService/NIF.cs | 89 ++++++++++++++++++ GeradoresService/NISS.cs | 90 +++++++++++++++++++ GeradoresWS/Controllers/GenerateController.cs | 38 ++++++++ .../Controllers/WeatherForecastController.cs | 33 +++++++ GeradoresWS/Dockerfile | 22 +++++ GeradoresWS/GeradoresWS.csproj | 21 +++++ GeradoresWS/Program.cs | 25 ++++++ GeradoresWS/Properties/launchSettings.json | 48 ++++++++++ GeradoresWS/WeatherForecast.cs | 13 +++ GeradoresWS/appsettings.Development.json | 8 ++ GeradoresWS/appsettings.json | 9 ++ 14 files changed, 442 insertions(+) create mode 100644 .dockerignore create mode 100644 GeradoresService/GeradoresService.csproj create mode 100644 GeradoresService/NIF.cs create mode 100644 GeradoresService/NISS.cs create mode 100644 GeradoresWS/Controllers/GenerateController.cs create mode 100644 GeradoresWS/Controllers/WeatherForecastController.cs create mode 100644 GeradoresWS/Dockerfile create mode 100644 GeradoresWS/GeradoresWS.csproj create mode 100644 GeradoresWS/Program.cs create mode 100644 GeradoresWS/Properties/launchSettings.json create mode 100644 GeradoresWS/WeatherForecast.cs create mode 100644 GeradoresWS/appsettings.Development.json create mode 100644 GeradoresWS/appsettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Geradores.sln b/Geradores.sln index 8b77bcc..b4ed6f4 100644 --- a/Geradores.sln +++ b/Geradores.sln @@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFi EndProject Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "geradoresfe", "geradoresfe\geradoresfe.esproj", "{1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeradoresWS", "GeradoresWS\GeradoresWS.csproj", "{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeradoresService", "GeradoresService\GeradoresService.csproj", "{35C4D726-3771-4D34-9A43-541588FCECAA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,6 +28,14 @@ Global {1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.ActiveCfg = Release|Any CPU {1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.Build.0 = Release|Any CPU {1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.Deploy.0 = Release|Any CPU + {78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Release|Any CPU.Build.0 = Release|Any CPU + {35C4D726-3771-4D34-9A43-541588FCECAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35C4D726-3771-4D34-9A43-541588FCECAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35C4D726-3771-4D34-9A43-541588FCECAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35C4D726-3771-4D34-9A43-541588FCECAA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GeradoresService/GeradoresService.csproj b/GeradoresService/GeradoresService.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/GeradoresService/GeradoresService.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/GeradoresService/NIF.cs b/GeradoresService/NIF.cs new file mode 100644 index 0000000..aa10072 --- /dev/null +++ b/GeradoresService/NIF.cs @@ -0,0 +1,89 @@ +using System.Net.NetworkInformation; + +namespace GeradoresService +{ + public class NIF + { + public static string Generate(string? type) + { + return GenerateRandomNIF(type); + } + + public static string GenerateRandomNIF(string? nifType) + { + var firstDigitValidate = new char[] { '1', '2','3', '5', '6', '8', '9' }; + Random rnd = new Random(); + char firstDigit; + + if (string.IsNullOrEmpty(nifType)) + { + // Gera o primeiro dígito aleatório dentro dos válidos + int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5 + firstDigit = firstDigitValidate[firstDigitIndex]; + } else { + if (firstDigitValidate.Contains(nifType[0])){ + firstDigit = nifType[0]; + }else + { + int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5 + firstDigit = firstDigitValidate[firstDigitIndex]; + } + } + + // Gera os próximos 7 dígitos aleatórios + string nextDigits = ""; + for (int i = 0; i < 7; i++) + { + nextDigits += rnd.Next(0, 10); // Gera um dígito aleatório de 0 a 9 + } + + // Calcula o dígito de controlo + int checkDigit = (firstDigit - '0') * 9; + for (int i = 2; i <= 8; i++) + { + checkDigit += (nextDigits[i - 2] - '0') * (10 - i); + } + checkDigit = 11 - (checkDigit % 11); + if (checkDigit >= 10) + checkDigit = 0; + + // Concatena os dígitos gerados e o dígito de controlo + string randomNIF = firstDigit + nextDigits + checkDigit; + + return randomNIF; + } + + + public static bool Validate(string nif) + { + // Verificar se o NIF tem 9 dígitos + if (nif.Length != 9) + { + return false; + } + + // Converter o NIF para um array de dígitos + int[] digitos = new int[9]; + for (int i = 0; i < 9; i++) + { + if (!int.TryParse(nif[i].ToString(), out digitos[i])) + { + return false; // Se algum caractere não for um dígito, o NIF é inválido + } + } + + // Calcular o dígito de controlo + int soma = 0; + for (int i = 0; i < 8; i++) + { + soma += digitos[i] * (9 - i); + } + int resto = soma % 11; + int digitoControlo = resto <= 1 ? 0 : 11 - resto; + + // Verificar se o dígito de controlo coincide com o último dígito do NIF + return digitoControlo == digitos[8]; + } + + } +} \ No newline at end of file diff --git a/GeradoresService/NISS.cs b/GeradoresService/NISS.cs new file mode 100644 index 0000000..425f351 --- /dev/null +++ b/GeradoresService/NISS.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeradoresService +{ + public class NISS + { + private static readonly Random random = new Random(); + public static string Generate() { + return GenerateNISS(); + } + private static string GenerateRandomDigits(int length) + { + string digits = "1"; + for (int i = 0; i < length; i++) + { + digits += random.Next(0, 10); // Gera um dígito aleatório entre 0 e 9 + } + return digits; + } + public static string GenerateNISS() + { + // Gera 9 dígitos aleatórios + string niss = GenerateRandomDigits(9); + + // Calcula o dígito de controlo + int soma = 0; + for (int i = 0; i < 10; i++) + { + int digito = int.Parse(niss[i].ToString()); + soma += digito * (i + 1); + } + int resto = soma % 11; + int digitoControlo = 11 - resto; + + // Verifica se o dígito de controlo está no intervalo [0, 9] + if (digitoControlo == 10) + { + niss += "0"; + } + else if (digitoControlo == 11) + { + niss += "1"; + } + else + { + niss += digitoControlo.ToString(); + } + + return niss; + } + public static bool Validate(string niss) + { + // Verifica se o NISS tem 11 dígitos + if (niss.Length != 11) + return false; + + // Verifica se é um número + foreach (char c in niss) + { + if (!char.IsDigit(c)) + return false; + } + + // Calcula o dígito de controlo + int soma = 0; + for (int i = 0; i < 10; i++) + { + int digito = int.Parse(niss[i].ToString()); + soma += digito * (i + 1); + } + int resto = soma % 11; + int digitoControlo = 11 - resto; + + // Verifica o último dígito do NISS + int ultimoDigito = int.Parse(niss[10].ToString()); + if (digitoControlo == 10 && ultimoDigito != 0) + return false; + if (digitoControlo == 11 && ultimoDigito != 0 && ultimoDigito != 1) + return false; + if (digitoControlo != ultimoDigito) + return false; + + return true; + } + } +} diff --git a/GeradoresWS/Controllers/GenerateController.cs b/GeradoresWS/Controllers/GenerateController.cs new file mode 100644 index 0000000..ead9f4c --- /dev/null +++ b/GeradoresWS/Controllers/GenerateController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; +using GeradoresService; + +namespace GeradoresWS.Controllers +{ + [ApiController] + [Route("[controller]")] + public class GenerateController : Controller + { + #region NIF + [HttpGet("GenerateNIF")] + public string GenerateNIF(string? type) + { + return NIF.Generate(type); + } + + [HttpGet("ValidateNIF")] + public bool ValidateNIF(string nif) + { + return NIF.Validate(nif); + } + #endregion + + #region NISS + [HttpGet("GenerateNISS")] + public string GenerateNISS() + { + return NISS.Generate(); + } + + [HttpGet("ValidateNISS")] + public bool ValidateNISS(string nif) + { + return NISS.Validate(nif); + } + #endregion + } +} diff --git a/GeradoresWS/Controllers/WeatherForecastController.cs b/GeradoresWS/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..7420f29 --- /dev/null +++ b/GeradoresWS/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace GeradoresWS.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/GeradoresWS/Dockerfile b/GeradoresWS/Dockerfile new file mode 100644 index 0000000..09268d4 --- /dev/null +++ b/GeradoresWS/Dockerfile @@ -0,0 +1,22 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base +WORKDIR /app +EXPOSE 80 +EXPOSE 443 + +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +WORKDIR /src +COPY ["GeradoresWS/GeradoresWS.csproj", "GeradoresWS/"] +RUN dotnet restore "GeradoresWS/GeradoresWS.csproj" +COPY . . +WORKDIR "/src/GeradoresWS" +RUN dotnet build "GeradoresWS.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "GeradoresWS.csproj" -c Release -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "GeradoresWS.dll"] \ No newline at end of file diff --git a/GeradoresWS/GeradoresWS.csproj b/GeradoresWS/GeradoresWS.csproj new file mode 100644 index 0000000..f85e9e7 --- /dev/null +++ b/GeradoresWS/GeradoresWS.csproj @@ -0,0 +1,21 @@ + + + + net7.0 + enable + enable + df357c84-88b1-4100-86d1-889166b333f7 + Linux + + + + + + + + + + + + + diff --git a/GeradoresWS/Program.cs b/GeradoresWS/Program.cs new file mode 100644 index 0000000..48863a6 --- /dev/null +++ b/GeradoresWS/Program.cs @@ -0,0 +1,25 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/GeradoresWS/Properties/launchSettings.json b/GeradoresWS/Properties/launchSettings.json new file mode 100644 index 0000000..d4edd08 --- /dev/null +++ b/GeradoresWS/Properties/launchSettings.json @@ -0,0 +1,48 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5053" + }, + "https": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7266;http://localhost:5053" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "publishAllPorts": true, + "useSSL": false + } + }, + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:29191", + "sslPort": 44329 + } + } +} \ No newline at end of file diff --git a/GeradoresWS/WeatherForecast.cs b/GeradoresWS/WeatherForecast.cs new file mode 100644 index 0000000..8907579 --- /dev/null +++ b/GeradoresWS/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace GeradoresWS +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} \ No newline at end of file diff --git a/GeradoresWS/appsettings.Development.json b/GeradoresWS/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/GeradoresWS/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/GeradoresWS/appsettings.json b/GeradoresWS/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/GeradoresWS/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}