-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterationModule.cs
More file actions
58 lines (52 loc) · 1.94 KB
/
Copy pathIterationModule.cs
File metadata and controls
58 lines (52 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
56
57
58
using Microsoft.Extensions.Options;
using TLECrawler.Api.Modules.Interfaces;
using TLECrawler.Application.DAL;
using TLECrawler.Application.Services;
using TLECrawler.Domain.Common.Configurations;
using TLECrawler.Domain.IterationModel;
using TLECrawler.Domain.UserModel;
using TLECrawler.Infrastructure.DAL;
using TLECrawler.Infrastructure.Services;
namespace TLECrawler.Api.Modules;
public class IterationModule : IModule
{
public IServiceCollection RegisterModule(IServiceCollection services)
{
services
.AddTransient<IIterationRepository, IterationRepository>()
.AddScoped<IIterationService, IterationService>();
return services;
}
public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/GetLastIteration", (IIterationRepository _iterations) =>
{
var iteration = _iterations.GetLast();
return Results.Ok(iteration);
});
endpoints.MapGet("/GetSchedule", (IOptions<SessionSettings> _options) =>
{
var periods = _options.Value.CheckHours
.Select(TimeOnly.Parse)
.ToList();
return Results.Ok(periods);
});
endpoints.MapPost("/MakeNewIteration", async (IIterationService _service, IIterationRepository _iterations) =>
{
var cts = new CancellationTokenSource();
try
{
var makeIterationTask = _service.StartIterationAsync(cts.Token);
await makeIterationTask;
var iteration = _iterations.GetLast();
return Results.Ok(new { iteration.Status!.ID, iteration.TLECount});
}
catch (Exception ex)
{
return Results.Problem(
$"The problem occured while executing the iteration. Reason: {ex.Message}");
}
});
return endpoints;
}
}