43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Hybrid;
|
|
using Moonlight.Api.Database;
|
|
using Moonlight.Api.Database.Entities;
|
|
using Moonlight.Api.Interfaces;
|
|
|
|
namespace Moonlight.Api.Services;
|
|
|
|
public class UserLogoutService
|
|
{
|
|
private readonly DatabaseRepository<User> Repository;
|
|
private readonly IEnumerable<IUserLogoutHook> Hooks;
|
|
private readonly HybridCache HybridCache;
|
|
|
|
public UserLogoutService(
|
|
DatabaseRepository<User> repository,
|
|
IEnumerable<IUserLogoutHook> hooks,
|
|
HybridCache hybridCache
|
|
)
|
|
{
|
|
Repository = repository;
|
|
Hooks = hooks;
|
|
HybridCache = hybridCache;
|
|
}
|
|
|
|
public async Task LogoutAsync(int userId)
|
|
{
|
|
var user = await Repository
|
|
.Query()
|
|
.FirstOrDefaultAsync(x => x.Id == userId);
|
|
|
|
if (user == null)
|
|
throw new AggregateException($"User with id {userId} not found");
|
|
|
|
foreach (var hook in Hooks)
|
|
await hook.ExecuteAsync(user);
|
|
|
|
user.InvalidateTimestamp = DateTimeOffset.UtcNow;
|
|
await Repository.UpdateAsync(user);
|
|
|
|
await HybridCache.RemoveAsync(string.Format(UserAuthService.CacheKeyPattern, user.Id));
|
|
}
|
|
} |