todo List 작성 api 추가

This commit is contained in:
2026-03-05 01:19:57 +09:00
parent de7468b256
commit 11423e5106
2 changed files with 140 additions and 0 deletions

View File

@@ -143,6 +143,93 @@ def init_db() -> None:
"ON best_picks(is_active, score_total DESC);"
)
# ── todos 테이블 ───────────────────────────────────────────────────────
conn.execute(
"""
CREATE TABLE IF NOT EXISTS todos (
id TEXT PRIMARY KEY
DEFAULT (lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2)))),
title TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'todo'
CHECK(status IN ('todo','in_progress','done')),
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
);
"""
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_todos_created ON todos(created_at DESC);"
)
# ── todos CRUD ───────────────────────────────────────────────────────────────
def _todo_row_to_dict(r) -> Dict[str, Any]:
return {
"id": r["id"],
"title": r["title"],
"description": r["description"],
"status": r["status"],
"created_at": r["created_at"],
"updated_at": r["updated_at"],
}
def get_all_todos() -> List[Dict[str, Any]]:
with _conn() as conn:
rows = conn.execute(
"SELECT * FROM todos ORDER BY created_at DESC"
).fetchall()
return [_todo_row_to_dict(r) for r in rows]
def create_todo(title: str, description: Optional[str], status: str) -> Dict[str, Any]:
with _conn() as conn:
conn.execute(
"INSERT INTO todos (title, description, status) VALUES (?, ?, ?)",
(title, description, status),
)
row = conn.execute(
"SELECT * FROM todos WHERE rowid = last_insert_rowid()"
).fetchone()
return _todo_row_to_dict(row)
def update_todo(todo_id: str, fields: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""fields에 있는 항목만 업데이트 (PATCH 방식), updated_at 자동 갱신"""
allowed = {"title", "description", "status"}
updates = {k: v for k, v in fields.items() if k in allowed}
if not updates:
with _conn() as conn:
row = conn.execute("SELECT * FROM todos WHERE id = ?", (todo_id,)).fetchone()
return _todo_row_to_dict(row) if row else None
set_clauses = ", ".join(f"{k} = ?" for k in updates)
set_clauses += ", updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')"
args = list(updates.values()) + [todo_id]
with _conn() as conn:
conn.execute(
f"UPDATE todos SET {set_clauses} WHERE id = ?",
args,
)
row = conn.execute("SELECT * FROM todos WHERE id = ?", (todo_id,)).fetchone()
return _todo_row_to_dict(row) if row else None
def delete_todo(todo_id: str) -> bool:
with _conn() as conn:
cur = conn.execute("DELETE FROM todos WHERE id = ?", (todo_id,))
return cur.rowcount > 0
def delete_done_todos() -> int:
with _conn() as conn:
cur = conn.execute("DELETE FROM todos WHERE status = 'done'")
return cur.rowcount
def upsert_draw(row: Dict[str, Any]) -> None:
with _conn() as conn:
conn.execute(