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"
|
||||
@@ -492,7 +493,8 @@ func TestTextureServiceImpl_Create(t *testing.T) {
|
||||
}
|
||||
userRepo.Create(testUser)
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -561,7 +563,9 @@ func TestTextureServiceImpl_Create(t *testing.T) {
|
||||
tt.setupMocks()
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
texture, err := textureService.Create(
|
||||
ctx,
|
||||
tt.uploaderID,
|
||||
tt.textureName,
|
||||
"Test description",
|
||||
@@ -612,7 +616,8 @@ func TestTextureServiceImpl_GetByID(t *testing.T) {
|
||||
}
|
||||
textureRepo.Create(testTexture)
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -633,7 +638,8 @@ func TestTextureServiceImpl_GetByID(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
texture, err := textureService.GetByID(tt.id)
|
||||
ctx := context.Background()
|
||||
texture, err := textureService.GetByID(ctx, tt.id)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
@@ -668,10 +674,13 @@ func TestTextureServiceImpl_GetByUserID_And_Search(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// GetByUserID 应按上传者过滤并调用 NormalizePagination
|
||||
textures, total, err := textureService.GetByUserID(1, 0, 0)
|
||||
textures, total, err := textureService.GetByUserID(ctx, 1, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByUserID 失败: %v", err)
|
||||
}
|
||||
@@ -680,7 +689,7 @@ func TestTextureServiceImpl_GetByUserID_And_Search(t *testing.T) {
|
||||
}
|
||||
|
||||
// Search 仅验证能够正常调用并返回结果
|
||||
searchResult, searchTotal, err := textureService.Search("", "", true, -1, 200)
|
||||
searchResult, searchTotal, err := textureService.Search(ctx, "", model.TextureTypeSkin, true, -1, 200)
|
||||
if err != nil {
|
||||
t.Fatalf("Search 失败: %v", err)
|
||||
}
|
||||
@@ -696,21 +705,24 @@ func TestTextureServiceImpl_Update_And_Delete(t *testing.T) {
|
||||
logger := zap.NewNop()
|
||||
|
||||
texture := &model.Texture{
|
||||
ID: 1,
|
||||
UploaderID: 1,
|
||||
Name: "Old",
|
||||
Description:"OldDesc",
|
||||
IsPublic: false,
|
||||
ID: 1,
|
||||
UploaderID: 1,
|
||||
Name: "Old",
|
||||
Description: "OldDesc",
|
||||
IsPublic: false,
|
||||
}
|
||||
textureRepo.Create(texture)
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 更新成功
|
||||
newName := "NewName"
|
||||
newDesc := "NewDesc"
|
||||
public := boolPtr(true)
|
||||
updated, err := textureService.Update(1, 1, newName, newDesc, public)
|
||||
updated, err := textureService.Update(ctx, 1, 1, newName, newDesc, public)
|
||||
if err != nil {
|
||||
t.Fatalf("Update 正常情况失败: %v", err)
|
||||
}
|
||||
@@ -720,17 +732,17 @@ func TestTextureServiceImpl_Update_And_Delete(t *testing.T) {
|
||||
}
|
||||
|
||||
// 无权限更新
|
||||
if _, err := textureService.Update(1, 2, "X", "Y", nil); err == nil {
|
||||
if _, err := textureService.Update(ctx, 1, 2, "X", "Y", nil); err == nil {
|
||||
t.Fatalf("Update 在无权限时应返回错误")
|
||||
}
|
||||
|
||||
// 删除成功
|
||||
if err := textureService.Delete(1, 1); err != nil {
|
||||
if err := textureService.Delete(ctx, 1, 1); err != nil {
|
||||
t.Fatalf("Delete 正常情况失败: %v", err)
|
||||
}
|
||||
|
||||
// 无权限删除
|
||||
if err := textureService.Delete(1, 2); err == nil {
|
||||
if err := textureService.Delete(ctx, 1, 2); err == nil {
|
||||
t.Fatalf("Delete 在无权限时应返回错误")
|
||||
}
|
||||
}
|
||||
@@ -751,10 +763,13 @@ func TestTextureServiceImpl_FavoritesAndLimit(t *testing.T) {
|
||||
_ = textureRepo.AddFavorite(1, i)
|
||||
}
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// GetUserFavorites
|
||||
favs, total, err := textureService.GetUserFavorites(1, -1, -1)
|
||||
favs, total, err := textureService.GetUserFavorites(ctx, 1, -1, -1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetUserFavorites 失败: %v", err)
|
||||
}
|
||||
@@ -763,12 +778,12 @@ func TestTextureServiceImpl_FavoritesAndLimit(t *testing.T) {
|
||||
}
|
||||
|
||||
// CheckUploadLimit 未超过上限
|
||||
if err := textureService.CheckUploadLimit(1, 10); err != nil {
|
||||
if err := textureService.CheckUploadLimit(ctx, 1, 10); err != nil {
|
||||
t.Fatalf("CheckUploadLimit 在未达到上限时不应报错: %v", err)
|
||||
}
|
||||
|
||||
// CheckUploadLimit 超过上限
|
||||
if err := textureService.CheckUploadLimit(1, 2); err == nil {
|
||||
if err := textureService.CheckUploadLimit(ctx, 1, 2); err == nil {
|
||||
t.Fatalf("CheckUploadLimit 在超过上限时应返回错误")
|
||||
}
|
||||
}
|
||||
@@ -791,10 +806,13 @@ func TestTextureServiceImpl_ToggleFavorite(t *testing.T) {
|
||||
}
|
||||
textureRepo.Create(testTexture)
|
||||
|
||||
textureService := NewTextureService(textureRepo, userRepo, logger)
|
||||
cacheManager := NewMockCacheManager()
|
||||
textureService := NewTextureService(textureRepo, userRepo, cacheManager, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 第一次收藏
|
||||
isFavorited, err := textureService.ToggleFavorite(1, 1)
|
||||
isFavorited, err := textureService.ToggleFavorite(ctx, 1, 1)
|
||||
if err != nil {
|
||||
t.Errorf("第一次收藏失败: %v", err)
|
||||
}
|
||||
@@ -803,7 +821,7 @@ func TestTextureServiceImpl_ToggleFavorite(t *testing.T) {
|
||||
}
|
||||
|
||||
// 第二次取消收藏
|
||||
isFavorited, err = textureService.ToggleFavorite(1, 1)
|
||||
isFavorited, err = textureService.ToggleFavorite(ctx, 1, 1)
|
||||
if err != nil {
|
||||
t.Errorf("取消收藏失败: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user