mengstack-api/internal/modules/example/infrastructure/example_repo.go
MengStack Dev 9bb2c447ec
Some checks failed
CI / Build & Test (push) Failing after 40s
feat: 插件接口预留 — Plugin 接口 + Registry + 示例插件
- kernel/plugin: 定义 Plugin 接口(Metadata/FxOption/SetupRoutes/Init)
- kernel/plugin: 全局 Registry(Register/All/InitAll/Reset)
- modules/example: 完整 4 层示例插件,实现 Plugin 接口
- config: 新增 PluginConfig(enabled 列表)
- app.go: 集成插件系统 — fx.Invoke 注册 + plugin.All() 路由 + InitAll 生命周期
2026-10-03 02:07:46 +08:00

61 lines
1.6 KiB
Go

package infrastructure
import (
"context"
"mengstack/internal/modules/example/domain"
"gorm.io/gorm"
)
type exampleRepo struct {
db *gorm.DB
}
// NewRepository returns a domain.Repository backed by GORM.
func NewRepository(db *gorm.DB) domain.Repository {
return &exampleRepo{db: db}
}
func (r *exampleRepo) Create(ctx context.Context, e *domain.Example) error {
return r.db.WithContext(ctx).Create(e).Error
}
func (r *exampleRepo) FindByID(ctx context.Context, tenantID string, id uint) (*domain.Example, error) {
var e domain.Example
err := r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).First(&e).Error
if err != nil {
return nil, err
}
return &e, nil
}
func (r *exampleRepo) FindAll(ctx context.Context, tenantID string) ([]domain.Example, error) {
var items []domain.Example
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&items).Error
return items, err
}
func (r *exampleRepo) Update(ctx context.Context, tenantID string, e *domain.Example) error {
return r.db.WithContext(ctx).
Model(e).
Where("id = ? AND tenant_id = ?", e.ID, tenantID).
Updates(map[string]any{
"name": e.Name,
"description": e.Description,
"status": e.Status,
"expires_at": e.ExpiresAt,
}).Error
}
func (r *exampleRepo) Delete(ctx context.Context, tenantID string, id uint) error {
return r.db.WithContext(ctx).
Where("id = ? AND tenant_id = ?", id, tenantID).
Delete(&domain.Example{}).Error
}
// Migrate runs auto-migration for the example table.
func Migrate(db *gorm.DB) error {
return db.AutoMigrate(&domain.Example{})
}