initial: blog-app snapshot
This commit is contained in:
46
repositories/issue_repo.py
Normal file
46
repositories/issue_repo.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""Issue queries. No Flask imports; take an explicit sqlite3 connection.
|
||||
|
||||
Queries filter on `issues.project_id`, which is backed by the
|
||||
`idx_issues_project_id` index (Stage 4.2; docs/analysis/optional-indexes.sql).
|
||||
"""
|
||||
|
||||
ISSUE_COLUMNS = "id, project_id, title, state, created_at, html_url, user"
|
||||
|
||||
|
||||
def _rows(cur):
|
||||
return [dict(row) for row in cur.fetchall()]
|
||||
|
||||
|
||||
def list_recent_issues(db, limit=10):
|
||||
return _rows(
|
||||
db.execute(
|
||||
f"SELECT {ISSUE_COLUMNS} FROM issues "
|
||||
"ORDER BY created_at DESC LIMIT ?",
|
||||
(limit,),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def list_issues_for_project(db, project_id, limit=None):
|
||||
sql = (
|
||||
f"SELECT {ISSUE_COLUMNS} FROM issues "
|
||||
"WHERE project_id = ? ORDER BY created_at DESC"
|
||||
)
|
||||
params = (project_id,)
|
||||
if limit is not None:
|
||||
sql += " LIMIT ?"
|
||||
params = (project_id, limit)
|
||||
return _rows(db.execute(sql, params))
|
||||
|
||||
|
||||
def search_issues(db, keyword, limit=None):
|
||||
like = f"%{keyword}%"
|
||||
sql = (
|
||||
f"SELECT {ISSUE_COLUMNS} FROM issues "
|
||||
"WHERE title LIKE ? ORDER BY created_at DESC"
|
||||
)
|
||||
params = (like,)
|
||||
if limit is not None:
|
||||
sql += " LIMIT ?"
|
||||
params = (like, limit)
|
||||
return _rows(db.execute(sql, params))
|
||||
Reference in New Issue
Block a user