Version: 0.9.73.dev.260505
后端: 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 记录
This commit is contained in:
52
backend/services/course/dao/connect.go
Normal file
52
backend/services/course/dao/connect.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OpenDBFromConfig 创建 course 服务自己的数据库句柄。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. course 当前没有独立课程写模型表,导入链路迁移期仍写 schedule_events / schedules;
|
||||
// 2. 本函数不 AutoMigrate schedule 表,避免 course 进程越权管理 schedule schema;
|
||||
// 3. 启动期只检查运行时依赖表是否存在,缺表时尽早失败。
|
||||
func OpenDBFromConfig() (*gorm.DB, error) {
|
||||
host := viper.GetString("database.host")
|
||||
port := viper.GetString("database.port")
|
||||
user := viper.GetString("database.user")
|
||||
password := viper.GetString("database.password")
|
||||
dbname := viper.GetString("database.dbname")
|
||||
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
user, password, host, port, dbname,
|
||||
)
|
||||
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = ensureRuntimeDependencyTables(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// ensureRuntimeDependencyTables 显式检查 course 导入链路迁移期仍直写的 schedule 表。
|
||||
//
|
||||
// 说明:
|
||||
// 1. schedule_events / schedules 属于 schedule 服务正式日程域;
|
||||
// 2. 本轮保留 course 直写权限,用来维持课程导入两个表同事务写入;
|
||||
// 3. 后续若改为 schedule RPC bridge,应先补课程导入幂等与冲突返回契约,再移除这里的依赖检查。
|
||||
func ensureRuntimeDependencyTables(db *gorm.DB) error {
|
||||
for _, table := range []string{"schedule_events", "schedules"} {
|
||||
if !db.Migrator().HasTable(table) {
|
||||
return fmt.Errorf("course runtime dependency table missing: %s", table)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
50
backend/services/course/dao/course.go
Normal file
50
backend/services/course/dao/course.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user