feat: Enhance dependency injection and service integration
- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"carrotskin/internal/model"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -427,7 +428,8 @@ func TestProfileServiceImpl_Create(t *testing.T) {
|
||||
}
|
||||
userRepo.Create(testUser)
|
||||
|
||||
profileService := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
profileService := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -472,7 +474,8 @@ func TestProfileServiceImpl_Create(t *testing.T) {
|
||||
tt.setupMocks()
|
||||
}
|
||||
|
||||
profile, err := profileService.Create(tt.userID, tt.profileName)
|
||||
ctx := context.Background()
|
||||
profile, err := profileService.Create(ctx, tt.userID, tt.profileName)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
@@ -515,7 +518,8 @@ func TestProfileServiceImpl_GetByUUID(t *testing.T) {
|
||||
}
|
||||
profileRepo.Create(testProfile)
|
||||
|
||||
profileService := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
profileService := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -536,7 +540,8 @@ func TestProfileServiceImpl_GetByUUID(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
profile, err := profileService.GetByUUID(tt.uuid)
|
||||
ctx := context.Background()
|
||||
profile, err := profileService.GetByUUID(ctx, tt.uuid)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
@@ -572,7 +577,8 @@ func TestProfileServiceImpl_Delete(t *testing.T) {
|
||||
}
|
||||
profileRepo.Create(testProfile)
|
||||
|
||||
profileService := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
profileService := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -596,7 +602,8 @@ func TestProfileServiceImpl_Delete(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := profileService.Delete(tt.uuid, tt.userID)
|
||||
ctx := context.Background()
|
||||
err := profileService.Delete(ctx, tt.uuid, tt.userID)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
@@ -622,9 +629,11 @@ func TestProfileServiceImpl_GetByUserID(t *testing.T) {
|
||||
profileRepo.Create(&model.Profile{UUID: "p2", UserID: 1, Name: "P2"})
|
||||
profileRepo.Create(&model.Profile{UUID: "p3", UserID: 2, Name: "P3"})
|
||||
|
||||
svc := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
svc := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
list, err := svc.GetByUserID(1)
|
||||
ctx := context.Background()
|
||||
list, err := svc.GetByUserID(ctx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByUserID 失败: %v", err)
|
||||
}
|
||||
@@ -646,13 +655,16 @@ func TestProfileServiceImpl_Update_And_SetActive(t *testing.T) {
|
||||
}
|
||||
profileRepo.Create(profile)
|
||||
|
||||
svc := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
svc := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 正常更新名称与皮肤/披风
|
||||
newName := "NewName"
|
||||
var skinID int64 = 10
|
||||
var capeID int64 = 20
|
||||
updated, err := svc.Update("u1", 1, &newName, &skinID, &capeID)
|
||||
updated, err := svc.Update(ctx, "u1", 1, &newName, &skinID, &capeID)
|
||||
if err != nil {
|
||||
t.Fatalf("Update 正常情况失败: %v", err)
|
||||
}
|
||||
@@ -661,7 +673,7 @@ func TestProfileServiceImpl_Update_And_SetActive(t *testing.T) {
|
||||
}
|
||||
|
||||
// 用户无权限
|
||||
if _, err := svc.Update("u1", 2, &newName, nil, nil); err == nil {
|
||||
if _, err := svc.Update(ctx, "u1", 2, &newName, nil, nil); err == nil {
|
||||
t.Fatalf("Update 在无权限时应返回错误")
|
||||
}
|
||||
|
||||
@@ -671,17 +683,17 @@ func TestProfileServiceImpl_Update_And_SetActive(t *testing.T) {
|
||||
UserID: 2,
|
||||
Name: "Duplicate",
|
||||
})
|
||||
if _, err := svc.Update("u1", 1, stringPtr("Duplicate"), nil, nil); err == nil {
|
||||
if _, err := svc.Update(ctx, "u1", 1, stringPtr("Duplicate"), nil, nil); err == nil {
|
||||
t.Fatalf("Update 在名称重复时应返回错误")
|
||||
}
|
||||
|
||||
// SetActive 正常
|
||||
if err := svc.SetActive("u1", 1); err != nil {
|
||||
if err := svc.SetActive(ctx, "u1", 1); err != nil {
|
||||
t.Fatalf("SetActive 正常情况失败: %v", err)
|
||||
}
|
||||
|
||||
// SetActive 无权限
|
||||
if err := svc.SetActive("u1", 2); err == nil {
|
||||
if err := svc.SetActive(ctx, "u1", 2); err == nil {
|
||||
t.Fatalf("SetActive 在无权限时应返回错误")
|
||||
}
|
||||
}
|
||||
@@ -696,20 +708,23 @@ func TestProfileServiceImpl_CheckLimit_And_GetByNames(t *testing.T) {
|
||||
profileRepo.Create(&model.Profile{UUID: "a", UserID: 1, Name: "A"})
|
||||
profileRepo.Create(&model.Profile{UUID: "b", UserID: 1, Name: "B"})
|
||||
|
||||
svc := NewProfileService(profileRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
svc := NewProfileService(profileRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// CheckLimit 未达上限
|
||||
if err := svc.CheckLimit(1, 3); err != nil {
|
||||
if err := svc.CheckLimit(ctx, 1, 3); err != nil {
|
||||
t.Fatalf("CheckLimit 未达到上限时不应报错: %v", err)
|
||||
}
|
||||
|
||||
// CheckLimit 达到上限
|
||||
if err := svc.CheckLimit(1, 2); err == nil {
|
||||
if err := svc.CheckLimit(ctx, 1, 2); err == nil {
|
||||
t.Fatalf("CheckLimit 达到上限时应报错")
|
||||
}
|
||||
|
||||
// GetByNames
|
||||
list, err := svc.GetByNames([]string{"A", "B"})
|
||||
list, err := svc.GetByNames(ctx, []string{"A", "B"})
|
||||
if err != nil {
|
||||
t.Fatalf("GetByNames 失败: %v", err)
|
||||
}
|
||||
@@ -718,7 +733,7 @@ func TestProfileServiceImpl_CheckLimit_And_GetByNames(t *testing.T) {
|
||||
}
|
||||
|
||||
// GetByProfileName 存在
|
||||
p, err := svc.GetByProfileName("A")
|
||||
p, err := svc.GetByProfileName(ctx, "A")
|
||||
if err != nil || p == nil || p.Name != "A" {
|
||||
t.Fatalf("GetByProfileName 返回错误, profile=%+v, err=%v", p, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user