Files
pig-farm-controller/internal/infra/repository/plan_repository.go

50 lines
1.6 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 repository
import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
// PlanRepository 定义了与计划模型相关的数据库操作接口
// 这是为了让业务逻辑层依赖于抽象,而不是具体的数据库实现
type PlanRepository interface {
// ListBasicPlans 获取所有计划的基本信息,不包含子计划和任务详情
ListBasicPlans() ([]models.Plan, error)
// GetBasicPlanByID 根据ID获取计划的基本信息不包含子计划和任务详情
GetBasicPlanByID(id uint) (*models.Plan, error)
}
// gormPlanRepository 是 PlanRepository 的 GORM 实现
type gormPlanRepository struct {
db *gorm.DB
}
// NewGormPlanRepository 创建一个新的 PlanRepository GORM 实现实例
func NewGormPlanRepository(db *gorm.DB) PlanRepository {
return &gormPlanRepository{
db: db,
}
}
// ListBasicPlans 获取所有计划的基本信息,不包含子计划和任务详情
func (r *gormPlanRepository) ListBasicPlans() ([]models.Plan, error) {
var plans []models.Plan
// GORM 默认不会加载关联,除非使用 Preload所以直接 Find 即可满足要求
result := r.db.Find(&plans)
if result.Error != nil {
return nil, result.Error
}
return plans, nil
}
// GetBasicPlanByID 根据ID获取计划的基本信息不包含子计划和任务详情
func (r *gormPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
var plan models.Plan
// GORM 默认不会加载关联,除非使用 Preload所以直接 First 即可满足要求
result := r.db.First(&plan, id)
if result.Error != nil {
return nil, result.Error
}
return &plan, nil
}