using API.APIEndpointRecords; using API.Workers; using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using static Microsoft.AspNetCore.Http.StatusCodes; // ReSharper disable InconsistentNaming namespace API.Controllers; [ApiVersion(2)] [ApiController] [Route("v{version:apiVersion}/[controller]")] public class WorkerController() : Controller { /// /// Returns all .Keys /// /// Keys/IDs [HttpGet] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetAllWorkers() { return Ok(Tranga.GetRunningWorkers().Select(w => w.Key).ToArray()); } /// /// Returns with requested /// /// Array of .Key /// [HttpPost("WithIDs")] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetWorkers([FromBody]string[] WorkerIds) { return Ok(Tranga.GetRunningWorkers().Where(worker => WorkerIds.Contains(worker.Key)).ToArray()); } /// /// Get all in requested /// /// Requested /// [HttpGet("State/{State}")] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetWorkersInState(WorkerExecutionState State) { return Ok(Tranga.GetRunningWorkers().Where(worker => worker.State == State).ToArray()); } /// /// Return with /// /// .Key /// /// with could not be found [HttpGet("{WorkerId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return NotFound(nameof(WorkerId)); return Ok(worker); } /// /// Delete with and all child-s /// /// .Key /// /// with could not be found [HttpDelete("{WorkerId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] public IActionResult DeleteWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return NotFound(nameof(WorkerId)); Tranga.StopWorker(worker); return Ok(); } /// /// Starts with /// /// .Key /// /// with could not be found /// was already running [HttpPost("{WorkerId}/Start")] [ProducesResponseType(Status202Accepted)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status412PreconditionFailed, "text/plain")] public IActionResult StartWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return NotFound(nameof(WorkerId)); if (worker.State >= WorkerExecutionState.Waiting) return StatusCode(Status412PreconditionFailed, "Already running"); Tranga.StartWorker(worker); return Ok(); } /// /// Stops with /// /// .Key /// /// with could not be found /// was not running [HttpPost("{WorkerId}/Stop")] [ProducesResponseType(Status501NotImplemented)] public IActionResult StopWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return NotFound(nameof(WorkerId)); if(worker.State is < WorkerExecutionState.Running or >= WorkerExecutionState.Completed) return StatusCode(Status208AlreadyReported, "Not running"); Tranga.StopWorker(worker); return Ok(); } }