| | 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.Responses; |
| | 7 | | using LGDXRobotCloud.Utilities.Constants; |
| | 8 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 9 | | using Microsoft.AspNetCore.Authorization; |
| | 10 | | using Microsoft.AspNetCore.Mvc; |
| | 11 | | using Microsoft.Extensions.Options; |
| | 12 | | using System.Text.Json; |
| | 13 | |
|
| | 14 | | namespace LGDXRobotCloud.API.Areas.Automation.Controllers; |
| | 15 | |
|
| | 16 | | [ApiController] |
| | 17 | | [Area("Automation")] |
| | 18 | | [Route("[area]/[controller]")] |
| | 19 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificationScheme)] |
| | 20 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| | 21 | | [ValidateLgdxUserAccess] |
| 0 | 22 | | public sealed class FlowsController( |
| 0 | 23 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration, |
| 0 | 24 | | IFlowService flowService |
| 0 | 25 | | ) : ControllerBase |
| | 26 | | { |
| 0 | 27 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value; |
| 0 | 28 | | private readonly IFlowService _flowService = flowService; |
| | 29 | |
|
| | 30 | | [HttpGet("")] |
| | 31 | | [ProducesResponseType(typeof(IEnumerable<FlowListDto>), StatusCodes.Status200OK)] |
| | 32 | | public async Task<ActionResult<IEnumerable<FlowListDto>>> GetFlows(string? name, int pageNumber = 1, int pageSize = 10 |
| 0 | 33 | | { |
| 0 | 34 | | pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize : |
| 0 | 35 | | var (flows, PaginationHelper) = await _flowService.GetFlowsAsync(name, pageNumber, pageSize); |
| 0 | 36 | | Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); |
| 0 | 37 | | return Ok(flows.ToDto()); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | [HttpGet("Search")] |
| | 41 | | [ProducesResponseType(typeof(IEnumerable<FlowSearchDto>), StatusCodes.Status200OK)] |
| | 42 | | public async Task<ActionResult<IEnumerable<FlowSearchDto>>> SearchFlows(string? name) |
| 0 | 43 | | { |
| 0 | 44 | | var flows = await _flowService.SearchFlowsAsync(name); |
| 0 | 45 | | return Ok(flows.ToDto()); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | [HttpGet("{id}", Name = "GetFlow")] |
| | 49 | | [ProducesResponseType(typeof(FlowDto), StatusCodes.Status200OK)] |
| | 50 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 51 | | public async Task<ActionResult<FlowDto>> GetFlow(int id) |
| 0 | 52 | | { |
| 0 | 53 | | var flow = await _flowService.GetFlowAsync(id); |
| 0 | 54 | | return Ok(flow.ToDto()); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | [HttpPost("")] |
| | 58 | | [ProducesResponseType(typeof(FlowDto), StatusCodes.Status201Created)] |
| | 59 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 60 | | public async Task<ActionResult> CreateFlow(FlowCreateDto flowCreateDto) |
| 0 | 61 | | { |
| 0 | 62 | | var flow = await _flowService.CreateFlowAsync(flowCreateDto.ToBusinessModel()); |
| 0 | 63 | | return CreatedAtAction(nameof(GetFlow), new { id = flow.Id }, flow.ToDto()); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | [HttpPut("{id}")] |
| | 67 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 68 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 69 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 70 | | public async Task<ActionResult> UpdateFlow(int id, FlowUpdateDto flowUpdateDto) |
| 0 | 71 | | { |
| 0 | 72 | | await _flowService.UpdateFlowAsync(id, flowUpdateDto.ToBusinessModel()); |
| 0 | 73 | | return NoContent(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | [HttpPost("{id}/TestDelete")] |
| | 77 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 78 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 79 | | public async Task<ActionResult> TestDeleteFlow(int id) |
| 0 | 80 | | { |
| 0 | 81 | | await _flowService.TestDeleteFlowAsync(id); |
| 0 | 82 | | return Ok(); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | [HttpDelete("{id}")] |
| | 86 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 87 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 88 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 89 | | public async Task<ActionResult> DeleteFlow(int id) |
| 0 | 90 | | { |
| 0 | 91 | | await _flowService.TestDeleteFlowAsync(id); |
| 0 | 92 | | if(!await _flowService.DeleteFlowAsync(id)) |
| 0 | 93 | | { |
| 0 | 94 | | return NotFound(); |
| | 95 | | } |
| 0 | 96 | | return NoContent(); |
| 0 | 97 | | } |
| | 98 | | } |