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{}) }