feat: add AI chat for repports

This commit is contained in:
2026-04-28 07:19:49 +02:00
parent 087bcab16b
commit 490a364c00
10 changed files with 340 additions and 5 deletions

View File

@ -666,3 +666,55 @@ func (r *Repository) DeleteReport(id, userID string) error {
_, err := r.db.Exec(`DELETE FROM reports WHERE id=$1 AND user_id=$2`, id, userID)
return err
}
func (r *Repository) GetReport(id, userID string) (*Report, error) {
rep := &Report{}
err := r.db.QueryRow(`
SELECT id, user_id, summary_id, context_excerpt, question, answer, status, error_msg, created_at
FROM reports WHERE id=$1 AND user_id=$2`, id, userID,
).Scan(&rep.ID, &rep.UserID, &rep.SummaryID, &rep.ContextExcerpt, &rep.Question, &rep.Answer, &rep.Status, &rep.ErrorMsg, &rep.CreatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
return rep, err
}
// ── Report messages ────────────────────────────────────────────────────────
func (r *Repository) CreateReportMessage(reportID, role, content, status string) (*ReportMessage, error) {
msg := &ReportMessage{}
err := r.db.QueryRow(`
INSERT INTO report_messages (report_id, role, content, status)
VALUES ($1, $2, $3, $4)
RETURNING id, report_id, role, content, status, created_at`,
reportID, role, content, status,
).Scan(&msg.ID, &msg.ReportID, &msg.Role, &msg.Content, &msg.Status, &msg.CreatedAt)
return msg, err
}
func (r *Repository) UpdateReportMessage(id, status, content string) error {
_, err := r.db.Exec(`
UPDATE report_messages SET status=$1, content=$2 WHERE id=$3`,
status, content, id)
return err
}
func (r *Repository) ListReportMessages(reportID string) ([]ReportMessage, error) {
rows, err := r.db.Query(`
SELECT id, report_id, role, content, status, created_at
FROM report_messages WHERE report_id=$1
ORDER BY created_at ASC`, reportID)
if err != nil {
return nil, err
}
defer rows.Close()
var msgs []ReportMessage
for rows.Next() {
var m ReportMessage
if err := rows.Scan(&m.ID, &m.ReportID, &m.Role, &m.Content, &m.Status, &m.CreatedAt); err != nil {
return nil, err
}
msgs = append(msgs, m)
}
return msgs, nil
}