Add internal/service/food.go
This commit is contained in:
81
internal/service/food.go
Normal file
81
internal/service/food.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"gotest/internal/repository"
|
||||
)
|
||||
|
||||
// FoodService 外卖服务
|
||||
type FoodService struct {
|
||||
repo *repository.FoodRepository
|
||||
}
|
||||
|
||||
// NewFoodService 创建外卖服务实例
|
||||
func NewFoodService(repo *repository.FoodRepository) *FoodService {
|
||||
return &FoodService{repo: repo}
|
||||
}
|
||||
|
||||
// ListCategories 获取餐厅分类
|
||||
func (s *FoodService) ListCategories() (interface{}, error) {
|
||||
return s.repo.ListCategories()
|
||||
}
|
||||
|
||||
// ListRestaurants 获取餐厅列表
|
||||
func (s *FoodService) ListRestaurants(categoryID uint, keyword, sort string, page, pageSize int) (map[string]interface{}, error) {
|
||||
restaurants, total, err := s.repo.ListRestaurants(categoryID, keyword, sort, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"list": restaurants,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": pageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetRestaurant 获取餐厅详情
|
||||
func (s *FoodService) GetRestaurant(id uint) (interface{}, error) {
|
||||
return s.repo.GetRestaurant(id)
|
||||
}
|
||||
|
||||
// ListFeatured 获取推荐餐厅
|
||||
func (s *FoodService) ListFeatured(limit int) (interface{}, error) {
|
||||
return s.repo.ListFeaturedRestaurants(limit)
|
||||
}
|
||||
|
||||
// GetRestaurantMenu 获取餐厅菜单(菜品分类 + 菜品)
|
||||
func (s *FoodService) GetRestaurantMenu(restaurantID uint) (map[string]interface{}, error) {
|
||||
restaurant, err := s.repo.GetRestaurant(restaurantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
categories, err := s.repo.ListMenuCategories(restaurantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := s.repo.ListMenuItems(restaurantID, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"restaurant": restaurant,
|
||||
"categories": categories,
|
||||
"menu_items": items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetHomeData 获取外卖首页数据(分类 + 推荐餐厅)
|
||||
func (s *FoodService) GetHomeData() (map[string]interface{}, error) {
|
||||
categories, err := s.repo.ListCategories()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
featured, err := s.repo.ListFeaturedRestaurants(8)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"categories": categories,
|
||||
"featured": featured,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user