Files
digital-psychology/apps/api/internal/repository/rhythm_config_repo.go
T
jackyu66gitandCursor 62cd8c45dd chore: 合入 stash Ops hardening 与 migration 000041
Ask/catalog 权限与审计加固、量表读权限统一,以及未提交的 ops hardening 变更。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 01:46:34 +08:00

58 lines
1.4 KiB
Go

package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// RhythmConfigRow is RhythmConfig catalog row.
type RhythmConfigRow struct {
ID uuid.UUID `json:"id"`
Code string `json:"code"`
Title string `json:"title"`
Active bool `json:"active"`
System bool `json:"system"`
UpdatedAt time.Time `json:"updated_at"`
}
// ListRhythmConfigs returns RhythmConfig catalog.
func (r *AdminRepo) ListRhythmConfigs(ctx context.Context) ([]RhythmConfigRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, active, system, updated_at
FROM rhythm_configs
ORDER BY active DESC, code ASC LIMIT 500`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []RhythmConfigRow
for rows.Next() {
var row RhythmConfigRow
if err := rows.Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt); err != nil {
return nil, err
}
out = append(out, row)
}
return out, rows.Err()
}
// GetRhythmConfig loads one by id.
func (r *AdminRepo) GetRhythmConfig(ctx context.Context, id uuid.UUID) (*RhythmConfigRow, error) {
var row RhythmConfigRow
err := r.Pool.QueryRow(ctx, `
SELECT id, code, title, active, system, updated_at
FROM rhythm_configs WHERE id=$1`, id,
).Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
if err != nil {
return nil, err
}
return &row, nil
}