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

@@ -49,9 +49,10 @@ func TestUserServiceImpl_Register(t *testing.T) {
email: "new@example.com",
avatar: "",
wantErr: true,
errMsg: "用户已存在",
// 服务实现现已统一使用 apperrors.ErrUserAlreadyExists错误信息为“用户已存在
errMsg: "用户已存在",
setupMocks: func() {
userRepo.Create(&model.User{
_ = userRepo.Create(context.Background(), &model.User{
Username: "existinguser",
Email: "old@example.com",
})
@@ -66,7 +67,7 @@ func TestUserServiceImpl_Register(t *testing.T) {
wantErr: true,
errMsg: "邮箱已被注册",
setupMocks: func() {
userRepo.Create(&model.User{
_ = userRepo.Create(context.Background(), &model.User{
Username: "otheruser",
Email: "existing@example.com",
})
@@ -126,7 +127,7 @@ func TestUserServiceImpl_Login(t *testing.T) {
Password: hashedPassword,
Status: 1,
}
userRepo.Create(testUser)
_ = userRepo.Create(context.Background(), testUser)
cacheManager := NewMockCacheManager()
userService := NewUserService(userRepo, configRepo, jwtService, nil, cacheManager, logger)
@@ -207,7 +208,7 @@ func TestUserServiceImpl_BasicGettersAndUpdates(t *testing.T) {
Email: "basic@example.com",
Avatar: "",
}
userRepo.Create(user)
_ = userRepo.Create(context.Background(), user)
cacheManager := NewMockCacheManager()
userService := NewUserService(userRepo, configRepo, jwtService, nil, cacheManager, logger)
@@ -231,7 +232,7 @@ func TestUserServiceImpl_BasicGettersAndUpdates(t *testing.T) {
if err := userService.UpdateInfo(ctx, user); err != nil {
t.Fatalf("UpdateInfo 失败: %v", err)
}
updated, _ := userRepo.FindByID(1)
updated, _ := userRepo.FindByID(context.Background(), 1)
if updated.Username != "updated" {
t.Fatalf("UpdateInfo 未更新用户名, got=%s", updated.Username)
}
@@ -255,7 +256,7 @@ func TestUserServiceImpl_ChangePassword(t *testing.T) {
Username: "changepw",
Password: hashed,
}
userRepo.Create(user)
_ = userRepo.Create(context.Background(), user)
cacheManager := NewMockCacheManager()
userService := NewUserService(userRepo, configRepo, jwtService, nil, cacheManager, logger)
@@ -290,7 +291,7 @@ func TestUserServiceImpl_ResetPassword(t *testing.T) {
Username: "resetpw",
Email: "reset@example.com",
}
userRepo.Create(user)
_ = userRepo.Create(context.Background(), user)
cacheManager := NewMockCacheManager()
userService := NewUserService(userRepo, configRepo, jwtService, nil, cacheManager, logger)
@@ -317,8 +318,8 @@ func TestUserServiceImpl_ChangeEmail(t *testing.T) {
user1 := &model.User{ID: 1, Email: "user1@example.com"}
user2 := &model.User{ID: 2, Email: "user2@example.com"}
userRepo.Create(user1)
userRepo.Create(user2)
_ = userRepo.Create(context.Background(), user1)
_ = userRepo.Create(context.Background(), user2)
cacheManager := NewMockCacheManager()
userService := NewUserService(userRepo, configRepo, jwtService, nil, cacheManager, logger)
@@ -389,8 +390,8 @@ func TestUserServiceImpl_MaxLimits(t *testing.T) {
}
// 配置有效值
configRepo.Update(&model.SystemConfig{Key: "max_profiles_per_user", Value: "10"})
configRepo.Update(&model.SystemConfig{Key: "max_textures_per_user", Value: "100"})
_ = configRepo.Update(context.Background(), &model.SystemConfig{Key: "max_profiles_per_user", Value: "10"})
_ = configRepo.Update(context.Background(), &model.SystemConfig{Key: "max_textures_per_user", Value: "100"})
if got := userService.GetMaxProfilesPerUser(); got != 10 {
t.Fatalf("GetMaxProfilesPerUser 配置值错误, got=%d", got)