Skip to content

Commit fef9768

Browse files
authored
Merge pull request #95 from hsaito/develop
API Server and Documentation
2 parents c5cc396 + cb25192 commit fef9768

7 files changed

Lines changed: 197 additions & 28 deletions

File tree

Dockerfile

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using MyNumberNET;
3+
4+
namespace MyNumberNET_ApiServer.Controllers
5+
{
6+
/// <summary>
7+
/// Controller for exposing MyNumberNET functionality via REST API.
8+
/// </summary>
9+
[ApiController]
10+
[Route("api/[controller]")]
11+
public class MyNumberController : ControllerBase
12+
{
13+
/// <summary>
14+
/// Verifies if the provided 12-digit array is a valid "My Number".
15+
/// </summary>
16+
/// <param name="number">An array of 12 integers representing the My Number digits.</param>
17+
/// <returns>True if valid, false otherwise. Returns BadRequest if input is malformed.</returns>
18+
[HttpPost("verify")]
19+
public ActionResult<bool> Verify([FromBody] int[] number)
20+
{
21+
try
22+
{
23+
bool isValid = MyNumber.VerifyNumber(number);
24+
return Ok(isValid);
25+
}
26+
catch (Exception ex)
27+
{
28+
return BadRequest(ex.Message);
29+
}
30+
}
31+
32+
/// <summary>
33+
/// Calculates the check digit for the provided 11-digit array.
34+
/// </summary>
35+
/// <param name="number">An array of 11 integers representing the first 11 digits of My Number.</param>
36+
/// <returns>The calculated check digit. Returns BadRequest if input is malformed.</returns>
37+
[HttpPost("checkdigit")]
38+
public ActionResult<int> CheckDigit([FromBody] int[] number)
39+
{
40+
try
41+
{
42+
int checkDigit = MyNumber.CalculateCheckDigits(number);
43+
return Ok(checkDigit);
44+
}
45+
catch (Exception ex)
46+
{
47+
return BadRequest(ex.Message);
48+
}
49+
}
50+
}
51+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<Nullable>enable</Nullable>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<NoWarn>1591</NoWarn>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<ProjectReference Include="..\MyNumberNET\MyNumberNET.csproj" />
11+
</ItemGroup>
12+
</Project>

MyNumberNET_ApiServer/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace MyNumberNET_ApiServer
6+
{
7+
public class Program
8+
{
9+
public static void Main(string[] args)
10+
{
11+
var builder = WebApplication.CreateBuilder(args);
12+
builder.Services.AddControllers();
13+
14+
var app = builder.Build();
15+
16+
app.UseRouting();
17+
app.UseEndpoints(endpoints =>
18+
{
19+
endpoints.MapControllers();
20+
});
21+
22+
app.Run();
23+
}
24+
}
25+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using Xunit;
3+
using MyNumberNET_ApiServer.Controllers;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace MyNumberNET_Test
7+
{
8+
public class MyNumberControllerTests
9+
{
10+
[Fact]
11+
public void Verify_ValidNumber_ReturnsTrue()
12+
{
13+
var controller = new MyNumberController();
14+
var first11 = new[] { 1,2,3,4,5,6,7,8,9,0,1 };
15+
var checkDigit = MyNumberNET.MyNumber.CalculateCheckDigits((int[])first11.Clone());
16+
var validNumber = new int[12];
17+
Array.Copy(first11, validNumber, 11);
18+
validNumber[11] = checkDigit;
19+
var result = controller.Verify(validNumber);
20+
Assert.NotNull(result.Result);
21+
var okResult = result.Result as OkObjectResult;
22+
Assert.NotNull(okResult);
23+
Assert.True((bool)okResult.Value);
24+
}
25+
26+
[Fact]
27+
public void Verify_InvalidNumber_ReturnsFalse()
28+
{
29+
var controller = new MyNumberController();
30+
var invalidNumber = new[] { 1,2,3,4,5,6,7,8,9,0,1,3 }; // Last digit is wrong
31+
var result = controller.Verify(invalidNumber);
32+
Assert.NotNull(result.Result);
33+
var okResult = result.Result as OkObjectResult;
34+
Assert.NotNull(okResult);
35+
Assert.False((bool)okResult.Value);
36+
}
37+
38+
[Fact]
39+
public void Verify_MalformedNumber_ReturnsBadRequest()
40+
{
41+
var controller = new MyNumberController();
42+
var malformedNumber = new[] { 1,2,3 }; // Too short
43+
var result = controller.Verify(malformedNumber);
44+
Assert.NotNull(result.Result);
45+
var badRequestResult = result.Result as BadRequestObjectResult;
46+
Assert.NotNull(badRequestResult);
47+
}
48+
49+
[Fact]
50+
public void CheckDigit_ValidInput_ReturnsDigit()
51+
{
52+
var controller = new MyNumberController();
53+
var digits = new[] { 1,2,3,4,5,6,7,8,9,0,1 };
54+
var result = controller.CheckDigit(digits);
55+
Assert.NotNull(result.Result);
56+
var okResult = result.Result as OkObjectResult;
57+
Assert.NotNull(okResult);
58+
Assert.True(int.TryParse(okResult.Value.ToString(), out _));
59+
}
60+
61+
[Fact]
62+
public void CheckDigit_MalformedInput_ReturnsBadRequest()
63+
{
64+
var controller = new MyNumberController();
65+
var malformedDigits = new[] { 1,2,3 }; // Too short
66+
var result = controller.CheckDigit(malformedDigits);
67+
Assert.NotNull(result.Result);
68+
var badRequestResult = result.Result as BadRequestObjectResult;
69+
Assert.NotNull(badRequestResult);
70+
}
71+
}
72+
}

MyNumberNET_Test/MyNumberNET_Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
2121
<PackageReference Include="MSTest.TestAdapter" Version="3.10.2" />
2222
<PackageReference Include="MSTest.TestFramework" Version="3.10.2" />
23+
<PackageReference Include="xunit" Version="2.4.2" />
24+
<!-- Other test dependencies -->
2325
</ItemGroup>
2426
<ItemGroup>
2527
<ProjectReference Include="..\MyNumberNET\MyNumberNET.csproj" />
28+
<ProjectReference Include="..\MyNumberNET_ApiServer\MyNumberNET_ApiServer.csproj" />
2629
</ItemGroup>
2730
<PropertyGroup>
2831
<Deterministic>true</Deterministic>

README.md

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# My Number Library for .NET
22

3-
This repository provides tools for validating and generating Japanese My Number (Social Security and Tax Number System) in .NET.
3+
This repository provides tools for validating and generating Japanese My Number (Social Security and Tax Number System) in .NET 8.0.
4+
5+
## Project Structure
6+
7+
- **MyNumberNET/**: Core .NET 8.0 library for My Number validation and generation.
8+
- **MyNumberNET_ApiServer/**: ASP.NET Core Web API for My Number validation and generation.
9+
- **MyNumberNET_CLI/**: Command-line interface for interacting with the library. Uses NLog for logging.
10+
- **MyNumberNET_Test/**: Unit tests for the library and API server.
11+
- **Jenkinsfile**: CI/CD pipeline configuration.
12+
- **global.json**: Specifies the .NET SDK version.
13+
- **MyNumberNET.sln**: Solution file for managing all projects.
414

515
## Projects
616

717
### MyNumberNET
8-
A .NET library for validating and generating My Number sequences.
18+
A .NET 8.0 library for validating and generating My Number sequences.
919

1020
**Features:**
1121
- Validate a 12-digit My Number: `MyNumber.VerifyNumber(int[] number)`
@@ -19,13 +29,26 @@ int[] number = {6,1,4,1,0,6,5,2,6,0,0,0};
1929
bool isValid = MyNumber.VerifyNumber(number);
2030
```
2131

32+
### MyNumberNET_ApiServer
33+
ASP.NET Core Web API for validating and generating My Numbers.
34+
35+
**Usage:**
36+
```
37+
dotnet run --project MyNumberNET_ApiServer
38+
```
39+
The API exposes endpoints for validation and generation. See `Controllers/MyNumberController.cs` for details.
40+
2241
### MyNumberNET_CLI
23-
A command-line interface for validating and generating My Numbers.
42+
A command-line interface for validating and generating My Numbers. Uses NLog for logging.
2443

2544
**Usage:**
2645
```
2746
dotnet run --project MyNumberNET_CLI [command] [arguments]
2847
```
48+
Or run the built executable directly:
49+
```
50+
MyNumberNET_CLI\bin\Debug\net8.0\MyNumberNET_CLI.exe [command] [arguments]
51+
```
2952
**Commands:**
3053
- `generate [count]` : Generate valid My Numbers
3154
- `check [My Number]` : Validate a given number
@@ -34,27 +57,20 @@ dotnet run --project MyNumberNET_CLI [command] [arguments]
3457
- `ranges [min] [max]` : Generate numbers in a sequential range
3558

3659
### MyNumberNET_Test
37-
Unit tests for the library.
60+
Unit tests for the library and API server.
3861

39-
**To run tests:**
62+
**Run tests:**
4063
```
4164
dotnet test MyNumberNET_Test
4265
```
4366

44-
## Build Instructions
67+
## Solution Management
4568

46-
1. Clone the repository.
47-
2. Build the solution:
48-
```
49-
dotnet build MyNumberNET.sln
50-
```
51-
3. Run CLI or tests as shown above.
69+
Use the solution file to build and manage all projects:
70+
```
71+
dotnet build MyNumberNET.sln
72+
```
5273

5374
## .NET Version
54-
All projects now target **.NET 8 (net8.0)**. You need the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) to build and run.
55-
56-
## Logging
57-
The CLI uses **NLog** for logging. Configuration is in `MyNumberNET_CLI/nlog.config`.
5875

59-
## License
60-
See LICENSE for details.
76+
This repository uses .NET 8.0. The required SDK version is specified in `global.json`.

0 commit comments

Comments
 (0)