83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using public_valetax.Repositories;
|
||
|
|
using public_valetax.Exceptions;
|
||
|
|
|
||
|
|
namespace public_valetax.Controllers
|
||
|
|
{
|
||
|
|
[ApiController]
|
||
|
|
[Route("api.[controller]")]
|
||
|
|
public class NodeController(ITreeRepository _treeRepository) : ControllerBase
|
||
|
|
{
|
||
|
|
[HttpPost("create")]
|
||
|
|
public async Task<IActionResult> Create(
|
||
|
|
[FromQuery] string treeName,
|
||
|
|
[FromQuery] long? parentNodeId,
|
||
|
|
[FromQuery] string nodeName)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// Get or create the tree
|
||
|
|
var tree = await _treeRepository.GetTreeByNameAsync(treeName);
|
||
|
|
if (tree == null)
|
||
|
|
{
|
||
|
|
tree = await _treeRepository.CreateTreeAsync(treeName);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create the node
|
||
|
|
var node = await _treeRepository.CreateNodeAsync(tree.Id, parentNodeId, nodeName);
|
||
|
|
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
catch (SecureException)
|
||
|
|
{
|
||
|
|
// Re-throw secure exceptions to be handled by middleware
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
// Wrap other exceptions in SecureException to trigger proper error handling
|
||
|
|
throw new SecureException(ex.Message, ex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("delete")]
|
||
|
|
public async Task<IActionResult> Delete([FromQuery] long nodeId)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await _treeRepository.DeleteNodeAsync(nodeId);
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
catch (SecureException)
|
||
|
|
{
|
||
|
|
// Re-throw secure exceptions to be handled by middleware
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
// Wrap other exceptions in SecureException to trigger proper error handling
|
||
|
|
throw new SecureException(ex.Message, ex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("rename")]
|
||
|
|
public async Task<IActionResult> Rename([FromQuery] long nodeId, [FromQuery] string newNodeName)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await _treeRepository.RenameNodeAsync(nodeId, newNodeName);
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
catch (SecureException)
|
||
|
|
{
|
||
|
|
// Re-throw secure exceptions to be handled by middleware
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
// Wrap other exceptions in SecureException to trigger proper error handling
|
||
|
|
throw new SecureException(ex.Message, ex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|