d23b253609
- Updated .gitignore to exclude cache files. - Refactored main.go to streamline route registration and improve code organization. - Added detailed comments in auth_handler.go, video_handler.go, and service files for better clarity on request handling and service logic. - Improved error messages in video_handler.go to provide clearer feedback to users in Chinese. - Introduced context handling in service methods to manage request timeouts effectively.
29 lines
802 B
Go
29 lines
802 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"wx_service/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MiniProgramService struct {
|
|
// service 层负责“业务能力”,通常会依赖数据库/第三方客户端等基础设施。
|
|
// 这里通过结构体字段持有 db(依赖注入),而不是在方法里自己创建连接。
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewMiniProgramService(db *gorm.DB) *MiniProgramService {
|
|
return &MiniProgramService{db: db}
|
|
}
|
|
|
|
func (s *MiniProgramService) GetByID(ctx context.Context, id uint) (*model.MiniProgram, error) {
|
|
// WithContext(ctx) 能把请求的超时/取消信号传递给数据库层,避免慢请求一直挂着。
|
|
var mp model.MiniProgram
|
|
if err := s.db.WithContext(ctx).Where("id = ?", id).First(&mp).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &mp, nil
|
|
}
|