From 5ef4ed92df5dc31fba1114f0e1d8bcc2791ad80e Mon Sep 17 00:00:00 2001 From: Marcin Bator Date: Sat, 23 Nov 2024 11:31:26 +0100 Subject: [PATCH] fix: #dev async fix 3 --- rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs | 1 - .../Database/Entity/AccountConfirmationToken.cs | 3 ++- rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs | 7 +++++-- .../Infrastructure/Database/Entity/PasswordResetToken.cs | 3 ++- .../Infrastructure/Database/Entity/RefreshToken.cs | 3 ++- rag-2-backend/Infrastructure/Database/Entity/User.cs | 4 +++- rag-2-backend/Infrastructure/Module/Auth/AuthService.cs | 1 - rag-2-backend/Infrastructure/Module/User/UserService.cs | 2 -- rag-2-backend/Program.cs | 2 +- 9 files changed, 15 insertions(+), 11 deletions(-) diff --git a/rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs b/rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs index 7029c1b..47a911d 100644 --- a/rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs +++ b/rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs @@ -12,7 +12,6 @@ public class RefreshTokenDao(DatabaseContext context) { public virtual async Task RemoveTokensForUser(User user) { - context.Attach(user); var unusedTokens = await context.RefreshTokens.Where(r => r.User.Id == user.Id).ToListAsync(); context.RefreshTokens.RemoveRange(unusedTokens); await context.SaveChangesAsync(); diff --git a/rag-2-backend/Infrastructure/Database/Entity/AccountConfirmationToken.cs b/rag-2-backend/Infrastructure/Database/Entity/AccountConfirmationToken.cs index d733b76..90aa6cf 100644 --- a/rag-2-backend/Infrastructure/Database/Entity/AccountConfirmationToken.cs +++ b/rag-2-backend/Infrastructure/Database/Entity/AccountConfirmationToken.cs @@ -12,5 +12,6 @@ public class AccountConfirmationToken { [Key] [MaxLength(100)] public required string Token { get; init; } public required DateTime Expiration { get; set; } - public required User User { get; init; } + + [ForeignKey("UserId")] public required User User { get; init; } } \ No newline at end of file diff --git a/rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs b/rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs index 58ffab7..92dfe1e 100644 --- a/rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs +++ b/rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs @@ -12,8 +12,11 @@ namespace rag_2_backend.Infrastructure.Database.Entity; public class GameRecord { [Key] public int Id { get; init; } - public required Game Game { get; init; } - public required User User { get; init; } + + [ForeignKey("GameId")] public required Game Game { get; init; } + + [ForeignKey("UserId")] public required User User { get; init; } + public required List Values { get; init; } public List? Players { get; init; } public DateTime Started { get; set; } diff --git a/rag-2-backend/Infrastructure/Database/Entity/PasswordResetToken.cs b/rag-2-backend/Infrastructure/Database/Entity/PasswordResetToken.cs index 5985081..f3b824b 100644 --- a/rag-2-backend/Infrastructure/Database/Entity/PasswordResetToken.cs +++ b/rag-2-backend/Infrastructure/Database/Entity/PasswordResetToken.cs @@ -12,5 +12,6 @@ public class PasswordResetToken { [Key] [MaxLength(100)] public required string Token { get; init; } public required DateTime Expiration { get; set; } - public required User User { get; init; } + + [ForeignKey("UserId")] public required User User { get; init; } } \ No newline at end of file diff --git a/rag-2-backend/Infrastructure/Database/Entity/RefreshToken.cs b/rag-2-backend/Infrastructure/Database/Entity/RefreshToken.cs index 652c180..4696e13 100644 --- a/rag-2-backend/Infrastructure/Database/Entity/RefreshToken.cs +++ b/rag-2-backend/Infrastructure/Database/Entity/RefreshToken.cs @@ -12,5 +12,6 @@ public class RefreshToken { [Key] [MaxLength(100)] public required string Token { get; init; } public required DateTime Expiration { get; init; } - public required User User { get; init; } + + [ForeignKey("UserId")] public required User User { get; init; } } \ No newline at end of file diff --git a/rag-2-backend/Infrastructure/Database/Entity/User.cs b/rag-2-backend/Infrastructure/Database/Entity/User.cs index a48a531..2e53d3b 100644 --- a/rag-2-backend/Infrastructure/Database/Entity/User.cs +++ b/rag-2-backend/Infrastructure/Database/Entity/User.cs @@ -47,6 +47,8 @@ public User(string email) public int StudyCycleYearB { get; set; } public bool Banned { get; set; } public DateTime LastPlayed { get; set; } - public Course? Course { get; set; } + + [ForeignKey("CourseId")] public Course? Course { get; set; } + [MaxLength(100)] public string? Group { get; set; } } \ No newline at end of file diff --git a/rag-2-backend/Infrastructure/Module/Auth/AuthService.cs b/rag-2-backend/Infrastructure/Module/Auth/AuthService.cs index 5cf355b..3234ff0 100644 --- a/rag-2-backend/Infrastructure/Module/Auth/AuthService.cs +++ b/rag-2-backend/Infrastructure/Module/Auth/AuthService.cs @@ -67,7 +67,6 @@ public async Task LogoutUser(string token) private async Task GenerateRefreshToken(double refreshTokenExpirationTimeDays, Database.Entity.User user) { - databaseContext.Attach(user); var refreshToken = new RefreshToken { User = user, diff --git a/rag-2-backend/Infrastructure/Module/User/UserService.cs b/rag-2-backend/Infrastructure/Module/User/UserService.cs index d791f12..5eb7f88 100644 --- a/rag-2-backend/Infrastructure/Module/User/UserService.cs +++ b/rag-2-backend/Infrastructure/Module/User/UserService.cs @@ -156,8 +156,6 @@ private async Task UpdateUserProperties( int? studyCycleYearA, int? studyCycleYearB, int? courseId, string? group, Database.Entity.User user ) { - context.Attach(user); - if (user.Role == Role.Student && ( !studyCycleYearA.HasValue || !studyCycleYearB.HasValue || !courseId.HasValue || string.IsNullOrWhiteSpace(group)) diff --git a/rag-2-backend/Program.cs b/rag-2-backend/Program.cs index f78c2e2..bacb7fa 100644 --- a/rag-2-backend/Program.cs +++ b/rag-2-backend/Program.cs @@ -18,7 +18,7 @@ { options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"), b => { b.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); }); -}, ServiceLifetime.Transient); +}); builder.Services.AddSingleton( ConnectionMultiplexer.Connect(builder.Configuration.GetSection("Redis:ConnectionString").Value ?? "") );