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:
@@ -6,32 +6,37 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateTexture 创建材质
|
||||
func CreateTexture(texture *model.Texture) error {
|
||||
return getDB().Create(texture).Error
|
||||
// textureRepository TextureRepository的实现
|
||||
type textureRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// FindTextureByID 根据ID查找材质
|
||||
func FindTextureByID(id int64) (*model.Texture, error) {
|
||||
// NewTextureRepository 创建TextureRepository实例
|
||||
func NewTextureRepository(db *gorm.DB) TextureRepository {
|
||||
return &textureRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *textureRepository) Create(texture *model.Texture) error {
|
||||
return r.db.Create(texture).Error
|
||||
}
|
||||
|
||||
func (r *textureRepository) FindByID(id int64) (*model.Texture, error) {
|
||||
var texture model.Texture
|
||||
err := getDB().Preload("Uploader").First(&texture, id).Error
|
||||
return HandleNotFound(&texture, err)
|
||||
err := r.db.Preload("Uploader").First(&texture, id).Error
|
||||
return handleNotFoundResult(&texture, err)
|
||||
}
|
||||
|
||||
// FindTextureByHash 根据Hash查找材质
|
||||
func FindTextureByHash(hash string) (*model.Texture, error) {
|
||||
func (r *textureRepository) FindByHash(hash string) (*model.Texture, error) {
|
||||
var texture model.Texture
|
||||
err := getDB().Where("hash = ?", hash).First(&texture).Error
|
||||
return HandleNotFound(&texture, err)
|
||||
err := r.db.Where("hash = ?", hash).First(&texture).Error
|
||||
return handleNotFoundResult(&texture, err)
|
||||
}
|
||||
|
||||
// FindTexturesByUploaderID 根据上传者ID查找材质列表
|
||||
func FindTexturesByUploaderID(uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
db := getDB()
|
||||
func (r *textureRepository) FindByUploaderID(uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
var textures []*model.Texture
|
||||
var total int64
|
||||
|
||||
query := db.Model(&model.Texture{}).Where("uploader_id = ? AND status != -1", uploaderID)
|
||||
query := r.db.Model(&model.Texture{}).Where("uploader_id = ? AND status != -1", uploaderID)
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -49,13 +54,11 @@ func FindTexturesByUploaderID(uploaderID int64, page, pageSize int) ([]*model.Te
|
||||
return textures, total, nil
|
||||
}
|
||||
|
||||
// SearchTextures 搜索材质
|
||||
func SearchTextures(keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
db := getDB()
|
||||
func (r *textureRepository) Search(keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
var textures []*model.Texture
|
||||
var total int64
|
||||
|
||||
query := db.Model(&model.Texture{}).Where("status = 1")
|
||||
query := r.db.Model(&model.Texture{}).Where("status = 1")
|
||||
|
||||
if publicOnly {
|
||||
query = query.Where("is_public = ?", true)
|
||||
@@ -83,79 +86,67 @@ func SearchTextures(keyword string, textureType model.TextureType, publicOnly bo
|
||||
return textures, total, nil
|
||||
}
|
||||
|
||||
// UpdateTexture 更新材质
|
||||
func UpdateTexture(texture *model.Texture) error {
|
||||
return getDB().Save(texture).Error
|
||||
func (r *textureRepository) Update(texture *model.Texture) error {
|
||||
return r.db.Save(texture).Error
|
||||
}
|
||||
|
||||
// UpdateTextureFields 更新材质指定字段
|
||||
func UpdateTextureFields(id int64, fields map[string]interface{}) error {
|
||||
return getDB().Model(&model.Texture{}).Where("id = ?", id).Updates(fields).Error
|
||||
func (r *textureRepository) UpdateFields(id int64, fields map[string]interface{}) error {
|
||||
return r.db.Model(&model.Texture{}).Where("id = ?", id).Updates(fields).Error
|
||||
}
|
||||
|
||||
// DeleteTexture 删除材质(软删除)
|
||||
func DeleteTexture(id int64) error {
|
||||
return getDB().Model(&model.Texture{}).Where("id = ?", id).Update("status", -1).Error
|
||||
func (r *textureRepository) Delete(id int64) error {
|
||||
return r.db.Model(&model.Texture{}).Where("id = ?", id).Update("status", -1).Error
|
||||
}
|
||||
|
||||
// IncrementTextureDownloadCount 增加下载次数
|
||||
func IncrementTextureDownloadCount(id int64) error {
|
||||
return getDB().Model(&model.Texture{}).Where("id = ?", id).
|
||||
func (r *textureRepository) IncrementDownloadCount(id int64) error {
|
||||
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
||||
UpdateColumn("download_count", gorm.Expr("download_count + ?", 1)).Error
|
||||
}
|
||||
|
||||
// IncrementTextureFavoriteCount 增加收藏次数
|
||||
func IncrementTextureFavoriteCount(id int64) error {
|
||||
return getDB().Model(&model.Texture{}).Where("id = ?", id).
|
||||
func (r *textureRepository) IncrementFavoriteCount(id int64) error {
|
||||
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
||||
UpdateColumn("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error
|
||||
}
|
||||
|
||||
// DecrementTextureFavoriteCount 减少收藏次数
|
||||
func DecrementTextureFavoriteCount(id int64) error {
|
||||
return getDB().Model(&model.Texture{}).Where("id = ?", id).
|
||||
func (r *textureRepository) DecrementFavoriteCount(id int64) error {
|
||||
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
||||
UpdateColumn("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error
|
||||
}
|
||||
|
||||
// CreateTextureDownloadLog 创建下载日志
|
||||
func CreateTextureDownloadLog(log *model.TextureDownloadLog) error {
|
||||
return getDB().Create(log).Error
|
||||
func (r *textureRepository) CreateDownloadLog(log *model.TextureDownloadLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
// IsTextureFavorited 检查是否已收藏
|
||||
func IsTextureFavorited(userID, textureID int64) (bool, error) {
|
||||
func (r *textureRepository) IsFavorited(userID, textureID int64) (bool, error) {
|
||||
var count int64
|
||||
err := getDB().Model(&model.UserTextureFavorite{}).
|
||||
err := r.db.Model(&model.UserTextureFavorite{}).
|
||||
Where("user_id = ? AND texture_id = ?", userID, textureID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// AddTextureFavorite 添加收藏
|
||||
func AddTextureFavorite(userID, textureID int64) error {
|
||||
func (r *textureRepository) AddFavorite(userID, textureID int64) error {
|
||||
favorite := &model.UserTextureFavorite{
|
||||
UserID: userID,
|
||||
TextureID: textureID,
|
||||
}
|
||||
return getDB().Create(favorite).Error
|
||||
return r.db.Create(favorite).Error
|
||||
}
|
||||
|
||||
// RemoveTextureFavorite 取消收藏
|
||||
func RemoveTextureFavorite(userID, textureID int64) error {
|
||||
return getDB().Where("user_id = ? AND texture_id = ?", userID, textureID).
|
||||
func (r *textureRepository) RemoveFavorite(userID, textureID int64) error {
|
||||
return r.db.Where("user_id = ? AND texture_id = ?", userID, textureID).
|
||||
Delete(&model.UserTextureFavorite{}).Error
|
||||
}
|
||||
|
||||
// GetUserTextureFavorites 获取用户收藏的材质列表
|
||||
func GetUserTextureFavorites(userID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
db := getDB()
|
||||
func (r *textureRepository) GetUserFavorites(userID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
||||
var textures []*model.Texture
|
||||
var total int64
|
||||
|
||||
subQuery := db.Model(&model.UserTextureFavorite{}).
|
||||
subQuery := r.db.Model(&model.UserTextureFavorite{}).
|
||||
Select("texture_id").
|
||||
Where("user_id = ?", userID)
|
||||
|
||||
query := db.Model(&model.Texture{}).
|
||||
query := r.db.Model(&model.Texture{}).
|
||||
Where("id IN (?) AND status = 1", subQuery)
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -174,10 +165,9 @@ func GetUserTextureFavorites(userID int64, page, pageSize int) ([]*model.Texture
|
||||
return textures, total, nil
|
||||
}
|
||||
|
||||
// CountTexturesByUploaderID 统计用户上传的材质数量
|
||||
func CountTexturesByUploaderID(uploaderID int64) (int64, error) {
|
||||
func (r *textureRepository) CountByUploaderID(uploaderID int64) (int64, error) {
|
||||
var count int64
|
||||
err := getDB().Model(&model.Texture{}).
|
||||
err := r.db.Model(&model.Texture{}).
|
||||
Where("uploader_id = ? AND status != -1", uploaderID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
|
||||
Reference in New Issue
Block a user