68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using Modules.User.Application.Gateways;
|
|
using Modules.User.Application.Models;
|
|
using Modules.User.Database.Repositories;
|
|
|
|
namespace Modules.User.Database.GatewaysImplementations;
|
|
|
|
public class AccountGateway(SessionRepository repository) : IAccountGateway
|
|
{
|
|
public async Task<List<Session>> GetAccountSessions(Guid accountId)
|
|
{
|
|
var sessions = await repository.GetAll(accountId);
|
|
return sessions.Select(q => new Session
|
|
{
|
|
Id = q.Id,
|
|
RefreshToken = q.RefreshToken,
|
|
ClientInfo = new()
|
|
{
|
|
UserAgent = q.ClientInfo.UserAgent,
|
|
Location = new()
|
|
{
|
|
Country = q.ClientInfo.Country,
|
|
Region = q.ClientInfo.Region,
|
|
},
|
|
},
|
|
AccountId = q.AccountId,
|
|
ExpiredDate = q.ExpiredDate,
|
|
}).ToList();
|
|
}
|
|
|
|
public async Task<Session?> TryGetSession(string refreshToken)
|
|
{
|
|
var session = await repository.GetFirstOrDefaultWhere(q => q.RefreshToken == refreshToken.Trim()
|
|
&& q.ExpiredDate > DateTime.UtcNow);
|
|
return session == null ? null : new Session
|
|
{
|
|
Id = session.Id,
|
|
RefreshToken = session.RefreshToken,
|
|
ClientInfo = new()
|
|
{
|
|
UserAgent = session.ClientInfo.UserAgent,
|
|
Location = new()
|
|
{
|
|
Country = session.ClientInfo.Country,
|
|
Region = session.ClientInfo.Region,
|
|
},
|
|
},
|
|
AccountId = session.AccountId,
|
|
ExpiredDate = session.ExpiredDate,
|
|
};
|
|
}
|
|
|
|
public async Task<bool> UpdateSessions(Guid accountId, IEnumerable<Session> sessions)
|
|
{
|
|
return await repository.SyncSessions(accountId, sessions.Select(q => new Database.Entities.Session
|
|
{
|
|
Id = q.Id != Guid.Empty ? q.Id : Guid.NewGuid(),
|
|
RefreshToken = q.RefreshToken,
|
|
ClientInfo = new()
|
|
{
|
|
UserAgent = q.ClientInfo.UserAgent,
|
|
Country = q.ClientInfo.Location.Country,
|
|
Region = q.ClientInfo.Location.Region,
|
|
},
|
|
AccountId = q.AccountId,
|
|
ExpiredDate = q.ExpiredDate,
|
|
}));
|
|
}
|
|
} |