|  |  | 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.ApiKeyOrCertificateScheme)] | 
|  |  | 20 |  | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] | 
|  |  | 21 |  | [ValidateLgdxUserAccess] | 
|  | 0 | 22 |  | public class TriggersController( | 
|  | 0 | 23 |  |     IOptionsSnapshot<LgdxRobotCloudConfiguration> lgdxRobotCloudConfiguration, | 
|  | 0 | 24 |  |     ITriggerService triggerService | 
|  | 0 | 25 |  |   ) : ControllerBase | 
|  |  | 26 |  | { | 
|  | 0 | 27 |  |   private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = lgdxRobotCloudConfiguration.Value ?? throw | 
|  | 0 | 28 |  |   private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer | 
|  |  | 29 |  |  | 
|  |  | 30 |  |   [HttpGet("")] | 
|  |  | 31 |  |   [ProducesResponseType(typeof(IEnumerable<TriggerListDto>), StatusCodes.Status200OK)] | 
|  |  | 32 |  |   public async Task<ActionResult<IEnumerable<TriggerListDto>>> GetTriggers(string? name, int pageNumber = 1, int pageSiz | 
|  | 0 | 33 |  |   { | 
|  | 0 | 34 |  |     pageSize = (pageSize > _lgdxRobotCloudConfiguration.ApiMaxPageSize) ? _lgdxRobotCloudConfiguration.ApiMaxPageSize :  | 
|  | 0 | 35 |  |     var (triggers, PaginationHelper) = await _triggerService.GetTriggersAsync(name, pageNumber, pageSize); | 
|  | 0 | 36 |  |     Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper)); | 
|  | 0 | 37 |  |     return Ok(triggers.ToDto()); | 
|  | 0 | 38 |  |   } | 
|  |  | 39 |  |  | 
|  |  | 40 |  |   [HttpGet("Search")] | 
|  |  | 41 |  |   [ProducesResponseType(typeof(IEnumerable<TriggerSearchDto>), StatusCodes.Status200OK)] | 
|  |  | 42 |  |   public async Task<ActionResult<IEnumerable<TriggerSearchDto>>> SearchTriggers(string? name) | 
|  | 0 | 43 |  |   { | 
|  | 0 | 44 |  |     var triggers = await _triggerService.SearchTriggersAsync(name); | 
|  | 0 | 45 |  |     return Ok(triggers.ToDto()); | 
|  | 0 | 46 |  |   } | 
|  |  | 47 |  |  | 
|  |  | 48 |  |   [HttpGet("{id}", Name = "GetTrigger")] | 
|  |  | 49 |  |   [ProducesResponseType(typeof(TriggerDto), StatusCodes.Status200OK)] | 
|  |  | 50 |  |   [ProducesResponseType(StatusCodes.Status404NotFound)] | 
|  |  | 51 |  |   public async Task<ActionResult<TriggerDto>> GetTrigger(int id) | 
|  | 0 | 52 |  |   { | 
|  | 0 | 53 |  |     var trigger = await _triggerService.GetTriggerAsync(id); | 
|  | 0 | 54 |  |     return Ok(trigger.ToDto()); | 
|  | 0 | 55 |  |   } | 
|  |  | 56 |  |  | 
|  |  | 57 |  |   [HttpPost("")] | 
|  |  | 58 |  |   [ProducesResponseType(typeof(TriggerDto), StatusCodes.Status201Created)] | 
|  |  | 59 |  |   [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] | 
|  |  | 60 |  |   public async Task<ActionResult> CreateTrigger(TriggerCreateDto triggerCreateDto) | 
|  | 0 | 61 |  |   { | 
|  | 0 | 62 |  |     var trigger = await _triggerService.CreateTriggerAsync(triggerCreateDto.ToBusinessModel()); | 
|  | 0 | 63 |  |     return CreatedAtAction(nameof(GetTrigger), new { id = trigger.Id }, trigger.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> UpdateTrigger(int id, TriggerUpdateDto triggerUpdateDto) | 
|  | 0 | 71 |  |   { | 
|  | 0 | 72 |  |     if (!await _triggerService.UpdateTriggerAsync(id, triggerUpdateDto.ToBusinessModel())) | 
|  | 0 | 73 |  |     { | 
|  | 0 | 74 |  |       return NotFound(); | 
|  |  | 75 |  |     } | 
|  | 0 | 76 |  |     return NoContent(); | 
|  | 0 | 77 |  |   } | 
|  |  | 78 |  |  | 
|  |  | 79 |  |   [HttpPost("{id}/TestDelete")] | 
|  |  | 80 |  |   [ProducesResponseType(StatusCodes.Status200OK)] | 
|  |  | 81 |  |   [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] | 
|  |  | 82 |  |   public async Task<ActionResult> TestDeleteTrigger(int id) | 
|  | 0 | 83 |  |   { | 
|  | 0 | 84 |  |     await _triggerService.TestDeleteTriggerAsync(id); | 
|  | 0 | 85 |  |     return Ok(); | 
|  | 0 | 86 |  |   } | 
|  |  | 87 |  |  | 
|  |  | 88 |  |   [HttpDelete("{id}")] | 
|  |  | 89 |  |   [ProducesResponseType(StatusCodes.Status204NoContent)] | 
|  |  | 90 |  |   [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] | 
|  |  | 91 |  |   [ProducesResponseType(StatusCodes.Status404NotFound)] | 
|  |  | 92 |  |   public async Task<ActionResult> DeleteTrigger(int id) | 
|  | 0 | 93 |  |   { | 
|  | 0 | 94 |  |     await _triggerService.TestDeleteTriggerAsync(id); | 
|  | 0 | 95 |  |     if (!await _triggerService.DeleteTriggerAsync(id)) | 
|  | 0 | 96 |  |     { | 
|  | 0 | 97 |  |       return NotFound(); | 
|  |  | 98 |  |     } | 
|  | 0 | 99 |  |     return NoContent(); | 
|  | 0 | 100 |  |   } | 
|  |  | 101 |  | } |