Files
smartmate/backend/dao/course.go
LoveLosita 75a44f2edd Version: 0.1.2.dev.260207
feat: ⚠️ 批量导入课程接口支持冲突预检测与冲突提示

- 批量导入课程接口支持预先检测冲突
- 返回并展示具体发生冲突的课程信息 📚💥
- 补全此前规划的冲突提示功能(把大饼补上了 🍞)

refactor: 🧱 使用工作单元模式管理 dao 层事务

- 引入工作单元模式(Unit of Work)统一管理 dao 层
- 新建全局事务,使跨 repo 的 gorm 事务管理更加方便 🔁

fix: 🐛 修复将任务块添加进日程接口的多个问题

- 修复核心逻辑 bug(费了老大劲 😵‍💫)
- 补充并覆盖该接口的多种异常与错误场景测试 🧪
2026-02-07 22:08:13 +08:00

51 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (dao *CourseDAO) AddUserCoursesIntoSchedule(ctx context.Context, courses []model.Schedule) error {
if err := dao.db.WithContext(ctx).Create(&courses).Error; err != nil {
return err
}
return nil
}
func (dao *CourseDAO) AddUserCoursesIntoScheduleEvents(ctx context.Context, events []model.ScheduleEvent) ([]int, error) {
if err := dao.db.WithContext(ctx).Create(&events).Error; err != nil {
return nil, err
}
ids := make([]int, 0, len(events))
for i := range events {
ids = append(ids, int(events[i].ID))
}
return ids, nil
}
// Transaction 在同一个数据库事务中执行传入的函数,供 service 层复用(自动提交/回滚)
// 规则fn 返回 nil \-\> 提交fn 返回 error 或发生 panic \-\> 回滚
// 说明gorm\.\(\\\*DB\)\.Transaction 会在 fn 返回 error 时回滚,并在发生 panic 时自动回滚后继续向上抛出 panic
func (dao *CourseDAO) Transaction(fn func(txDAO *CourseDAO) error) error {
return dao.db.Transaction(func(tx *gorm.DB) error {
return fn(NewCourseDAO(tx))
})
}