Add internal/repository/music.go
This commit is contained in:
92
internal/repository/music.go
Normal file
92
internal/repository/music.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gotest/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MusicRepository 音乐数据访问层
|
||||
type MusicRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewMusicRepository 创建音乐仓库实例
|
||||
func NewMusicRepository(db *gorm.DB) *MusicRepository {
|
||||
return &MusicRepository{db: db}
|
||||
}
|
||||
|
||||
// ListCategories 获取音乐分类列表
|
||||
func (r *MusicRepository) ListCategories() ([]model.MusicCategory, error) {
|
||||
var categories []model.MusicCategory
|
||||
if err := r.db.Order("sort ASC, id ASC").Find(&categories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
// ListSongs 获取歌曲列表,支持分类过滤、关键词搜索和排序
|
||||
func (r *MusicRepository) ListSongs(categoryID uint, keyword string, sort string, page, pageSize int) ([]model.Song, int64, error) {
|
||||
var songs []model.Song
|
||||
var total int64
|
||||
|
||||
query := r.db.Where("status = 1")
|
||||
if categoryID > 0 {
|
||||
query = query.Where("category_id = ?", categoryID)
|
||||
}
|
||||
if keyword != "" {
|
||||
query = query.Where("title LIKE ? OR artist LIKE ? OR album LIKE ? OR tags LIKE ?", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
if err := query.Model(&model.Song{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
switch sort {
|
||||
case "plays":
|
||||
query = query.Order("play_count DESC")
|
||||
case "likes":
|
||||
query = query.Order("like_count DESC")
|
||||
case "newest":
|
||||
query = query.Order("release_date DESC")
|
||||
default:
|
||||
query = query.Order("sort ASC, id DESC")
|
||||
}
|
||||
|
||||
if page > 0 && pageSize > 0 {
|
||||
offset := (page - 1) * pageSize
|
||||
query = query.Offset(offset).Limit(pageSize)
|
||||
}
|
||||
|
||||
if err := query.Find(&songs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return songs, total, nil
|
||||
}
|
||||
|
||||
// GetSong 根据 ID 获取歌曲详情
|
||||
func (r *MusicRepository) GetSong(id uint) (*model.Song, error) {
|
||||
var song model.Song
|
||||
if err := r.db.First(&song, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &song, nil
|
||||
}
|
||||
|
||||
// ListHotSongs 获取热门歌曲
|
||||
func (r *MusicRepository) ListHotSongs(limit int) ([]model.Song, error) {
|
||||
var songs []model.Song
|
||||
if err := r.db.Where("status = 1 AND is_hot = true").Order("play_count DESC").Limit(limit).Find(&songs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return songs, nil
|
||||
}
|
||||
|
||||
// ListNewSongs 获取新歌
|
||||
func (r *MusicRepository) ListNewSongs(limit int) ([]model.Song, error) {
|
||||
var songs []model.Song
|
||||
if err := r.db.Where("status = 1 AND is_new = true").Order("release_date DESC").Limit(limit).Find(&songs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return songs, nil
|
||||
}
|
||||
Reference in New Issue
Block a user