后端: 1.阶段 5 course 服务边界落地 - 新增 cmd/course 独立进程入口,落地 services/course dao/rpc/sv - 新增 gateway/client/course、shared/contracts/course 和 shared/ports course port - 将 /api/v1/course/* HTTP 门面切到 course zrpc,gateway 只保留鉴权、限流、幂等、文件读取和响应透传 - 保留 course 迁移期直写 schedule_events / schedules 权限,维持课程导入两个表同事务写入语义 - 为 course parse-image 补 bytes RPC 契约和 gRPC 消息大小配置,兼容课表图片上传 - 补充 course.rpc 示例配置与阶段 5 文档基线、切流点、残留依赖和 smoke 记录
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/model"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type CourseDAO struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewCourseDAO 创建ScheduleDAO实例
|
||
func NewCourseDAO(db *gorm.DB) *CourseDAO {
|
||
return &CourseDAO{
|
||
db: db,
|
||
}
|
||
}
|
||
|
||
func (r *CourseDAO) WithTx(tx *gorm.DB) *CourseDAO {
|
||
return &CourseDAO{db: tx}
|
||
}
|
||
|
||
func (r *CourseDAO) AddUserCoursesIntoSchedule(ctx context.Context, courses []model.Schedule) error {
|
||
if err := r.db.WithContext(ctx).Create(&courses).Error; err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (r *CourseDAO) AddUserCoursesIntoScheduleEvents(ctx context.Context, events []model.ScheduleEvent) ([]int, error) {
|
||
if err := r.db.WithContext(ctx).Create(&events).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
ids := make([]int, 0, len(events))
|
||
for i := range events {
|
||
ids = append(ids, events[i].ID)
|
||
}
|
||
return ids, nil
|
||
}
|
||
|
||
// Transaction 在同一个数据库事务中执行传入的函数,供 service 层复用(自动提交/回滚)
|
||
// 规则:fn 返回 nil \-\> 提交;fn 返回 error 或发生 panic \-\> 回滚
|
||
// 说明:gorm\.\(\\\*DB\)\.Transaction 会在 fn 返回 error 时回滚,并在发生 panic 时自动回滚后继续向上抛出 panic
|
||
func (r *CourseDAO) Transaction(fn func(txDAO *CourseDAO) error) error {
|
||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||
return fn(NewCourseDAO(tx))
|
||
})
|
||
}
|