Phase 5: library & stats
- Full library home page: question list with per-user mastery stats,
search/source filter, sort (A-Z, weakest first, most-seen), source
breakdown in header, Take a test CTA.
- GET/POST /questions/{id}: view and edit question text, source, answers;
radio-select correct answer; shows seen×/correct% stat.
- POST /questions/{id}/delete: hard delete (cascades to answers via FK).
- repo: ListQuestions supports SortWeakest/SortMostSeen via LEFT JOIN;
added CountBySource, UpdateQuestion, UpdateAnswers, DeleteQuestion.
- render: added pct template func (correct*100/seen).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+74
-5
@@ -149,23 +149,37 @@ func (r *Repo) GetQuestion(id string) (*models.Question, []*models.Answer, error
|
||||
}
|
||||
|
||||
func (r *Repo) ListQuestions(f ListFilter) ([]*models.Question, error) {
|
||||
var where []string
|
||||
var args []any
|
||||
|
||||
join := ""
|
||||
if f.UserID != 0 && (f.Sort == SortWeakest || f.Sort == SortMostSeen) {
|
||||
join = " LEFT JOIN user_question_stats s ON s.question_id = q.id AND s.user_id = ?"
|
||||
args = append(args, f.UserID)
|
||||
}
|
||||
|
||||
var where []string
|
||||
if f.Source != "" {
|
||||
where = append(where, "source = ?")
|
||||
where = append(where, "q.source = ?")
|
||||
args = append(args, f.Source)
|
||||
}
|
||||
if f.Search != "" {
|
||||
where = append(where, "text LIKE ?")
|
||||
where = append(where, "q.text LIKE ?")
|
||||
args = append(args, "%"+f.Search+"%")
|
||||
}
|
||||
|
||||
query := "SELECT id, text, source, created_at FROM questions"
|
||||
query := "SELECT q.id, q.text, q.source, q.created_at FROM questions q" + join
|
||||
if len(where) > 0 {
|
||||
query += " WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
query += " ORDER BY text COLLATE NOCASE ASC"
|
||||
|
||||
switch f.Sort {
|
||||
case SortWeakest:
|
||||
query += " ORDER BY CASE WHEN s.times_seen IS NULL OR s.times_seen = 0 THEN 0.0 ELSE CAST(s.times_correct AS REAL) / s.times_seen END ASC, q.text COLLATE NOCASE ASC"
|
||||
case SortMostSeen:
|
||||
query += " ORDER BY COALESCE(s.times_seen, 0) DESC, q.text COLLATE NOCASE ASC"
|
||||
default:
|
||||
query += " ORDER BY q.text COLLATE NOCASE ASC"
|
||||
}
|
||||
|
||||
rows, err := r.db.Query(query, args...)
|
||||
if err != nil {
|
||||
@@ -206,6 +220,61 @@ func (r *Repo) ListSources() ([]string, error) {
|
||||
return sources, rows.Err()
|
||||
}
|
||||
|
||||
// SourceStat holds a source name with its question count.
|
||||
type SourceStat struct {
|
||||
Source string
|
||||
Count int
|
||||
}
|
||||
|
||||
func (r *Repo) CountBySource() ([]SourceStat, error) {
|
||||
rows, err := r.db.Query(
|
||||
"SELECT source, COUNT(*) FROM questions WHERE source != '' GROUP BY source ORDER BY source",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var stats []SourceStat
|
||||
for rows.Next() {
|
||||
var s SourceStat
|
||||
if err := rows.Scan(&s.Source, &s.Count); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats = append(stats, s)
|
||||
}
|
||||
return stats, rows.Err()
|
||||
}
|
||||
|
||||
func (r *Repo) UpdateQuestion(id, text, source string) error {
|
||||
_, err := r.db.Exec("UPDATE questions SET text = ?, source = ? WHERE id = ?", text, source, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// AnswerUpdate carries the fields to write for a single answer row.
|
||||
type AnswerUpdate struct {
|
||||
ID int64
|
||||
Text string
|
||||
IsCorrect bool
|
||||
}
|
||||
|
||||
func (r *Repo) UpdateAnswers(updates []AnswerUpdate) error {
|
||||
for _, u := range updates {
|
||||
if _, err := r.db.Exec(
|
||||
"UPDATE answers SET text = ?, is_correct = ? WHERE id = ?",
|
||||
u.Text, u.IsCorrect, u.ID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repo) DeleteQuestion(id string) error {
|
||||
_, err := r.db.Exec("DELETE FROM questions WHERE id = ?", id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repo) CountQuestions() (int, error) {
|
||||
var n int
|
||||
return n, r.db.QueryRow("SELECT COUNT(*) FROM questions").Scan(&n)
|
||||
|
||||
Reference in New Issue
Block a user