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