IDIS_Server.Console/IDIS_Server-SIM/Controllers/IDIS_TemplateController.cs

69 lines
1.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BITKit;
using IDIS;
using IDIS.Models;
using IDIS.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Controllers;
[ApiController]
public class IDIS_TemplateController:Controller
{
private readonly IDIS_TemplateService _service;
private readonly ILogger<IDIS_TemplateController> _logger;
public IDIS_TemplateController(IDIS_TemplateService service, ILogger<IDIS_TemplateController> logger)
{
_service = service;
_logger = logger;
}
/// <summary>
/// 查询标识模板
/// </summary>
/// <param name="prefix">前缀例如88.123.99</param>
/// <param name="version">版本例如v0.0.1</param>
/// <returns></returns>
[Route("/snms/api/template/v1")]
[HttpGet]
public async Task<IActionResult> Query(string prefix, string version)
{
try
{
return Ok(new IDIS_Response(await _service.QueryAsync(prefix, version)));
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message,false)
{
Status = 2
});
}
}
/// <summary>
/// 保存标识模板
/// </summary>
/// <returns></returns>
[Route("/snms/api/template/v1")]
[HttpPost]
public async Task<IActionResult> Save()
{
using var reader = new StreamReader(Request.Body);
var json = await reader.ReadToEndAsync();
try
{
var data = JsonConvert.DeserializeObject<IDIS_Template>(json)!;
await _service.SaveAsync(data);
return Ok(new IDIS_Response());
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message, false)
{
Status = 2
});
}
}
}