151 lines
4.5 KiB
C#
151 lines
4.5 KiB
C#
using AmtScanner.Api.Data;
|
|
using AmtScanner.Api.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AmtScanner.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/amt-devices")]
|
|
[Authorize]
|
|
public class AmtDevicesController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
private readonly ILogger<AmtDevicesController> _logger;
|
|
|
|
public AmtDevicesController(AppDbContext db, ILogger<AmtDevicesController> logger)
|
|
{
|
|
_db = db;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取已接管设备列表
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetList([FromQuery] int page = 1, [FromQuery] int pageSize = 20, [FromQuery] string? search = null)
|
|
{
|
|
var query = _db.AmtDevices_new.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(d =>
|
|
d.IpAddress.Contains(search) ||
|
|
d.Uuid.Contains(search) ||
|
|
(d.Hostname != null && d.Hostname.Contains(search)));
|
|
}
|
|
|
|
var total = await query.CountAsync();
|
|
var items = await query
|
|
.OrderByDescending(d => d.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(d => new
|
|
{
|
|
d.Uuid,
|
|
d.IpAddress,
|
|
d.SubnetMask,
|
|
d.Gateway,
|
|
d.AmtUsername,
|
|
// 不返回密码
|
|
d.AmtVersion,
|
|
d.Hostname,
|
|
d.CreatedAt,
|
|
d.UpdatedAt
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(ApiResponse<object>.Success(new { items, total, page, pageSize }));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单个设备详情
|
|
/// </summary>
|
|
[HttpGet("{uuid}")]
|
|
public async Task<IActionResult> GetDevice(string uuid)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
return Ok(ApiResponse<object>.Success(new
|
|
{
|
|
device.Uuid,
|
|
device.IpAddress,
|
|
device.SubnetMask,
|
|
device.Gateway,
|
|
device.AmtUsername,
|
|
device.AmtVersion,
|
|
device.Hostname,
|
|
device.CreatedAt,
|
|
device.UpdatedAt
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新设备信息
|
|
/// </summary>
|
|
[HttpPut("{uuid}")]
|
|
public async Task<IActionResult> UpdateDevice(string uuid, [FromBody] UpdateAmtDeviceRequest request)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.IpAddress))
|
|
device.IpAddress = request.IpAddress;
|
|
if (!string.IsNullOrEmpty(request.SubnetMask))
|
|
device.SubnetMask = request.SubnetMask;
|
|
if (!string.IsNullOrEmpty(request.Gateway))
|
|
device.Gateway = request.Gateway;
|
|
if (!string.IsNullOrEmpty(request.AmtUsername))
|
|
device.AmtUsername = request.AmtUsername;
|
|
if (!string.IsNullOrEmpty(request.AmtPassword))
|
|
device.AmtPassword = request.AmtPassword;
|
|
if (!string.IsNullOrEmpty(request.Hostname))
|
|
device.Hostname = request.Hostname;
|
|
|
|
device.UpdatedAt = DateTime.UtcNow;
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(ApiResponse<object>.Success(null, "更新成功"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除设备
|
|
/// </summary>
|
|
[HttpDelete("{uuid}")]
|
|
public async Task<IActionResult> DeleteDevice(string uuid)
|
|
{
|
|
var device = await _db.AmtDevices_new.FindAsync(uuid);
|
|
|
|
if (device == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.Fail(404, "设备不存在"));
|
|
}
|
|
|
|
_db.AmtDevices_new.Remove(device);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(ApiResponse<object>.Success(null, "删除成功"));
|
|
}
|
|
}
|
|
|
|
public class UpdateAmtDeviceRequest
|
|
{
|
|
public string? IpAddress { get; set; }
|
|
public string? SubnetMask { get; set; }
|
|
public string? Gateway { get; set; }
|
|
public string? AmtUsername { get; set; }
|
|
public string? AmtPassword { get; set; }
|
|
public string? Hostname { get; set; }
|
|
}
|