Files
PnvPanel/backend/src/PnvPanel.Application/Admin/Nodes/DeleteNodeCommandHandler.cs
T

25 lines
948 B
C#

using Microsoft.EntityFrameworkCore;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Messaging;
using PnvPanel.Application.Common.Models;
namespace PnvPanel.Application.Admin.Nodes;
public sealed class DeleteNodeCommandHandler(IAppDbContext dbContext, IXuiPanelGateway gateway)
: ICommandHandler<DeleteNodeCommand, Result>
{
public async Task<Result> Handle(DeleteNodeCommand command, CancellationToken cancellationToken)
{
var node = await dbContext.Nodes.FirstOrDefaultAsync(n => n.Id == command.NodeId, cancellationToken);
if (node is null)
return Result.Failure(NodeErrors.NotFound);
var inbounds = await dbContext.Inbounds.Where(i => i.NodeId == node.Id).ToListAsync(cancellationToken);
dbContext.Inbounds.RemoveRange(inbounds);
dbContext.Nodes.Remove(node);
gateway.InvalidateClient(node.Id);
return Result.Success();
}
}