refactor: Update service and repository methods to use context

- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles.
- Updated handlers to utilize context in method calls, improving error handling and performance.
- Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -208,7 +208,7 @@ func TestTokenServiceImpl_Create(t *testing.T) {
Name: "TestProfile",
IsActive: true,
}
profileRepo.Create(testProfile)
_ = profileRepo.Create(context.Background(), testProfile)
tokenService := NewTokenService(tokenRepo, profileRepo, logger)
@@ -274,7 +274,7 @@ func TestTokenServiceImpl_Validate(t *testing.T) {
ProfileId: "test-profile-uuid",
Usable: true,
}
tokenRepo.Create(testToken)
_ = tokenRepo.Create(context.Background(), testToken)
tokenService := NewTokenService(tokenRepo, profileRepo, logger)
@@ -336,7 +336,7 @@ func TestTokenServiceImpl_Invalidate(t *testing.T) {
ProfileId: "test-profile-uuid",
Usable: true,
}
tokenRepo.Create(testToken)
_ = tokenRepo.Create(context.Background(), testToken)
tokenService := NewTokenService(tokenRepo, profileRepo, logger)
@@ -352,7 +352,7 @@ func TestTokenServiceImpl_Invalidate(t *testing.T) {
tokenService.Invalidate(ctx, "token-to-invalidate")
// 验证Token已失效从repo中删除
_, err := tokenRepo.FindByAccessToken("token-to-invalidate")
_, err := tokenRepo.FindByAccessToken(context.Background(), "token-to-invalidate")
if err == nil {
t.Error("Token应该已被删除")
}
@@ -366,7 +366,7 @@ func TestTokenServiceImpl_InvalidateUserTokens(t *testing.T) {
// 预置多个Token
for i := 1; i <= 3; i++ {
tokenRepo.Create(&model.Token{
_ = tokenRepo.Create(context.Background(), &model.Token{
AccessToken: fmt.Sprintf("user1-token-%d", i),
ClientToken: "client-token",
UserID: 1,
@@ -374,7 +374,7 @@ func TestTokenServiceImpl_InvalidateUserTokens(t *testing.T) {
Usable: true,
})
}
tokenRepo.Create(&model.Token{
_ = tokenRepo.Create(context.Background(), &model.Token{
AccessToken: "user2-token-1",
ClientToken: "client-token",
UserID: 2,
@@ -390,13 +390,13 @@ func TestTokenServiceImpl_InvalidateUserTokens(t *testing.T) {
tokenService.InvalidateUserTokens(ctx, 1)
// 验证用户1的Token已失效
tokens, _ := tokenRepo.GetByUserID(1)
tokens, _ := tokenRepo.GetByUserID(context.Background(), 1)
if len(tokens) > 0 {
t.Errorf("用户1的Token应该全部被删除但还剩 %d 个", len(tokens))
}
// 验证用户2的Token仍然存在
tokens2, _ := tokenRepo.GetByUserID(2)
tokens2, _ := tokenRepo.GetByUserID(context.Background(), 2)
if len(tokens2) != 1 {
t.Errorf("用户2的Token应该仍然存在期望1个实际 %d 个", len(tokens2))
}
@@ -413,7 +413,7 @@ func TestTokenServiceImpl_Refresh(t *testing.T) {
UUID: "profile-uuid",
UserID: 1,
}
profileRepo.Create(profile)
_ = profileRepo.Create(context.Background(), profile)
oldToken := &model.Token{
AccessToken: "old-token",
@@ -422,7 +422,7 @@ func TestTokenServiceImpl_Refresh(t *testing.T) {
ProfileId: "",
Usable: true,
}
tokenRepo.Create(oldToken)
_ = tokenRepo.Create(context.Background(), oldToken)
tokenService := NewTokenService(tokenRepo, profileRepo, logger)
@@ -455,7 +455,7 @@ func TestTokenServiceImpl_GetByAccessToken(t *testing.T) {
ProfileId: "profile-42",
Usable: true,
}
tokenRepo.Create(token)
_ = tokenRepo.Create(context.Background(), token)
tokenService := NewTokenService(tokenRepo, profileRepo, logger)
@@ -489,25 +489,25 @@ func TestTokenServiceImpl_validateProfileByUserID(t *testing.T) {
UUID: "p-1",
UserID: 1,
}
profileRepo.Create(profile)
_ = profileRepo.Create(context.Background(), profile)
// 参数非法
if ok, err := svc.validateProfileByUserID(0, ""); err == nil || ok {
if ok, err := svc.validateProfileByUserID(context.Background(), 0, ""); err == nil || ok {
t.Fatalf("validateProfileByUserID 在参数非法时应返回错误")
}
// Profile 不存在
if ok, err := svc.validateProfileByUserID(1, "not-exists"); err == nil || ok {
if ok, err := svc.validateProfileByUserID(context.Background(), 1, "not-exists"); err == nil || ok {
t.Fatalf("validateProfileByUserID 在 Profile 不存在时应返回错误")
}
// 用户与 Profile 匹配
if ok, err := svc.validateProfileByUserID(1, "p-1"); err != nil || !ok {
if ok, err := svc.validateProfileByUserID(context.Background(), 1, "p-1"); err != nil || !ok {
t.Fatalf("validateProfileByUserID 匹配时应返回 true, err=%v", err)
}
// 用户与 Profile 不匹配
if ok, err := svc.validateProfileByUserID(2, "p-1"); err != nil || ok {
if ok, err := svc.validateProfileByUserID(context.Background(), 2, "p-1"); err != nil || ok {
t.Fatalf("validateProfileByUserID 不匹配时应返回 false, err=%v", err)
}
}