-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
47 lines (36 loc) · 1.71 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.AspNetCore.WebUtilities;
namespace Calculator
{
class Program
{
static void Main()
{
var builder = WebApplication.CreateBuilder();
var app = builder.Build();
app.MapGet("/", async (HttpContext ctx) =>
{
try {
var queryStrings = QueryHelpers.ParseQuery(ctx.Request.QueryString.Value);
IFactory Factory = new Factory();
IGetInput InputGetter = Factory.MakeInputGetter();
string operatorInput = InputGetter.Get(queryStrings, "operator");
string firstNumberInput = InputGetter.Get(queryStrings, "firstNumber");
string secondNumberInput = InputGetter.Get(queryStrings, "secondNumber");
int firstNumber = int.Parse(firstNumberInput);
int secondNumber = int.Parse(secondNumberInput);
IOperator Operator = Factory.MakeOperator(operatorInput);
int output = Operator.Operate(firstNumber, secondNumber);
ctx.Response.StatusCode = 200;
ctx.Response.Headers.ContentType = "text/plain";
string response = string.Format($"The result of calculation is {output}");
await ctx.Response.WriteAsync(response);
await ctx.Response.WriteAsync("\n");
} catch (Exception e) {
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsync(e.Message);
}
});
app.Run();
}
}
}