Add an API
This commit is contained in:
		| @@ -1,7 +1,9 @@ | |||||||
| using System.Globalization; | using System.Globalization; | ||||||
|  | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace GrpcCalculator.Server; | namespace GrpcCalculator.Server; | ||||||
|  |  | ||||||
|  | [JsonConverter(typeof(JsonStringEnumConverter))] | ||||||
| public enum CalculatorState | public enum CalculatorState | ||||||
| { | { | ||||||
|     AfterEquals, |     AfterEquals, | ||||||
| @@ -9,7 +11,7 @@ public enum CalculatorState | |||||||
|     AfterDigit, |     AfterDigit, | ||||||
| } | } | ||||||
|  |  | ||||||
|  | [JsonConverter(typeof(JsonStringEnumConverter))] | ||||||
| public enum CalculatorOperator | public enum CalculatorOperator | ||||||
| { | { | ||||||
|     Addition, |     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 | using GrpcCalculator.Server; | ||||||
| Console.WriteLine("Hello, World!"); |  | ||||||
|  | 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> |   <PropertyGroup> | ||||||
|     <OutputType>Exe</OutputType> |  | ||||||
|     <TargetFramework>net8.0</TargetFramework> |     <TargetFramework>net8.0</TargetFramework> | ||||||
|     <ImplicitUsings>enable</ImplicitUsings> |     <ImplicitUsings>enable</ImplicitUsings> | ||||||
|     <Nullable>enable</Nullable> |     <Nullable>enable</Nullable> | ||||||
|  |     <InvariantGlobalization>true</InvariantGlobalization> | ||||||
|     <RootNamespace>GrpcCalculator.Server</RootNamespace> |     <RootNamespace>GrpcCalculator.Server</RootNamespace> | ||||||
|     <AssemblyName>GrpcCalculator.Server</AssemblyName> |     <AssemblyName>GrpcCalculator.Server</AssemblyName> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|  |   <ItemGroup> | ||||||
|  |     <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" /> | ||||||
|  |     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||||||
|  |   </ItemGroup> | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user