Add an API
This commit is contained in:
parent
985f7728a5
commit
0c393870d9
@ -1,7 +1,9 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GrpcCalculator.Server;
|
||||
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum CalculatorState
|
||||
{
|
||||
AfterEquals,
|
||||
@ -9,7 +11,7 @@ public enum CalculatorState
|
||||
AfterDigit,
|
||||
}
|
||||
|
||||
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum CalculatorOperator
|
||||
{
|
||||
Addition,
|
||||
|
15
Server/CalculatorEndpoints.cs
Normal file
15
Server/CalculatorEndpoints.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace GrpcCalculator.Server;
|
||||
|
||||
public static class CalculatorEndpoints
|
||||
{
|
||||
public static RouteGroupBuilder MapCalculatorApi(this RouteGroupBuilder group)
|
||||
{
|
||||
group.MapPost("/operator/{op}", (
|
||||
CalculatorOperator op,
|
||||
Calculator calculator) => calculator.OperatorPressed(op));
|
||||
group.MapPost("/equals", (Calculator calculator) => calculator.EqualsPressed());
|
||||
group.MapPost("/digit/{digit}", (string digit, Calculator calculator) => calculator.DigitPressed(digit));
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
@ -1,2 +1,22 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
using GrpcCalculator.Server;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapGroup("/")
|
||||
.MapCalculatorApi()
|
||||
.WithOpenApi();
|
||||
|
||||
app.Run();
|
||||
|
@ -1,12 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<RootNamespace>GrpcCalculator.Server</RootNamespace>
|
||||
<AssemblyName>GrpcCalculator.Server</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user