Enhance API and error handling for video services
- 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.
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
)
|
||||
|
||||
type MiniProgramService struct {
|
||||
// service 层负责“业务能力”,通常会依赖数据库/第三方客户端等基础设施。
|
||||
// 这里通过结构体字段持有 db(依赖注入),而不是在方法里自己创建连接。
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
@@ -17,6 +19,7 @@ func NewMiniProgramService(db *gorm.DB) *MiniProgramService {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user