package repository import ( "context" "evobgp/internal/store" ) func (p *Postgres) fillModuleDohFields(ctx context.Context, m *store.Module) error { if m == nil { return nil } rows, err := p.pool.Query(ctx, ` SELECT doh_profile_id::text FROM module_doh_profile WHERE module_id = $1 ORDER BY sort_order, doh_profile_id`, m.ID) if err != nil { return err } defer rows.Close() var ids []string for rows.Next() { var id string if err := rows.Scan(&id); err != nil { return err } ids = append(ids, id) } if err := rows.Err(); err != nil { return err } m.DohProfileIDs = store.NormalizeDohProfileIDList(ids) m.SyncLegacyDohProfileID() return nil } func (p *Postgres) setModuleDohProfiles(ctx context.Context, moduleID string, ids []string) error { ids = store.NormalizeDohProfileIDList(ids) if _, err := p.pool.Exec(ctx, `DELETE FROM module_doh_profile WHERE module_id = $1`, moduleID); err != nil { return err } for i, id := range ids { if _, err := p.pool.Exec(ctx, ` INSERT INTO module_doh_profile (module_id, doh_profile_id, sort_order) VALUES ($1, $2, $3)`, moduleID, id, i); err != nil { return err } } var dohArg any if len(ids) > 0 { dohArg = ids[0] } _, err := p.pool.Exec(ctx, `UPDATE module SET doh_profile_id = $2, updated_at = now() WHERE id = $1`, moduleID, dohArg) return err } func (p *Postgres) moduleDohProfileInUse(ctx context.Context, dohProfileID string) (bool, error) { var n int if err := p.pool.QueryRow(ctx, ` SELECT COUNT(*) FROM module_doh_profile mdp JOIN module m ON m.id = mdp.module_id WHERE mdp.doh_profile_id = $1::uuid AND m.deleted_at IS NULL`, dohProfileID).Scan(&n); err != nil { return false, err } return n > 0, nil }