From e9eee5bfecec933ab4778ae02433bfa3feb9c64c Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Thu, 24 Jul 2025 21:37:12 +0700 Subject: [PATCH] UserSession namespace was changed --- .../Controllers/SessionControllerTests.cs | 1 + Govor.API/Hubs/GroupsHub.cs | 8 ++ .../UserSessions/UserSessionReaderTests.cs | 1 + .../UserSessions/UserSessionRevokerTests.cs | 1 + .../UserSession/IUserSessionReader.cs | 2 +- .../UserSessions/UserSessionOpener.cs | 129 +++++++++--------- .../UserSessions/UserSessionReader.cs | 3 +- .../UserSessions/UserSessionRefresher.cs | 2 +- Govor.Core/Models/{ => Users}/UserSession.cs | 2 +- .../IUserSessionsExist.cs | 2 +- .../IUserSessionsReader.cs | 2 +- .../IUserSessionsWriter.cs | 1 + .../UserSessionsRepositoryTests.cs | 1 + .../Repositories/UserSessionsRepository.cs | 2 +- 14 files changed, 85 insertions(+), 72 deletions(-) create mode 100644 Govor.API/Hubs/GroupsHub.cs rename Govor.Core/Models/{ => Users}/UserSession.cs (96%) diff --git a/Govor.API.Tests/Controllers/SessionControllerTests.cs b/Govor.API.Tests/Controllers/SessionControllerTests.cs index 3b95be3..b8add34 100644 --- a/Govor.API.Tests/Controllers/SessionControllerTests.cs +++ b/Govor.API.Tests/Controllers/SessionControllerTests.cs @@ -4,6 +4,7 @@ using Govor.Application.Interfaces.Infrastructure.Extensions; using Govor.Application.Interfaces.UserSession; using Govor.Contracts.DTOs; using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Data.Repositories.Exceptions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; diff --git a/Govor.API/Hubs/GroupsHub.cs b/Govor.API/Hubs/GroupsHub.cs new file mode 100644 index 0000000..2469e1e --- /dev/null +++ b/Govor.API/Hubs/GroupsHub.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.SignalR; + +namespace Govor.API.Hubs; + +public class GroupsHub : Hub +{ + +} \ No newline at end of file diff --git a/Govor.Application.Tests/Services/UserSessions/UserSessionReaderTests.cs b/Govor.Application.Tests/Services/UserSessions/UserSessionReaderTests.cs index 1b8e698..fbbcf6f 100644 --- a/Govor.Application.Tests/Services/UserSessions/UserSessionReaderTests.cs +++ b/Govor.Application.Tests/Services/UserSessions/UserSessionReaderTests.cs @@ -2,6 +2,7 @@ using AutoFixture; using Govor.Application.Interfaces.UserSession; using Govor.Application.Services.UserSessions; using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data.Repositories.Exceptions; using Microsoft.Extensions.Logging; diff --git a/Govor.Application.Tests/Services/UserSessions/UserSessionRevokerTests.cs b/Govor.Application.Tests/Services/UserSessions/UserSessionRevokerTests.cs index 26066ee..cd40008 100644 --- a/Govor.Application.Tests/Services/UserSessions/UserSessionRevokerTests.cs +++ b/Govor.Application.Tests/Services/UserSessions/UserSessionRevokerTests.cs @@ -1,5 +1,6 @@ using Govor.Application.Services.UserSessions; using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data.Repositories.Exceptions; using Microsoft.Extensions.Logging; diff --git a/Govor.Application/Interfaces/UserSession/IUserSessionReader.cs b/Govor.Application/Interfaces/UserSession/IUserSessionReader.cs index 3824747..3800917 100644 --- a/Govor.Application/Interfaces/UserSession/IUserSessionReader.cs +++ b/Govor.Application/Interfaces/UserSession/IUserSessionReader.cs @@ -2,5 +2,5 @@ namespace Govor.Application.Interfaces.UserSession; public interface IUserSessionReader { - Task> GetAllSessionsAsync(Guid userId); + Task> GetAllSessionsAsync(Guid userId); } diff --git a/Govor.Application/Services/UserSessions/UserSessionOpener.cs b/Govor.Application/Services/UserSessions/UserSessionOpener.cs index 5fe5e59..2d3d2be 100644 --- a/Govor.Application/Services/UserSessions/UserSessionOpener.cs +++ b/Govor.Application/Services/UserSessions/UserSessionOpener.cs @@ -7,83 +7,84 @@ using Govor.Data.Repositories.Exceptions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -namespace Govor.Application.Services.UserSessions; - -public class UserSessionOpener : IUserSessionOpener +namespace Govor.Application.Services.UserSessions { - private readonly IUserSessionsRepository _repository; - private readonly ILogger _logger; - private readonly JwtRefreshOption _options; - private readonly IJwtService _jwtService; - - public UserSessionOpener( - IUserSessionsRepository repository, - IJwtService jwtService, - IOptions options, - ILogger logger) + public class UserSessionOpener : IUserSessionOpener { - _jwtService = jwtService; - _repository = repository; - _logger = logger; - _options = options.Value; - } + private readonly IUserSessionsRepository _repository; + private readonly ILogger _logger; + private readonly JwtRefreshOption _options; + private readonly IJwtService _jwtService; - public async Task OpenSessionAsync(User user, string deviceInfo) - { - _logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'"); - - try + public UserSessionOpener( + IUserSessionsRepository repository, + IJwtService jwtService, + IOptions options, + ILogger logger) { - var sessions = await _repository.GetByUserIdAsync(user.Id); - var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo); + _jwtService = jwtService; + _repository = repository; + _logger = logger; + _options = options.Value; + } - var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user); - var accessToken = await _jwtService.GenerateAccessTokenAsync(user); - - var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays); + public async Task OpenSessionAsync(User user, string deviceInfo) + { + _logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'"); - if (session is not null) + try { - // Всегда обновляем токен и дату - session.RefreshToken = newRefreshToken; - session.ExpiresAt = newExpiresAt; - session.CreatedAt = DateTime.UtcNow; - session.IsRevoked = false; + var sessions = await _repository.GetByUserIdAsync(user.Id); + var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo); - await _repository.UpdateAsync(session); - _logger.LogInformation($"Updated session for user {user.Id} on device '{deviceInfo}'"); + var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user); + var accessToken = await _jwtService.GenerateAccessTokenAsync(user); + + var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays); - return new RefreshResult(session.RefreshToken, accessToken); + if (session is not null) + { + // Всегда обновляем токен и дату + session.RefreshToken = newRefreshToken; + session.ExpiresAt = newExpiresAt; + session.CreatedAt = DateTime.UtcNow; + session.IsRevoked = false; + + await _repository.UpdateAsync(session); + _logger.LogInformation($"Updated session for user {user.Id} on device '{deviceInfo}'"); + + return new RefreshResult(session.RefreshToken, accessToken); + } + + return await OpenNewSession(); } - - return await OpenNewSession(); - } - catch (NotFoundByKeyException ex) - { - return await OpenNewSession(); - } - - async Task OpenNewSession() - { - var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user); - var accessToken = await _jwtService.GenerateAccessTokenAsync(user); - - var newSession = new Core.Models.UserSession + catch (NotFoundByKeyException ex) { - UserId = user.Id, - DeviceInfo = deviceInfo, - RefreshToken = newRefreshToken, - CreatedAt = DateTime.UtcNow, - ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays), - IsRevoked = false - }; - - await _repository.AddAsync(newSession); + return await OpenNewSession(); + } + + async Task OpenNewSession() + { + var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user); + var accessToken = await _jwtService.GenerateAccessTokenAsync(user); - _logger.LogInformation($"Created new session for user {user.Id} on device '{deviceInfo}'"); + var newSession = new UserSession + { + UserId = user.Id, + DeviceInfo = deviceInfo, + RefreshToken = newRefreshToken, + CreatedAt = DateTime.UtcNow, + ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays), + IsRevoked = false + }; - return new RefreshResult(newRefreshToken, accessToken); + await _repository.AddAsync(newSession); + + _logger.LogInformation($"Created new session for user {user.Id} on device '{deviceInfo}'"); + + return new RefreshResult(newRefreshToken, accessToken); + } } } } - \ No newline at end of file + diff --git a/Govor.Application/Services/UserSessions/UserSessionReader.cs b/Govor.Application/Services/UserSessions/UserSessionReader.cs index cd93052..f9ba04c 100644 --- a/Govor.Application/Services/UserSessions/UserSessionReader.cs +++ b/Govor.Application/Services/UserSessions/UserSessionReader.cs @@ -1,5 +1,4 @@ using Govor.Application.Interfaces.UserSession; -using Govor.Core.Models; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data.Repositories.Exceptions; using Microsoft.Extensions.Logging; @@ -17,7 +16,7 @@ public class UserSessionReader : IUserSessionReader _logger = logger; } - public async Task> GetAllSessionsAsync(Guid userId) + public async Task> GetAllSessionsAsync(Guid userId) { try { diff --git a/Govor.Application/Services/UserSessions/UserSessionRefresher.cs b/Govor.Application/Services/UserSessions/UserSessionRefresher.cs index 1404404..b6a968f 100644 --- a/Govor.Application/Services/UserSessions/UserSessionRefresher.cs +++ b/Govor.Application/Services/UserSessions/UserSessionRefresher.cs @@ -1,7 +1,7 @@ using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.UserSession; using Govor.Application.Services.Authentication; -using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Core.Repositories.Users; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data.Repositories.Exceptions; diff --git a/Govor.Core/Models/UserSession.cs b/Govor.Core/Models/Users/UserSession.cs similarity index 96% rename from Govor.Core/Models/UserSession.cs rename to Govor.Core/Models/Users/UserSession.cs index ce6cdaa..62f8384 100644 --- a/Govor.Core/Models/UserSession.cs +++ b/Govor.Core/Models/Users/UserSession.cs @@ -1,4 +1,4 @@ -namespace Govor.Core.Models; +namespace Govor.Core.Models.Users; public class UserSession { diff --git a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsExist.cs b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsExist.cs index 38a16fd..9d4ff9e 100644 --- a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsExist.cs +++ b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsExist.cs @@ -1,4 +1,4 @@ -using Govor.Core.Models; +using Govor.Core.Models.Users; namespace Govor.Core.Repositories.UserSessionsRepository; diff --git a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsReader.cs b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsReader.cs index 6d4fc71..b86be83 100644 --- a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsReader.cs +++ b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsReader.cs @@ -1,4 +1,4 @@ -using Govor.Core.Models; +using Govor.Core.Models.Users; namespace Govor.Core.Repositories.UserSessionsRepository; diff --git a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsWriter.cs b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsWriter.cs index aa6eeaa..4f7eeac 100644 --- a/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsWriter.cs +++ b/Govor.Core/Repositories/UserSessionsRepository/IUserSessionsWriter.cs @@ -1,4 +1,5 @@ using Govor.Core.Models; +using Govor.Core.Models.Users; namespace Govor.Core.Repositories.UserSessionsRepository; diff --git a/Govor.Data.Tests/Repositories/UserSessionsRepositoryTests.cs b/Govor.Data.Tests/Repositories/UserSessionsRepositoryTests.cs index 37dd421..6871a63 100644 --- a/Govor.Data.Tests/Repositories/UserSessionsRepositoryTests.cs +++ b/Govor.Data.Tests/Repositories/UserSessionsRepositoryTests.cs @@ -1,5 +1,6 @@ using AutoFixture; using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Data.Repositories; using Govor.Data.Repositories.Exceptions; using Microsoft.EntityFrameworkCore; diff --git a/Govor.Data/Repositories/UserSessionsRepository.cs b/Govor.Data/Repositories/UserSessionsRepository.cs index c4ed8d1..37cb6eb 100644 --- a/Govor.Data/Repositories/UserSessionsRepository.cs +++ b/Govor.Data/Repositories/UserSessionsRepository.cs @@ -1,5 +1,5 @@ using Govor.Core.Infrastructure.Extensions; -using Govor.Core.Models; +using Govor.Core.Models.Users; using Govor.Core.Repositories.UserSessionsRepository; using Govor.Data.Repositories.Exceptions; using Microsoft.EntityFrameworkCore;