63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using GreenHome.Application;
|
|
|
|
namespace GreenHome.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class MonthlyReportController : ControllerBase
|
|
{
|
|
private readonly IMonthlyReportService _monthlyReportService;
|
|
private readonly ILogger<MonthlyReportController> _logger;
|
|
|
|
public MonthlyReportController(
|
|
IMonthlyReportService monthlyReportService,
|
|
ILogger<MonthlyReportController> logger)
|
|
{
|
|
_monthlyReportService = monthlyReportService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// دریافت گزارش آماری ماهانه
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ActionResult<MonthlyReportDto>> GetMonthlyReport(
|
|
[FromQuery] int deviceId,
|
|
[FromQuery] int year,
|
|
[FromQuery] int month,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
if (deviceId <= 0)
|
|
{
|
|
return BadRequest(new { error = "شناسه دستگاه نامعتبر است" });
|
|
}
|
|
|
|
if (month < 1 || month > 12)
|
|
{
|
|
return BadRequest(new { error = "ماه باید بین 1 تا 12 باشد" });
|
|
}
|
|
|
|
var result = await _monthlyReportService.GetMonthlyReportAsync(deviceId, year, month, cancellationToken);
|
|
|
|
_logger.LogInformation(
|
|
"گزارش ماهانه برای دستگاه {DeviceId} و ماه {Month}/{Year} ایجاد شد",
|
|
deviceId, month, year);
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return NotFound(new { error = ex.Message });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error generating monthly report");
|
|
return StatusCode(500, new { error = "خطا در ایجاد گزارش ماهانه" });
|
|
}
|
|
}
|
|
}
|
|
|