RobotNet/RobotNet.MapManager/Controllers/NodesController.cs
2025-10-15 15:15:53 +07:00

58 lines
2.0 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RobotNet.MapManager.Data;
using RobotNet.MapShares.Dtos;
using RobotNet.Shares;
namespace RobotNet.MapManager.Controllers;
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class NodesController(MapEditorDbContext MapDb) : ControllerBase
{
[HttpGet]
[Route("{mapId}")]
public async Task<NodeDto[]> GetNodes(Guid mapId)
{
return await (from node in MapDb.Nodes
where node.MapId == mapId
select new NodeDto()
{
Id = node.Id,
Name = node.Name,
MapId = mapId,
X = node.X,
Y = node.Y,
Theta = node.Theta,
AllowedDeviationXy = node.AllowedDeviationXy,
AllowedDeviationTheta = node.AllowedDeviationTheta,
Actions = node.Actions,
}).ToArrayAsync();
}
[HttpPut]
[Route("")]
public async Task<MessageResult> Update([FromBody] NodeUpdateModel model)
{
var node = await MapDb.Nodes.FindAsync(model.Id);
if (node == null) return new(false, $"Không tồn tại node id = {model.Id}");
if (node.Name != model.Name && !string.IsNullOrWhiteSpace(model.Name) && await MapDb.Nodes.AnyAsync(n => n.Name == model.Name && n.MapId == node.MapId))
{
return new(false, $"Tên node {model.Name} đã tồn tại trong map");
}
node.Name = model.Name;
node.X = model.X;
node.Y = model.Y;
node.Theta = model.Theta;
node.AllowedDeviationXy = model.AllowedDeviationXy;
node.AllowedDeviationTheta = model.AllowedDeviationTheta;
node.Actions = System.Text.Json.JsonSerializer.Serialize(model.Actions ?? []);
await MapDb.SaveChangesAsync();
return new(true);
}
}