Skip to content

Commit

Permalink
(#421) identity: update query handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 23, 2024
1 parent 49eff5a commit 83980b4
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Convey.CQRS.Queries;
using Convey.Persistence.MongoDB;
using MiniSpace.Services.Identity.Application.DTO;
using MiniSpace.Services.Identity.Application.Queries;
using MiniSpace.Services.Identity.Infrastructure.Mongo.Documents;
using MongoDB.Driver;

namespace MiniSpace.Services.Identity.Infrastructure.Mongo.Queries.Handlers
{
internal sealed class GetOnlineUsersHandler : IQueryHandler<GetOnlineUsers, PagedResult<UserDto>>
{
private readonly IMongoRepository<UserDocument, Guid> _userRepository;

public GetOnlineUsersHandler(IMongoRepository<UserDocument, Guid> userRepository)
{
_userRepository = userRepository;
}

public async Task<PagedResult<UserDto>> HandleAsync(GetOnlineUsers query)
{
var filter = Builders<UserDocument>.Filter.Eq(u => u.IsOnline, true);

var totalResults = await _userRepository.Collection.CountDocumentsAsync(filter);

var totalPages = (int)Math.Ceiling(totalResults / (double)query.PageSize);

var onlineUsers = await _userRepository.Collection
.Find(filter)
.SortByDescending(u => u.LastActive)
.Skip((query.Page - 1) * query.PageSize)
.Limit(query.PageSize)
.ToListAsync();

return PagedResult<UserDto>.Create(
onlineUsers.Select(user => user.AsDto()),
query.Page,
query.PageSize,
totalPages,
totalResults
);
}
}
}

0 comments on commit 83980b4

Please sign in to comment.