51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using AVSCartonShipmentVerifier.DTOs;
|
|
using AVSCartonShipmentVerifier.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace AVSCartonShipmentVerifier.Controllers;
|
|
|
|
[Route("api/verification")]
|
|
public sealed class VerificationController(
|
|
IShipmentVerificationService shipmentVerificationService,
|
|
IScanHistoryStore scanHistoryStore,
|
|
ILogger<VerificationController> logger) : Controller
|
|
{
|
|
private readonly IShipmentVerificationService _shipmentVerificationService = shipmentVerificationService;
|
|
private readonly IScanHistoryStore _scanHistoryStore = scanHistoryStore;
|
|
private readonly ILogger<VerificationController> _logger = logger;
|
|
|
|
[HttpPost("scan")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Scan([FromBody] QrScanRequestDto request, CancellationToken cancellationToken)
|
|
{
|
|
if (request is null)
|
|
{
|
|
return BadRequest(new { message = "Request payload is required." });
|
|
}
|
|
|
|
try
|
|
{
|
|
var verificationResult = await _shipmentVerificationService.VerifyQrAsync(request.QrValue, cancellationToken);
|
|
return Ok(new
|
|
{
|
|
result = verificationResult,
|
|
recentScans = _scanHistoryStore.GetLastFive()
|
|
});
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return StatusCode(StatusCodes.Status499ClientClosedRequest);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_logger.LogError(exception, "Unexpected error while verifying QR scan.");
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
|
{
|
|
message = "An unexpected error occurred during verification."
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|