| | 1 | | using LGDXRobotCloud.API.Authorisation; |
| | 2 | | using LGDXRobotCloud.API.Configurations; |
| | 3 | | using LGDXRobotCloud.API.Services.Automation; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 5 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 6 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Requests; |
| | 7 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 8 | | using LGDXRobotCloud.Utilities.Constants; |
| | 9 | | using LGDXRobotCloud.Utilities.Enums; |
| | 10 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 11 | | using Microsoft.AspNetCore.Authorization; |
| | 12 | | using Microsoft.AspNetCore.Mvc; |
| | 13 | | using Microsoft.Extensions.Options; |
| | 14 | | using System.Text.Json; |
| | 15 | |
|
| | 16 | | namespace LGDXRobotCloud.API.Areas.Automation.Controllers; |
| | 17 | |
|
| | 18 | | [ApiController] |
| | 19 | | [Area("Automation")] |
| | 20 | | [Route("[area]/[controller]")] |
| | 21 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificationScheme)] |
| | 22 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 23 | | [ValidateLgdxUserAccess] |
| 0 | 24 | | public sealed class AutoTasksController( |
| 0 | 25 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration, |
| 0 | 26 | | IAutoTaskService autoTaskService |
| 0 | 27 | | ) : ControllerBase |
| | 28 | | { |
| 0 | 29 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value ?? throw |
| 0 | 30 | | private readonly IAutoTaskService _autoTaskService = autoTaskService ?? throw new ArgumentNullException(nameof(autoTas |
| | 31 | |
|
| | 32 | | [HttpGet("")] |
| | 33 | | [ProducesResponseType(typeof(IEnumerable<AutoTaskListDto>), StatusCodes.Status200OK)] |
| | 34 | | public async Task<ActionResult<IEnumerable<AutoTaskListDto>>> GetTasks(int? realmId, string? name, AutoTaskCatrgory? a |
| 0 | 35 | | { |
| 0 | 36 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 37 | | var (tasks, PaginationHelper) = await _autoTaskService.GetAutoTasksAsync(realmId, name, autoTaskCatrgory, pageNumber |
| 0 | 38 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 39 | | return Ok(tasks.ToDto()); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | [HttpGet("{id}", Name = "GetTask")] |
| | 43 | | [ProducesResponseType(typeof(AutoTaskDto), StatusCodes.Status200OK)] |
| | 44 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 45 | | public async Task<ActionResult<AutoTaskDto>> GetTask(int id) |
| 0 | 46 | | { |
| 0 | 47 | | var autoTask = await _autoTaskService.GetAutoTaskAsync(id); |
| 0 | 48 | | return Ok(autoTask.ToDto()); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | [HttpPost("")] |
| | 52 | | [ProducesResponseType(typeof(AutoTaskDto), StatusCodes.Status201Created)] |
| | 53 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 54 | | public async Task<ActionResult> CreateTask(AutoTaskCreateDto autoTaskCreateDto) |
| 0 | 55 | | { |
| 0 | 56 | | var autoTask = await _autoTaskService.CreateAutoTaskAsync(autoTaskCreateDto.ToBusinessModel()); |
| 0 | 57 | | return CreatedAtAction(nameof(GetTask), new { id = autoTask.Id }, autoTask.ToDto()); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | [HttpPut("{id}")] |
| | 61 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 62 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 63 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 64 | | public async Task<ActionResult> UpdateTask(int id, AutoTaskUpdateDto autoTaskUpdateDto) |
| 0 | 65 | | { |
| 0 | 66 | | await _autoTaskService.UpdateAutoTaskAsync(id, autoTaskUpdateDto.ToBusinessModel()); |
| 0 | 67 | | return NoContent(); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | [HttpDelete("{id}")] |
| | 71 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 72 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 73 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 74 | | public async Task<ActionResult> DeleteTask(int id) |
| 0 | 75 | | { |
| 0 | 76 | | await _autoTaskService.DeleteAutoTaskAsync(id); |
| 0 | 77 | | return NoContent(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | [HttpPost("{id}/Abort")] |
| | 81 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 82 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 83 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 84 | | public async Task<ActionResult> AbortTask(int id) |
| 0 | 85 | | { |
| 0 | 86 | | await _autoTaskService.AbortAutoTaskAsync(id); |
| 0 | 87 | | return NoContent(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | [HttpGet("Statistics/{realmId}")] |
| | 91 | | [ProducesResponseType(typeof(AutoTaskStatisticsDto), StatusCodes.Status200OK)] |
| | 92 | | public async Task<ActionResult<AutoTaskStatisticsDto>> GetStatistics(int realmId) |
| 0 | 93 | | { |
| 0 | 94 | | var statistics = await _autoTaskService.GetAutoTaskStatisticsAsync(realmId); |
| 0 | 95 | | return Ok(statistics.ToDto()); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | [HttpGet("RobotCurrentTask/{robotId}")] |
| | 99 | | [ProducesResponseType(typeof(AutoTaskListDto), StatusCodes.Status200OK)] |
| | 100 | | public async Task<ActionResult<AutoTaskListDto?>> GetRobotCurrentTask(Guid robotId) |
| 0 | 101 | | { |
| 0 | 102 | | var autoTask = await _autoTaskService.GetRobotCurrentTaskAsync(robotId); |
| 0 | 103 | | return Ok(autoTask?.ToDto()); |
| 0 | 104 | | } |
| | 105 | | } |