176 lines
4.8 KiB
Go
176 lines
4.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// textureRepositoryImpl TextureRepository的实现
|
|
type textureRepositoryImpl struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewTextureRepository 创建TextureRepository实例
|
|
func NewTextureRepository(db *gorm.DB) TextureRepository {
|
|
return &textureRepositoryImpl{db: db}
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) Create(texture *model.Texture) error {
|
|
return r.db.Create(texture).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) 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 *textureRepositoryImpl) 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 *textureRepositoryImpl) 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 *textureRepositoryImpl) 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 *textureRepositoryImpl) Update(texture *model.Texture) error {
|
|
return r.db.Save(texture).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) UpdateFields(id int64, fields map[string]interface{}) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) Delete(id int64) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).Update("status", -1).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) IncrementDownloadCount(id int64) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
|
UpdateColumn("download_count", gorm.Expr("download_count + ?", 1)).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) IncrementFavoriteCount(id int64) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
|
UpdateColumn("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) DecrementFavoriteCount(id int64) error {
|
|
return r.db.Model(&model.Texture{}).Where("id = ?", id).
|
|
UpdateColumn("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) CreateDownloadLog(log *model.TextureDownloadLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) 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 *textureRepositoryImpl) AddFavorite(userID, textureID int64) error {
|
|
favorite := &model.UserTextureFavorite{
|
|
UserID: userID,
|
|
TextureID: textureID,
|
|
}
|
|
return r.db.Create(favorite).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) RemoveFavorite(userID, textureID int64) error {
|
|
return r.db.Where("user_id = ? AND texture_id = ?", userID, textureID).
|
|
Delete(&model.UserTextureFavorite{}).Error
|
|
}
|
|
|
|
func (r *textureRepositoryImpl) 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 *textureRepositoryImpl) 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
|
|
}
|
|
|