Phase 4: upload, LLM extraction, import review flow

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jānis Kacēns
2026-05-11 13:15:04 +03:00
parent e53e7662e9
commit 5199c1fa16
10 changed files with 447 additions and 6 deletions
+48
View File
@@ -1,8 +1,10 @@
package db
import (
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
@@ -373,3 +375,49 @@ func (r *Repo) GetStatsForUser(userID int64, questionIDs []string) (map[string]*
}
return result, rows.Err()
}
// ── Draft (import review) ────────────────────────────────────────────────────
func newDraftID() string {
b := make([]byte, 16)
rand.Read(b)
return hex.EncodeToString(b)
}
func (r *Repo) CreateDraft(userID int64, source string, questions []models.DraftQuestion) (string, error) {
data, err := json.Marshal(questions)
if err != nil {
return "", err
}
id := newDraftID()
_, err = r.db.Exec(
"INSERT INTO import_drafts (id, user_id, source, questions) VALUES (?, ?, ?, ?)",
id, userID, source, string(data),
)
if err != nil {
return "", err
}
return id, nil
}
func (r *Repo) GetDraftForUser(id string, userID int64) (*models.Draft, error) {
d := &models.Draft{}
var questionsJSON, createdAt string
err := r.db.QueryRow(
"SELECT id, user_id, source, questions, created_at FROM import_drafts WHERE id = ? AND user_id = ?",
id, userID,
).Scan(&d.ID, &d.UserID, &d.Source, &questionsJSON, &createdAt)
if err != nil {
return nil, err
}
d.CreatedAt = parseTime(createdAt)
if err := json.Unmarshal([]byte(questionsJSON), &d.Questions); err != nil {
return nil, err
}
return d, nil
}
func (r *Repo) DeleteDraft(id string) error {
_, err := r.db.Exec("DELETE FROM import_drafts WHERE id = ?", id)
return err
}
+8
View File
@@ -54,6 +54,14 @@ CREATE TABLE IF NOT EXISTS sessions (
);
CREATE INDEX IF NOT EXISTS idx_sessions_expiry ON sessions(expiry);
CREATE TABLE IF NOT EXISTS import_drafts (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
source TEXT NOT NULL DEFAULT '',
questions TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_test_answers_test ON test_answers(test_id);
CREATE INDEX IF NOT EXISTS idx_answers_question ON answers(question_id);
CREATE INDEX IF NOT EXISTS idx_stats_user ON user_question_stats(user_id);