using AsbCloudApp.Data; using AsbCloudInfrastructure.Background; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers; [Route("api/[controller]")] [Authorize] [ApiController] public class BackgroundWorkController : ControllerBase { private readonly BackgroundWorker worker; public BackgroundWorkController(BackgroundWorker worker) { this.worker = worker; } [HttpGet] public IActionResult GetAll() { var result = new { CurrentWork = (BackgroundWorkDto?)worker.CurrentWork, worker.MainLoopLastException, RunOnceQueue = worker.Works.Select(work => (BackgroundWorkDto)work), Done = worker.Done.Select(work => (BackgroundWorkDto)work), Felled = worker.Felled.Select(work => (BackgroundWorkDto)work), }; return Ok(result); } [HttpGet("current")] public IActionResult GetCurrent() { var work = worker.CurrentWork; if (work == null) return NoContent(); return Ok(work); } [HttpGet("failed")] public IActionResult GetFelled() { var result = worker.Felled.Select(work => (BackgroundWorkDto)work); return Ok(result); } [HttpGet("done")] public IActionResult GetDone() { var result = worker.Done.Select(work => (BackgroundWorkDto)work); return Ok(result); } [HttpPost("restart"), Obsolete("temporary method")] public async Task RestartAsync(CancellationToken token) { await worker.StopAsync(token); await worker.StartAsync(token); return Ok(); } }