All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 4s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 11m18s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Has been skipped
BCards Deployment Pipeline / Deploy to Release Swarm (ARM) (push) Successful in 17s
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s
87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
using BCards.Web.Areas.Support.Models;
|
|
using BCards.Web.Areas.Support.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
|
|
namespace BCards.Web.Areas.Support.Controllers;
|
|
|
|
[Area("Support")]
|
|
[Route("api/ratings")]
|
|
[ApiController]
|
|
public class RatingsController : ControllerBase
|
|
{
|
|
private readonly IRatingService _ratingService;
|
|
private readonly ILogger<RatingsController> _logger;
|
|
|
|
public RatingsController(IRatingService ratingService, ILogger<RatingsController> logger)
|
|
{
|
|
_ratingService = ratingService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> SubmitRating([FromBody] RatingSubmissionDto dto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
_logger.LogWarning("Rating inválido submetido: {Errors}", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var userId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
var success = await _ratingService.SubmitRatingAsync(dto, userId, HttpContext);
|
|
|
|
if (success)
|
|
{
|
|
_logger.LogInformation("Rating de {Stars} estrelas submetido com sucesso", dto.RatingValue);
|
|
return Ok(new { message = "Avaliação enviada com sucesso! Obrigado pelo feedback." });
|
|
}
|
|
|
|
_logger.LogError("Falha ao submeter rating");
|
|
return StatusCode(503, new { message = "Erro ao processar sua avaliação. Tente novamente mais tarde." });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao processar rating");
|
|
return StatusCode(500, new { message = "Erro interno ao processar sua avaliação." });
|
|
}
|
|
}
|
|
|
|
[HttpGet("average")]
|
|
public async Task<IActionResult> GetAverageRating()
|
|
{
|
|
try
|
|
{
|
|
var average = await _ratingService.GetAverageRatingAsync();
|
|
var total = await _ratingService.GetTotalCountAsync();
|
|
|
|
return Ok(new { average = Math.Round(average, 2), total });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao buscar média de ratings");
|
|
return StatusCode(500, new { message = "Erro ao buscar avaliações" });
|
|
}
|
|
}
|
|
|
|
[HttpGet("recent")]
|
|
public async Task<IActionResult> GetRecentRatings([FromQuery] int limit = 10)
|
|
{
|
|
try
|
|
{
|
|
if (limit < 1 || limit > 50)
|
|
limit = 10;
|
|
|
|
var ratings = await _ratingService.GetRecentRatingsAsync(limit);
|
|
return Ok(ratings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao buscar ratings recentes");
|
|
return StatusCode(500, new { message = "Erro ao buscar avaliações recentes" });
|
|
}
|
|
}
|
|
}
|