- aggiunta API chiamate web
This commit is contained in:
Samuele Locatelli
2024-04-03 12:43:08 +02:00
parent 9f06775afb
commit bd61e76529
3 changed files with 112 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MP.Data.DatabaseModels;
using MP.Stats.Data;
using NLog;
using NLog.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TaskController : ControllerBase
{
public TaskController(IConfiguration configuration, MpStatsService DataService)
{
Log.Trace("Starting TaskController");
_configuration = configuration;
DService = DataService;
Log.Trace("Avviato TaskController");
}
/// <summary>
/// List of available task and last execution data
/// </summary>
/// <returns></returns>
[HttpGet("GetList")]
public async Task<List<TaskListModel>> GetList()
{
List<TaskListModel> answ = await DService.TaskListAll(MP.Data.Objects.Enums.Task2ExeType.ND, "");
return answ;
}
/// <summary>
/// Call for execution of due task and report state
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<List<TaskResultModel>> ExecuteAndReturnGet()
{
List<TaskResultModel> answ = new List<TaskResultModel>();
List<TaskListModel> listTask = await DService.TaskListAll(MP.Data.Objects.Enums.Task2ExeType.ND, "");
// verifico SE ci siano task in scadenza...
DateTime adesso = DateTime.Now;
var task2exe = listTask.Where(x => x.DtNextExec <= adesso).ToList();
foreach (var taskRec in task2exe)
{
TaskResultModel result = await DService.ExecuteTask(taskRec.TaskId);
answ.Add(result);
}
// resituisco
return answ;
}
private static IConfiguration _configuration = null!;
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Dataservice x accesso DB
/// </summary>
protected MpStatsService DService { get; set; }
}
}
+3
View File
@@ -6,6 +6,8 @@
<UserSecretsId>826e877c-ba70-4253-84cb-d0b1cafd4440</UserSecretsId>
<Version>6.16.2404.0312</Version>
<Version>6.16.2404.0312</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
@@ -192,6 +194,7 @@
<PackageReference Include="ElmahCore.Sql" Version="2.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
+41
View File
@@ -5,9 +5,13 @@ using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using MP.Stats.Data;
using StackExchange.Redis;
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
namespace MP.Stats
{
@@ -65,8 +69,17 @@ namespace MP.Stats
app.UseRouting();
// aggiunta swagger, alla pagina /swagger/index.html
if (env.IsDevelopment() || env.IsStaging())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Configure the HTTP request pipeline.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
@@ -76,6 +89,34 @@ namespace MP.Stats
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "MP-STATS API",
Description = "ASP.NET Core Web API for managing MP-STATS items",
TermsOfService = new Uri("https://www.egalware.com/mp-stats-api-terms"),
//Contact = new OpenApiContact
//{
// Name = "Example Contact",
// Url = new Uri("https://example.com/contact")
//},
//License = new OpenApiLicense
//{
// Name = "Example License",
// Url = new Uri("https://example.com/license")
//}
});
// using System.Reflection;
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
// Elmah
services.AddElmah();
//string elmaConn = "Data Source=SQL2016DEV;Initial Catalog=Elmah;User ID=sa;Password=keyhammer16;integrated security=False;MultipleActiveResultSets=True;App=SHERPA.BBM;";