Add internal/service/shop.go
This commit is contained in:
60
internal/service/shop.go
Normal file
60
internal/service/shop.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"gotest/internal/repository"
|
||||
)
|
||||
|
||||
// ShopService 电商商品服务
|
||||
type ShopService struct {
|
||||
repo *repository.ShopRepository
|
||||
}
|
||||
|
||||
// NewShopService 创建电商服务实例
|
||||
func NewShopService(repo *repository.ShopRepository) *ShopService {
|
||||
return &ShopService{repo: repo}
|
||||
}
|
||||
|
||||
// ListCategories 获取分类列表
|
||||
func (s *ShopService) ListCategories() (interface{}, error) {
|
||||
return s.repo.ListCategories()
|
||||
}
|
||||
|
||||
// ListProducts 获取商品列表
|
||||
func (s *ShopService) ListProducts(categoryID uint, keyword, sort string, page, pageSize int) (map[string]interface{}, error) {
|
||||
products, total, err := s.repo.ListProducts(categoryID, keyword, sort, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"list": products,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetProduct 获取商品详情
|
||||
func (s *ShopService) GetProduct(id uint) (interface{}, error) {
|
||||
return s.repo.GetProduct(id)
|
||||
}
|
||||
|
||||
// ListFeatured 获取推荐商品
|
||||
func (s *ShopService) ListFeatured(limit int) (interface{}, error) {
|
||||
return s.repo.ListFeaturedProducts(limit)
|
||||
}
|
||||
|
||||
// GetHomeData 获取商城首页数据(分类 + 推荐商品)
|
||||
func (s *ShopService) GetHomeData() (map[string]interface{}, error) {
|
||||
categories, err := s.repo.ListCategories()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
featured, err := s.repo.ListFeaturedProducts(8)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"categories": categories,
|
||||
"featured": featured,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user