Files
smartmate/backend/dao/course.go
LoveLosita 79b6be5f40 Version: 0.1.3.dev.260208
refactor: ♻️ 重命名部分接口接收器以避免与 dao 层包名冲突

- 调整部分接口接收器命名,避免与 dao 层包名重名 🧩

feat: 📅 新增获取用户今日日程接口并完成实现

fix: 🐛 修复现实日期与相对日期转换逻辑中的初始化时序问题

- 修复 conv/time.go 中日期转换函数的一个 bug ⏱️
- 解决 viper 在包级变量初始化时机过早的问题
- 避免因过早初始化导致无法读取配置中的学期开学/结束时间 📆
2026-02-08 19:09:40 +08:00

51 lines
1.3 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 (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, int(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))
})
}