Files
digital-psychology/apps/api/internal/repository/image_card_deck_write.go
T
jackyu66gitandCursor 0158036341 feat(ECR-045): ImageCardDeck 写面闭环并 Closed
复用 admin.explore.write、POST/PUT+审计、C端 GET /cards/decks、
H5 无 active 空态/失败回退;migration 000054。无牌面内容编辑。
ExploreConfig Loop STOP,禁自动 ECR-046。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 21:29:43 +08:00

140 lines
3.7 KiB
Go

package repository
import (
"context"
"encoding/json"
"errors"
"strings"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// ImageCardDeckWriteInput is create/update payload.
type ImageCardDeckWriteInput struct {
Code string
Title string
Active bool
}
// ListActiveImageCardDecks returns active decks for C-end.
func (r *AdminRepo) ListActiveImageCardDecks(ctx context.Context) ([]ImageCardDeckRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, active, system, updated_at
FROM image_card_decks
WHERE active = true
ORDER BY code ASC
LIMIT 100`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []ImageCardDeckRow
for rows.Next() {
var row ImageCardDeckRow
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()
}
// CreateImageCardDeckWithAudit inserts and audits.
func (r *AdminRepo) CreateImageCardDeckWithAudit(
ctx context.Context, adminID uuid.UUID, in ImageCardDeckWriteInput, meta json.RawMessage,
) (*ImageCardDeckRow, error) {
tx, err := r.Pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
var row ImageCardDeckRow
err = tx.QueryRow(ctx, `
INSERT INTO image_card_decks(code, title, active, system)
VALUES ($1,$2,$3,false)
RETURNING id, code, title, active, system, updated_at`,
in.Code, in.Title, in.Active,
).Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt)
if err != nil {
return nil, mapImageCardDeckWriteErr(err)
}
if meta == nil {
meta = json.RawMessage(`{}`)
}
if _, err := tx.Exec(ctx, `
INSERT INTO admin_audit_logs(admin_id, action, target_type, target_id, meta)
VALUES ($1,'explore.image_card_deck.create','image_card_deck',$2,$3)`,
adminID, row.ID.String(), meta,
); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return &row, nil
}
// UpdateImageCardDeckWithAudit updates and audits.
func (r *AdminRepo) UpdateImageCardDeckWithAudit(
ctx context.Context, adminID, id uuid.UUID, in ImageCardDeckWriteInput, meta json.RawMessage,
) (*ImageCardDeckRow, error) {
tx, err := r.Pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
var system bool
var oldCode string
err = tx.QueryRow(ctx, `SELECT system, code FROM image_card_decks WHERE id=$1`, id).Scan(&system, &oldCode)
if errors.Is(err, pgx.ErrNoRows) {
return nil, pgx.ErrNoRows
}
if err != nil {
return nil, err
}
code := in.Code
if system {
code = oldCode
}
var row ImageCardDeckRow
err = tx.QueryRow(ctx, `
UPDATE image_card_decks
SET code=$2, title=$3, active=$4, updated_at=now()
WHERE id=$1
RETURNING id, code, title, active, system, updated_at`,
id, code, in.Title, in.Active,
).Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt)
if err != nil {
return nil, mapImageCardDeckWriteErr(err)
}
if meta == nil {
meta = json.RawMessage(`{}`)
}
if _, err := tx.Exec(ctx, `
INSERT INTO admin_audit_logs(admin_id, action, target_type, target_id, meta)
VALUES ($1,'explore.image_card_deck.update','image_card_deck',$2,$3)`,
adminID, id.String(), meta,
); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return &row, nil
}
func mapImageCardDeckWriteErr(err error) error {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return errString("image card deck code conflict")
}
return err
}
// ImageCardDeckCodeConflict reports unique violation.
func ImageCardDeckCodeConflict(err error) bool {
return err != nil && strings.Contains(err.Error(), "image card deck code conflict")
}