Files
digital-psychology/apps/api/internal/repository/report_template_repo.go
T
jackyu66git 5de9a7bbe5 feat(ECR-038): GrowthInsights ReportTemplate 只读并 Closed
ReportTemplate catalog (000039) · Loop continuous.
2026-08-08 03:16:57 +08:00

59 lines
1.5 KiB
Go

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