feat(generation): persist summary outcomes without articles
Record generation outcomes and summaries for history while keeping article content transient. Refs #7 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
59
backend/internal/models/generations.go
Normal file
59
backend/internal/models/generations.go
Normal file
@ -0,0 +1,59 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GenerationHistory struct {
|
||||
ID string `json:"id"`
|
||||
SummaryID *string `json:"summary_id"`
|
||||
Outcome json.RawMessage `json:"outcome"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// SaveGeneration deliberately accepts no article data and commits both rows together.
|
||||
func (r *Repository) SaveGeneration(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time) error {
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var summaryID *string
|
||||
if summary != nil {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO summaries (id,user_id,content,ai_provider_id,generated_at) VALUES ($1,$2,$3,$4,$5)`, summary.ID, userID, summary.Content, summary.AIProviderID, summary.GeneratedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
summaryID = &summary.ID
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO summary_generations (id,user_id,summary_id,outcome,created_at) VALUES ($1,$2,$3,$4,$5)`, id, userID, summaryID, string(outcome), createdAt); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
func (r *Repository) ListGenerations(ctx context.Context, userID string, limit int) ([]GenerationHistory, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT id,summary_id,outcome,created_at FROM summary_generations WHERE user_id=$1 ORDER BY created_at DESC,id DESC LIMIT $2`, userID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := []GenerationHistory{}
|
||||
for rows.Next() {
|
||||
var g GenerationHistory
|
||||
if err = rows.Scan(&g.ID, &g.SummaryID, &g.Outcome, &g.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, g)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
func (r *Repository) GetOwnedSummary(id, userID string) (*Summary, error) {
|
||||
s := &Summary{}
|
||||
err := r.db.QueryRow(`SELECT id,user_id,content,ai_provider_id,generated_at FROM summaries WHERE id=$1 AND user_id=$2`, id, userID).Scan(&s.ID, &s.UserID, &s.Content, &s.AIProviderID, &s.GeneratedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
Reference in New Issue
Block a user