62 lines
2.0 KiB
C#

using AmtScanner.Api.Models;
using AmtScanner.Api.Services;
using Microsoft.AspNetCore.Mvc;
namespace AmtScanner.Api.Controllers;
[ApiController]
[Route("api/hardware-info")]
public class HardwareInfoController : ControllerBase
{
private readonly IHardwareInfoService _hardwareInfoService;
private readonly ILogger<HardwareInfoController> _logger;
public HardwareInfoController(
IHardwareInfoService hardwareInfoService,
ILogger<HardwareInfoController> logger)
{
_hardwareInfoService = hardwareInfoService;
_logger = logger;
}
[HttpGet("{deviceId}")]
public async Task<ActionResult<ApiResponse<HardwareInfoDto>>> GetHardwareInfo(
long deviceId,
[FromQuery] bool refresh = false)
{
try
{
var result = await _hardwareInfoService.GetHardwareInfoAsync(deviceId, refresh);
return Ok(ApiResponse<HardwareInfoDto>.Success(result));
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting hardware info for device {DeviceId}", deviceId);
return Ok(ApiResponse<HardwareInfoDto>.Fail(500, ex.Message));
}
}
[HttpPost("batch")]
public async Task<ActionResult<ApiResponse<BatchHardwareInfoResponse>>> GetBatchHardwareInfo(
[FromBody] BatchHardwareInfoRequest request)
{
try
{
var results = await _hardwareInfoService.GetBatchHardwareInfoAsync(
request.DeviceIds,
request.Refresh);
return Ok(ApiResponse<BatchHardwareInfoResponse>.Success(new BatchHardwareInfoResponse { Results = results }));
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting batch hardware info");
return Ok(ApiResponse<BatchHardwareInfoResponse>.Fail(500, ex.Message));
}
}
}
public class BatchHardwareInfoResponse
{
public List<BatchHardwareInfoResult> Results { get; set; } = new();
}