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 backgroundWorker; public BackgroundWorkController(BackgroundWorker backgroundWorker) { this.backgroundWorker = backgroundWorker; } [HttpGet] public IActionResult GetAll() { var result = new { CurrentWork = (BackgroundWorkDto?)backgroundWorker.CurrentWork, backgroundWorker.MainLoopLastException, RunOnceQueue = backgroundWorker.WorkStore.RunOnceQueue.Select(work => (BackgroundWorkDto)work), Periodics = backgroundWorker.WorkStore.Periodics.Select(work => (BackgroundWorkDto)work.Work), Done = backgroundWorker.WorkStore.Done.Select(work => (BackgroundWorkDto)work), Felled = backgroundWorker.WorkStore.Felled.Select(work => (BackgroundWorkDto)work), }; return Ok(result); } [HttpGet("current")] public IActionResult GetCurrent() { var work = backgroundWorker.CurrentWork; if (work == null) return NoContent(); return Ok(work); } [HttpGet("failed")] public IActionResult GetFelled() { var result = backgroundWorker.WorkStore.Felled.Select(work => (BackgroundWorkDto)work); return Ok(result); } [HttpGet("done")] public IActionResult GetDone() { var result = backgroundWorker.WorkStore.Done.Select(work => (BackgroundWorkDto)work); return Ok(result); } [HttpPost("restart"), Obsolete("temporary method")] public async Task RestartAsync(CancellationToken token) { await backgroundWorker.StopAsync(token); await backgroundWorker.StartAsync(token); return Ok(); } } }