using Microsoft.AspNetCore.Mvc; using WIDESEAWCS_S7Simulator.Application.Protocol; using WIDESEAWCS_S7Simulator.Core.Protocol; namespace WIDESEAWCS_S7Simulator.Server.Controllers; [ApiController] [Route("api/[controller]")] public class ProtocolTemplatesController : ControllerBase { private readonly IProtocolTemplateService _templateService; public ProtocolTemplatesController(IProtocolTemplateService templateService) { _templateService = templateService; } [HttpGet] public async Task>> GetAll() { return Ok(await _templateService.GetAllAsync()); } [HttpGet("{id}")] public async Task> GetById(string id) { var template = await _templateService.GetByIdAsync(id); if (template == null) { return NotFound(new { error = $"Protocol template '{id}' not found." }); } return Ok(template); } [HttpPost] public async Task> Create([FromBody] ProtocolTemplate template) { if (template == null || string.IsNullOrWhiteSpace(template.Id)) { return BadRequest(new { error = "Template id is required." }); } var result = await _templateService.UpsertAsync(template); return Ok(result); } [HttpPut("{id}")] public async Task> Update(string id, [FromBody] ProtocolTemplate template) { if (template == null) { return BadRequest(new { error = "Template data is required." }); } template.Id = id; var result = await _templateService.UpsertAsync(template); return Ok(result); } [HttpDelete("{id}")] public async Task Delete(string id) { var removed = await _templateService.DeleteAsync(id); if (!removed) { return NotFound(new { error = $"Protocol template '{id}' not found." }); } return NoContent(); } }