RobotNet/RobotNet.ScriptManager/Controllers/ScriptVariablesController.cs
2025-10-15 15:15:53 +07:00

73 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RobotNet.Script.Shares;
using RobotNet.ScriptManager.Services;
using RobotNet.Shares;
namespace RobotNet.ScriptManager.Controllers;
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class ScriptVariablesController(ScriptGlobalsManager globalManager) : ControllerBase
{
[HttpGet]
[Route("")]
public IEnumerable<ScriptVariableDto> GetAlls() => globalManager.GetVariablesData();
[HttpGet]
[Route("{name}")]
public MessageResult<string> GetVariableValue(string name)
{
if (globalManager.Globals.TryGetValue(name, out var value))
{
return new (true, "") { Data = value?.ToString() ?? "null" };
}
else
{
return new(false, $"Variable \"{name}\" not found.");
}
}
[HttpPut]
[Route("{name}")]
public MessageResult UpdateVariableValue(string name, [FromBody] UpdateVariableModel model)
{
try
{
if(name != model.Name)
{
return new MessageResult(false, "Variable name in the URL does not match the name in the body.");
}
globalManager.SetValue(model.Name, model.Value);
return new MessageResult(true, $"Variable \"{model.Name}\" updated {model.Value} successfully.");
}
catch (Exception ex)
{
return new MessageResult(false, ex.Message);
}
}
[HttpPut]
[Route("{name}/Reset")]
public MessageResult ResetVariableValue(string name, [FromBody] UpdateVariableModel model)
{
try
{
if (name != model.Name)
{
return new MessageResult(false, "Variable name in the URL does not match the name in the body.");
}
globalManager.ResetValue(model.Name);
return new MessageResult(true, $"Variable \"{model.Name}\" reset successfully.");
}
catch (Exception ex)
{
return new MessageResult(false, ex.Message);
}
}
}