prepared last tool

This commit is contained in:
didier committed 2026-06-16 13:39:41 -04:00
1 parent 8fa9d43530
commit a65d39cea0
2 files changed
+116

No files matched your search

+56
View File
@@ -364,6 +364,62 @@ func (s *sqliteStore) searchFTS(ctx context.Context, query string, limit uint64)
return results, nil
}
func (s *sqliteStore) listRecent(ctx context.Context, tag, repo, host string, limit uint64) ([]flashcardSummary, error) {
if limit == 0 {
limit = 20
}
q := `SELECT f.public_id, f.version, f.note, f.host, f.user, f.tags, f.repos, f.created
FROM flashcard f
WHERE f.version = (SELECT MAX(f2.version) FROM flashcard f2 WHERE f2.public_id = f.public_id)
AND f.delete_timestamp IS NULL
AND f.public_id != 0`
var args []any
if tag != "" {
q += ` AND f.tags LIKE ?`
args = append(args, `%"`+tag+`"%`)
}
if repo != "" {
q += ` AND f.repos LIKE ?`
args = append(args, `%"`+repo+`"%`)
}
if host != "" {
q += ` AND f.host = ?`
args = append(args, host)
}
q += ` ORDER BY f.created DESC LIMIT ?`
args = append(args, limit)
rows, err := s.db.QueryContext(ctx, q, args...)
if err != nil {
return nil, fmt.Errorf("list recent: %w", err)
}
defer rows.Close()
var summaries []flashcardSummary
for rows.Next() {
var s flashcardSummary
var tagsJSON, reposJSON *string
if err := rows.Scan(&s.PublicID, &s.Version, &s.Note, &s.Host, &s.User, &tagsJSON, &reposJSON, &s.Created); err != nil {
return nil, fmt.Errorf("scan: %w", err)
}
if tagsJSON != nil {
json.Unmarshal([]byte(*tagsJSON), &s.Tags)
}
if reposJSON != nil {
json.Unmarshal([]byte(*reposJSON), &s.Repos)
}
s.URL = fmt.Sprintf("/api/v1/flashcards/%d", s.PublicID)
summaries = append(summaries, s)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("rows: %w", err)
}
return summaries, nil
}
func (s *sqliteStore) close() error {
return s.db.Close()
}