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
124 lines
3.6 KiB
C#
124 lines
3.6 KiB
C#
using BCards.Web.Areas.Support.Models;
|
|
using MongoDB.Driver;
|
|
|
|
namespace BCards.Web.Areas.Support.Repositories;
|
|
|
|
public class RatingRepository : IRatingRepository
|
|
{
|
|
private readonly IMongoCollection<Rating> _ratings;
|
|
private readonly ILogger<RatingRepository> _logger;
|
|
|
|
public RatingRepository(IMongoDatabase database, ILogger<RatingRepository> logger)
|
|
{
|
|
_ratings = database.GetCollection<Rating>("ratings");
|
|
_logger = logger;
|
|
|
|
// Criar índices
|
|
CreateIndexes();
|
|
}
|
|
|
|
private void CreateIndexes()
|
|
{
|
|
try
|
|
{
|
|
var indexKeysDefinition = Builders<Rating>.IndexKeys.Descending(r => r.CreatedAt);
|
|
var indexModel = new CreateIndexModel<Rating>(indexKeysDefinition);
|
|
_ratings.Indexes.CreateOne(indexModel);
|
|
|
|
var userIdIndexKeys = Builders<Rating>.IndexKeys.Ascending(r => r.UserId);
|
|
var userIdIndexModel = new CreateIndexModel<Rating>(userIdIndexKeys);
|
|
_ratings.Indexes.CreateOne(userIdIndexModel);
|
|
|
|
var ratingValueIndexKeys = Builders<Rating>.IndexKeys.Ascending(r => r.RatingValue);
|
|
var ratingValueIndexModel = new CreateIndexModel<Rating>(ratingValueIndexKeys);
|
|
_ratings.Indexes.CreateOne(ratingValueIndexModel);
|
|
|
|
var cultureIndexKeys = Builders<Rating>.IndexKeys.Ascending(r => r.Culture);
|
|
var cultureIndexModel = new CreateIndexModel<Rating>(cultureIndexKeys);
|
|
_ratings.Indexes.CreateOne(cultureIndexModel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Não foi possível criar índices para a collection ratings");
|
|
}
|
|
}
|
|
|
|
public async Task<Rating> CreateAsync(Rating rating)
|
|
{
|
|
try
|
|
{
|
|
await _ratings.InsertOneAsync(rating);
|
|
_logger.LogInformation("Rating criado com sucesso: {RatingId}", rating.Id);
|
|
return rating;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao criar rating");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<Rating>> GetRecentAsync(int limit = 10)
|
|
{
|
|
try
|
|
{
|
|
return await _ratings
|
|
.Find(_ => true)
|
|
.SortByDescending(r => r.CreatedAt)
|
|
.Limit(limit)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao buscar ratings recentes");
|
|
return new List<Rating>();
|
|
}
|
|
}
|
|
|
|
public async Task<double> GetAverageRatingAsync()
|
|
{
|
|
try
|
|
{
|
|
var ratings = await _ratings.Find(_ => true).ToListAsync();
|
|
if (ratings.Count == 0)
|
|
return 0;
|
|
|
|
return ratings.Average(r => r.RatingValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao calcular média de ratings");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<int> GetTotalCountAsync()
|
|
{
|
|
try
|
|
{
|
|
return (int)await _ratings.CountDocumentsAsync(_ => true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao contar ratings");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<List<Rating>> GetByUserIdAsync(string userId)
|
|
{
|
|
try
|
|
{
|
|
return await _ratings
|
|
.Find(r => r.UserId == userId)
|
|
.SortByDescending(r => r.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Erro ao buscar ratings do usuário {UserId}", userId);
|
|
return new List<Rating>();
|
|
}
|
|
}
|
|
}
|