Skip to content

Commit

Permalink
Split Web to another class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShroomAgent27 committed Mar 9, 2024
1 parent 702d0fc commit 2464f21
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 74 deletions.
76 changes: 2 additions & 74 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,8 @@ public class Program
{
public static void Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

WebApplication app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

AzureHandler handler = new AzureHandler();

app.UseHttpsRedirection();

app.MapGet("/", () => {
return "CONNECTION SUCCESSFUL";
})
.WithName("TestRoot")
.WithOpenApi();


app.MapPut("/emails/add/", async context =>
{
Console.WriteLine("GOT REQUEST.");
if (context.Request.ContentType != "application/json")
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
string json = await ReadLines(context.Request.Body);
Email email;
try
{
email = JsonConvert.DeserializeObject<Email>(json);
}
catch (Exception e)
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
if (email is null || email.email is null)
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return;
}
handler.AddEmail(email);
context.Response.StatusCode = (int)HttpStatusCode.OK;
})
.WithName("RegisterEmail")
.WithOpenApi();

app.Run();
}

private static async Task<string> ReadLines(Stream stream)
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string result = "";
string line;
while (true)
{
line = await reader.ReadLineAsync();
if (line is null)
return result;
result += line;
}
}
new WebHandler(args);
}

}

121 changes: 121 additions & 0 deletions WebHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.Net;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;

public class WebHandler
{

private WebApplicationBuilder builder;
private WebApplication app;
private AzureHandler handler;
public WebHandler(string[] args)
{
buildWebApp(args);
}

private void buildWebApp(string[] args)
{
builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

handler = new AzureHandler();

app.UseHttpsRedirection();

app.MapGet("/", () =>
{
return "CONNECTION SUCCESSFUL";
})
.WithName("TestRoot")
.WithOpenApi();


app.MapPut("/emails/add/", async context =>
{
Email email = await GetEmailAndSetResponse(context.Request, context.Response);
if (email is null)
return;
handler.AddEmail(email);
context.Response.StatusCode = (int)HttpStatusCode.OK;
})
.WithName("RegisterEmail")
.WithOpenApi();

app.MapPut("/emails/remove/", async context =>
{
Email email = await GetEmailAndSetResponse(context.Request, context.Response);
if (email is null)
return;
handler.DeleteEmail(email);
context.Response.StatusCode = (int)HttpStatusCode.OK;

})
.WithName("DeleteEmail")
.WithOpenApi();

app.MapGet("/emails/get", () =>
{
return JsonConvert.SerializeObject(handler.GetEmails());
})
.WithName("GetEmails")
.WithOpenApi();

app.Run();
}

private async Task<Email> GetEmailAndSetResponse(HttpRequest request, HttpResponse response)
{
Email email;
if (request.ContentType != "application/json")
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
string json = await ReadLines(request.Body);
try
{
email = JsonConvert.DeserializeObject<Email>(json);
}
catch (Exception e)
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
if (email is null || email.email is null)
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
return email;
}

private async Task<string> ReadLines(Stream stream)
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string result = "";
string line;
while (true)
{
line = await reader.ReadLineAsync();
if (line is null)
return result;
result += line;
}
}
}
}

0 comments on commit 2464f21

Please sign in to comment.