71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"gotest/internal/repository"
|
|
)
|
|
|
|
// GameService 游戏服务
|
|
type GameService struct {
|
|
repo *repository.GameRepository
|
|
}
|
|
|
|
// NewGameService 创建游戏服务实例
|
|
func NewGameService(repo *repository.GameRepository) *GameService {
|
|
return &GameService{repo: repo}
|
|
}
|
|
|
|
// ListCategories 获取游戏分类
|
|
func (s *GameService) ListCategories() (interface{}, error) {
|
|
return s.repo.ListCategories()
|
|
}
|
|
|
|
// ListGames 获取游戏列表
|
|
func (s *GameService) ListGames(categoryID uint, keyword, sort string, page, pageSize int) (map[string]interface{}, error) {
|
|
games, total, err := s.repo.ListGames(categoryID, keyword, sort, page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]interface{}{
|
|
"list": games,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
}, nil
|
|
}
|
|
|
|
// GetGame 获取游戏详情
|
|
func (s *GameService) GetGame(id uint) (interface{}, error) {
|
|
return s.repo.GetGame(id)
|
|
}
|
|
|
|
// ListHot 获取热门游戏
|
|
func (s *GameService) ListHot(limit int) (interface{}, error) {
|
|
return s.repo.ListHotGames(limit)
|
|
}
|
|
|
|
// ListNew 获取新游
|
|
func (s *GameService) ListNew(limit int) (interface{}, error) {
|
|
return s.repo.ListNewGames(limit)
|
|
}
|
|
|
|
// GetHomeData 获取游戏首页数据(分类 + 热门 + 新游)
|
|
func (s *GameService) GetHomeData() (map[string]interface{}, error) {
|
|
categories, err := s.repo.ListCategories()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hot, err := s.repo.ListHotGames(8)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newGames, err := s.repo.ListNewGames(4)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]interface{}{
|
|
"categories": categories,
|
|
"hot": hot,
|
|
"new": newGames,
|
|
}, nil
|
|
}
|