- 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.
175 lines
4.7 KiB
Go
175 lines
4.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// textureRepository TextureRepository的实现
|
|
type textureRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// 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 := r.db.Preload("Uploader").First(&texture, id).Error
|
|
return handleNotFoundResult(&texture, err)
|
|
}
|
|
|
|
func (r *textureRepository) FindByHash(hash string) (*model.Texture, error) {
|
|
var texture model.Texture
|
|
err := r.db.Where("hash = ?", hash).First(&texture).Error
|
|
return handleNotFoundResult(&texture, err)
|
|
}
|
|
|
|
func (r *textureRepository) FindByUploaderID(uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
|
var textures []*model.Texture
|
|
var total int64
|
|
|
|
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
|
|
}
|
|
|
|
err := query.Scopes(Paginate(page, pageSize)).
|
|
Preload("Uploader").
|
|
Order("created_at DESC").
|
|
Find(&textures).Error
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return textures, total, nil
|
|
}
|
|
|
|
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 := r.db.Model(&model.Texture{}).Where("status = 1")
|
|
|
|
if publicOnly {
|
|
query = query.Where("is_public = ?", true)
|
|
}
|
|
if textureType != "" {
|
|
query = query.Where("type = ?", textureType)
|
|
}
|
|
if keyword != "" {
|
|
query = query.Where("name LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := query.Scopes(Paginate(page, pageSize)).
|
|
Preload("Uploader").
|
|
Order("created_at DESC").
|
|
Find(&textures).Error
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return textures, total, nil
|
|
}
|
|
|
|
func (r *textureRepository) Update(texture *model.Texture) error {
|
|
return r.db.Save(texture).Error
|
|
}
|
|
|
|
func (r *textureRepository) UpdateFields(id int64, fields map[string]interface{}) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
func (r *textureRepository) Delete(id int64) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).Update("status", -1).Error
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (r *textureRepository) CreateDownloadLog(log *model.TextureDownloadLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
func (r *textureRepository) IsFavorited(userID, textureID int64) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&model.UserTextureFavorite{}).
|
|
Where("user_id = ? AND texture_id = ?", userID, textureID).
|
|
Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *textureRepository) AddFavorite(userID, textureID int64) error {
|
|
favorite := &model.UserTextureFavorite{
|
|
UserID: userID,
|
|
TextureID: textureID,
|
|
}
|
|
return r.db.Create(favorite).Error
|
|
}
|
|
|
|
func (r *textureRepository) RemoveFavorite(userID, textureID int64) error {
|
|
return r.db.Where("user_id = ? AND texture_id = ?", userID, textureID).
|
|
Delete(&model.UserTextureFavorite{}).Error
|
|
}
|
|
|
|
func (r *textureRepository) GetUserFavorites(userID int64, page, pageSize int) ([]*model.Texture, int64, error) {
|
|
var textures []*model.Texture
|
|
var total int64
|
|
|
|
subQuery := r.db.Model(&model.UserTextureFavorite{}).
|
|
Select("texture_id").
|
|
Where("user_id = ?", userID)
|
|
|
|
query := r.db.Model(&model.Texture{}).
|
|
Where("id IN (?) AND status = 1", subQuery)
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := query.Scopes(Paginate(page, pageSize)).
|
|
Preload("Uploader").
|
|
Order("created_at DESC").
|
|
Find(&textures).Error
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return textures, total, nil
|
|
}
|
|
|
|
func (r *textureRepository) CountByUploaderID(uploaderID int64) (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&model.Texture{}).
|
|
Where("uploader_id = ? AND status != -1", uploaderID).
|
|
Count(&count).Error
|
|
return count, err
|
|
}
|