Зависимости на контракты
This commit is contained in:
@@ -4,15 +4,17 @@ using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Knot.Contracts.Messaging.Abstractions;
|
||||
using Knot.Contracts.Messaging.Application.Abstractions;
|
||||
using Knot.Contracts.Messaging.Domain;
|
||||
using Knot.Modules.Messaging.Application.Abstractions;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using ContractUserStatsService = Knot.Contracts.Messaging.Application.Abstractions.IUserStatsService;
|
||||
using ModuleUserStatsService = Knot.Modules.Messaging.Application.Abstractions.IUserStatsService;
|
||||
|
||||
namespace Knot.Modules.Messaging.Infrastructure.Persistence.Mongo;
|
||||
|
||||
public sealed class UserStatsService : ModuleUserStatsService
|
||||
public sealed class UserStatsService : ModuleUserStatsService, ContractUserStatsService
|
||||
{
|
||||
private readonly IMongoCollection<Message> _messages;
|
||||
|
||||
@@ -23,11 +25,10 @@ public sealed class UserStatsService : ModuleUserStatsService
|
||||
|
||||
public async Task<Dictionary<Guid, Knot.Modules.Messaging.Application.Abstractions.UserStats>> GetStatsForUsersAsync(IEnumerable<Guid> userIds, CancellationToken ct = default)
|
||||
{
|
||||
var userIdList = userIds.ToList();
|
||||
if (!userIdList.Any()) return new Dictionary<Guid, Knot.Modules.Messaging.Application.Abstractions.UserStats>();
|
||||
if (!userIds.Any()) return new Dictionary<Guid, Knot.Modules.Messaging.Application.Abstractions.UserStats>();
|
||||
|
||||
var stats = await _messages.Aggregate()
|
||||
.Match(Builders<Message>.Filter.In(m => m.SenderId, userIdList))
|
||||
.Match(Builders<Message>.Filter.In(m => m.SenderId, userIds))
|
||||
.Group(new BsonDocument {
|
||||
{ "_id", "$SenderId" },
|
||||
{ "Count", new BsonDocument("$sum", 1) },
|
||||
@@ -44,6 +45,28 @@ public sealed class UserStatsService : ModuleUserStatsService
|
||||
);
|
||||
}
|
||||
|
||||
async Task<Dictionary<Guid, Knot.Contracts.Messaging.Application.Abstractions.UserStats>> Knot.Contracts.Messaging.Application.Abstractions.IUserStatsService.GetStatsForUsersAsync(List<Guid> userIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!userIds.Any()) return new Dictionary<Guid, Knot.Contracts.Messaging.Application.Abstractions.UserStats>();
|
||||
|
||||
var stats = await _messages.Aggregate()
|
||||
.Match(Builders<Message>.Filter.In(m => m.SenderId, userIds))
|
||||
.Group(new BsonDocument {
|
||||
{ "_id", "$SenderId" },
|
||||
{ "Count", new BsonDocument("$sum", 1) },
|
||||
{ "MediaSize", new BsonDocument("$sum", new BsonDocument("$sum", "$Media.Size")) }
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return stats.ToDictionary(
|
||||
doc => doc["_id"].AsGuid,
|
||||
doc => new Knot.Contracts.Messaging.Application.Abstractions.UserStats(
|
||||
doc["Count"].AsInt32,
|
||||
doc.Contains("MediaSize") && !doc["MediaSize"].IsBsonNull ? (long)(doc["MediaSize"].IsInt64 ? doc["MediaSize"].AsInt64 : doc["MediaSize"].AsInt32) : 0L
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<long> GetTotalStorageSizeAsync(CancellationToken ct = default)
|
||||
{
|
||||
var result = await _messages.Aggregate()
|
||||
|
||||
Reference in New Issue
Block a user