wanshenmean
5 天以前 b0327633d7d0c19693a4e577d1e17b3b22e8274e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
    }
}