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