using API.Controllers.DTOs; using API.Workers; using Asp.Versioning; using Microsoft.AspNetCore.Http.HttpResults; 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 /// /// [HttpGet] [ProducesResponseType>(Status200OK, "application/json")] public Ok> GetWorkers() { IEnumerable result = Tranga.GetRunningWorkers().Select(w => new Worker(w.Key, w.AllDependencies.Select(d => d.Key), w.MissingDependencies.Select(d => d.Key), w.AllDependenciesFulfilled, w.State)); return TypedResults.Ok(result.ToList()); } /// /// Returns all .Keys /// /// [HttpGet("Keys")] [ProducesResponseType(Status200OK, "application/json")] public Ok> GetWorkerIds() { return TypedResults.Ok(Tranga.GetRunningWorkers().Select(w => w.Key).ToList()); } /// /// Get all in requested /// /// Requested /// [HttpGet("State/{State}")] [ProducesResponseType>(Status200OK, "application/json")] public Ok> GetWorkersInState(WorkerExecutionState State) { IEnumerable result = Tranga.GetRunningWorkers().Where(worker => worker.State == State).Select(w => new Worker(w.Key, w.AllDependencies.Select(d => d.Key), w.MissingDependencies.Select(d => d.Key), w.AllDependenciesFulfilled, w.State)); return TypedResults.Ok(result.ToList()); } /// /// Return with /// /// .Key /// /// with could not be found [HttpGet("{WorkerId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound, "text/plain")] public Results, NotFound> GetWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } w) return TypedResults.NotFound(nameof(WorkerId)); Worker result = new (w.Key, w.AllDependencies.Select(d => d.Key), w.MissingDependencies.Select(d => d.Key), w.AllDependenciesFulfilled, w.State); return TypedResults.Ok(result); } /// /// Delete with and all child-s /// /// .Key /// /// with could not be found [HttpDelete("{WorkerId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] public Results> DeleteWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return TypedResults.NotFound(nameof(WorkerId)); Tranga.StopWorker(worker); return TypedResults.Ok(); } /// /// Starts with /// /// .Key /// /// with could not be found /// was already running [HttpPost("{WorkerId}/Start")] [ProducesResponseType(Status202Accepted)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status412PreconditionFailed)] public Results, StatusCodeHttpResult> StartWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return TypedResults.NotFound(nameof(WorkerId)); if (worker.State >= WorkerExecutionState.Waiting) return TypedResults.StatusCode(Status412PreconditionFailed); Tranga.StartWorker(worker); return TypedResults.Ok(); } /// /// Stops with /// /// .Key /// /// with could not be found /// was already not running [HttpPost("{WorkerId}/Stop")] [ProducesResponseType(Status202Accepted)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status412PreconditionFailed)] public Results, StatusCodeHttpResult> StopWorker(string WorkerId) { if(Tranga.GetRunningWorkers().FirstOrDefault(w => w.Key == WorkerId) is not { } worker) return TypedResults.NotFound(nameof(WorkerId)); if(worker.State is < WorkerExecutionState.Running or >= WorkerExecutionState.Completed) return TypedResults.StatusCode(Status412PreconditionFailed); Tranga.StopWorker(worker); return TypedResults.Ok(); } }