refactor: Implement dependency injection for handlers and services
- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection. - Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection. - Updated the container to initialize service instances and provide them to handlers. - Enhanced code structure for better testability and adherence to Go best practices.
This commit is contained in:
@@ -6,18 +6,38 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CreateTexture 创建材质
|
||||
func CreateTexture(db *gorm.DB, uploaderID int64, name, description, textureType, url, hash string, size int, isPublic, isSlim bool) (*model.Texture, error) {
|
||||
// textureServiceImpl TextureService的实现
|
||||
type textureServiceImpl struct {
|
||||
textureRepo repository.TextureRepository
|
||||
userRepo repository.UserRepository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewTextureService 创建TextureService实例
|
||||
func NewTextureService(
|
||||
textureRepo repository.TextureRepository,
|
||||
userRepo repository.UserRepository,
|
||||
logger *zap.Logger,
|
||||
) TextureService {
|
||||
return &textureServiceImpl{
|
||||
textureRepo: textureRepo,
|
||||
userRepo: userRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *textureServiceImpl) Create(uploaderID int64, name, description, textureType, url, hash string, size int, isPublic, isSlim bool) (*model.Texture, error) {
|
||||
// 验证用户存在
|
||||
if _, err := EnsureUserExists(uploaderID); err != nil {
|
||||
return nil, err
|
||||
user, err := s.userRepo.FindByID(uploaderID)
|
||||
if err != nil || user == nil {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
|
||||
// 检查Hash是否已存在
|
||||
existingTexture, err := repository.FindTextureByHash(hash)
|
||||
existingTexture, err := s.textureRepo.FindByHash(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -26,7 +46,7 @@ func CreateTexture(db *gorm.DB, uploaderID int64, name, description, textureType
|
||||
}
|
||||
|
||||
// 转换材质类型
|
||||
textureTypeEnum, err := parseTextureType(textureType)
|
||||
textureTypeEnum, err := parseTextureTypeInternal(textureType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -47,36 +67,49 @@ func CreateTexture(db *gorm.DB, uploaderID int64, name, description, textureType
|
||||
FavoriteCount: 0,
|
||||
}
|
||||
|
||||
if err := repository.CreateTexture(texture); err != nil {
|
||||
if err := s.textureRepo.Create(texture); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return texture, nil
|
||||
}
|
||||
|
||||
// GetTextureByID 根据ID获取材质
|
||||
func GetTextureByID(db *gorm.DB, id int64) (*model.Texture, error) {
|
||||
return EnsureTextureExists(id)
|
||||
}
|
||||
|
||||
// GetUserTextures 获取用户上传的材质列表
|
||||
func GetUserTextures(db *gorm.DB, uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
return repository.FindTexturesByUploaderID(uploaderID, page, pageSize)
|
||||
}
|
||||
|
||||
// SearchTextures 搜索材质
|
||||
func SearchTextures(db *gorm.DB, keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
return repository.SearchTextures(keyword, textureType, publicOnly, page, pageSize)
|
||||
}
|
||||
|
||||
// UpdateTexture 更新材质
|
||||
func UpdateTexture(db *gorm.DB, textureID, uploaderID int64, name, description string, isPublic *bool) (*model.Texture, error) {
|
||||
// 获取材质并验证权限
|
||||
if _, err := GetTextureWithPermissionCheck(textureID, uploaderID); err != nil {
|
||||
func (s *textureServiceImpl) GetByID(id int64) (*model.Texture, error) {
|
||||
texture, err := s.textureRepo.FindByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if texture == nil {
|
||||
return nil, ErrTextureNotFound
|
||||
}
|
||||
if texture.Status == -1 {
|
||||
return nil, errors.New("材质已删除")
|
||||
}
|
||||
return texture, nil
|
||||
}
|
||||
|
||||
func (s *textureServiceImpl) GetByUserID(uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
return s.textureRepo.FindByUploaderID(uploaderID, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *textureServiceImpl) Search(keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
return s.textureRepo.Search(keyword, textureType, publicOnly, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *textureServiceImpl) Update(textureID, uploaderID int64, name, description string, isPublic *bool) (*model.Texture, error) {
|
||||
// 获取材质并验证权限
|
||||
texture, err := s.textureRepo.FindByID(textureID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if texture == nil {
|
||||
return nil, ErrTextureNotFound
|
||||
}
|
||||
if texture.UploaderID != uploaderID {
|
||||
return nil, ErrTextureNoPermission
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
updates := make(map[string]interface{})
|
||||
@@ -91,83 +124,73 @@ func UpdateTexture(db *gorm.DB, textureID, uploaderID int64, name, description s
|
||||
}
|
||||
|
||||
if len(updates) > 0 {
|
||||
if err := repository.UpdateTextureFields(textureID, updates); err != nil {
|
||||
if err := s.textureRepo.UpdateFields(textureID, updates); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return repository.FindTextureByID(textureID)
|
||||
return s.textureRepo.FindByID(textureID)
|
||||
}
|
||||
|
||||
// DeleteTexture 删除材质
|
||||
func DeleteTexture(db *gorm.DB, textureID, uploaderID int64) error {
|
||||
if _, err := GetTextureWithPermissionCheck(textureID, uploaderID); err != nil {
|
||||
func (s *textureServiceImpl) Delete(textureID, uploaderID int64) error {
|
||||
// 获取材质并验证权限
|
||||
texture, err := s.textureRepo.FindByID(textureID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return repository.DeleteTexture(textureID)
|
||||
if texture == nil {
|
||||
return ErrTextureNotFound
|
||||
}
|
||||
if texture.UploaderID != uploaderID {
|
||||
return ErrTextureNoPermission
|
||||
}
|
||||
|
||||
return s.textureRepo.Delete(textureID)
|
||||
}
|
||||
|
||||
// RecordTextureDownload 记录下载
|
||||
func RecordTextureDownload(db *gorm.DB, textureID int64, userID *int64, ipAddress, userAgent string) error {
|
||||
if _, err := EnsureTextureExists(textureID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := repository.IncrementTextureDownloadCount(textureID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log := &model.TextureDownloadLog{
|
||||
TextureID: textureID,
|
||||
UserID: userID,
|
||||
IPAddress: ipAddress,
|
||||
UserAgent: userAgent,
|
||||
}
|
||||
|
||||
return repository.CreateTextureDownloadLog(log)
|
||||
}
|
||||
|
||||
// ToggleTextureFavorite 切换收藏状态
|
||||
func ToggleTextureFavorite(db *gorm.DB, userID, textureID int64) (bool, error) {
|
||||
if _, err := EnsureTextureExists(textureID); err != nil {
|
||||
func (s *textureServiceImpl) ToggleFavorite(userID, textureID int64) (bool, error) {
|
||||
// 确保材质存在
|
||||
texture, err := s.textureRepo.FindByID(textureID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if texture == nil {
|
||||
return false, ErrTextureNotFound
|
||||
}
|
||||
|
||||
isFavorited, err := repository.IsTextureFavorited(userID, textureID)
|
||||
isFavorited, err := s.textureRepo.IsFavorited(userID, textureID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if isFavorited {
|
||||
// 已收藏 -> 取消收藏
|
||||
if err := repository.RemoveTextureFavorite(userID, textureID); err != nil {
|
||||
if err := s.textureRepo.RemoveFavorite(userID, textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := repository.DecrementTextureFavoriteCount(textureID); err != nil {
|
||||
if err := s.textureRepo.DecrementFavoriteCount(textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
} else {
|
||||
// 未收藏 -> 添加收藏
|
||||
if err := repository.AddTextureFavorite(userID, textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := repository.IncrementTextureFavoriteCount(textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 未收藏 -> 添加收藏
|
||||
if err := s.textureRepo.AddFavorite(userID, textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := s.textureRepo.IncrementFavoriteCount(textureID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetUserTextureFavorites 获取用户收藏的材质列表
|
||||
func GetUserTextureFavorites(db *gorm.DB, userID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
func (s *textureServiceImpl) GetUserFavorites(userID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
page, pageSize = NormalizePagination(page, pageSize)
|
||||
return repository.GetUserTextureFavorites(userID, page, pageSize)
|
||||
return s.textureRepo.GetUserFavorites(userID, page, pageSize)
|
||||
}
|
||||
|
||||
// CheckTextureUploadLimit 检查用户上传材质数量限制
|
||||
func CheckTextureUploadLimit(db *gorm.DB, uploaderID int64, maxTextures int) error {
|
||||
count, err := repository.CountTexturesByUploaderID(uploaderID)
|
||||
func (s *textureServiceImpl) CheckUploadLimit(uploaderID int64, maxTextures int) error {
|
||||
count, err := s.textureRepo.CountByUploaderID(uploaderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -179,8 +202,8 @@ func CheckTextureUploadLimit(db *gorm.DB, uploaderID int64, maxTextures int) err
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseTextureType 解析材质类型
|
||||
func parseTextureType(textureType string) (model.TextureType, error) {
|
||||
// parseTextureTypeInternal 解析材质类型
|
||||
func parseTextureTypeInternal(textureType string) (model.TextureType, error) {
|
||||
switch textureType {
|
||||
case "SKIN":
|
||||
return model.TextureTypeSkin, nil
|
||||
|
||||
Reference in New Issue
Block a user