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<ActionResult<IReadOnlyList<ProtocolTemplate>>> GetAll()
|
{
|
return Ok(await _templateService.GetAllAsync());
|
}
|
|
[HttpGet("{id}")]
|
public async Task<ActionResult<ProtocolTemplate>> 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<ActionResult<ProtocolTemplate>> 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<ActionResult<ProtocolTemplate>> 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<IActionResult> Delete(string id)
|
{
|
var removed = await _templateService.DeleteAsync(id);
|
if (!removed)
|
{
|
return NotFound(new { error = $"Protocol template '{id}' not found." });
|
}
|
|
return NoContent();
|
}
|
}
|