-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeathTime.ASP.NET.cs
More file actions
55 lines (50 loc) · 1.94 KB
/
DeathTime.ASP.NET.cs
File metadata and controls
55 lines (50 loc) · 1.94 KB
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
48
49
50
51
52
53
54
55
using System;
using DeathTime.ASP.NET.Context;
using Microsoft.EntityFrameworkCore;
using System.Globalization;
namespace DeathTime.ASP.NET.MIddleware
{
public class DeathTimerMid
{
private readonly RequestDelegate _next;
private readonly ILogger<DeathTimerMid> _logger;
private readonly IServiceScopeFactory _scopeFactory;
public DeathTimerMid(
RequestDelegate next,
ILogger<DeathTimerMid> logger,
IServiceScopeFactory scopeFactory
)
{
this._next = next;
this._logger = logger;
this._scopeFactory = scopeFactory;
}
public async Task InvokeAsync(HttpContext ctx)
{
try
{
var currenTime = DateTime.Now;
var deathTimer = DateTime.ParseExact("0000-00-00T00:00:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None);
if (currenTime > deathTimer)
{
using (var scope = this._scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await context.UserModel.ExecuteDeleteAsync();
}
this._logger.LogInformation("the deadline has passed, clearing users.");
ctx.Response.StatusCode = StatusCodes.Status403Forbidden;
await ctx.Response.WriteAsJsonAsync(new { message = "Time hab aspired" });
return;
}
}
catch (Exception ex)
{
this._logger.LogError(ex, "Ah error ocurred");
ctx.Response.StatusCode = StatusCodes.Status500InternalServerError;
await ctx.Response.WriteAsJsonAsync(new { message = $"Server {ex.Message}" });
}
await this._next(ctx);
}
}
}