Spaces:
Sleeping
Sleeping
samar m commited on
Commit ·
dfcb935
1
Parent(s): d8394f9
feat: add batch/topic/question DB queries
Browse files- backend/db/queries.py +86 -0
backend/db/queries.py
CHANGED
|
@@ -79,3 +79,89 @@ async def delete_old_refresh_tokens(user_id: str) -> None:
|
|
| 79 |
await conn.execute(
|
| 80 |
"DELETE FROM refresh_tokens WHERE user_id = $1", user_id
|
| 81 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
await conn.execute(
|
| 80 |
"DELETE FROM refresh_tokens WHERE user_id = $1", user_id
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
async def create_batch(name: str, instructor_id: str) -> asyncpg.Record:
|
| 85 |
+
async with get_pool().acquire() as conn:
|
| 86 |
+
return await conn.fetchrow(
|
| 87 |
+
"""
|
| 88 |
+
INSERT INTO batches (name, instructor_id)
|
| 89 |
+
VALUES ($1, $2)
|
| 90 |
+
RETURNING *
|
| 91 |
+
""",
|
| 92 |
+
name, instructor_id,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
async def get_batch_by_instructor_id(instructor_id: str) -> Optional[asyncpg.Record]:
|
| 97 |
+
async with get_pool().acquire() as conn:
|
| 98 |
+
return await conn.fetchrow(
|
| 99 |
+
"SELECT * FROM batches WHERE instructor_id = $1", instructor_id
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
async def get_batch_by_id(batch_id: str) -> Optional[asyncpg.Record]:
|
| 104 |
+
async with get_pool().acquire() as conn:
|
| 105 |
+
return await conn.fetchrow(
|
| 106 |
+
"SELECT * FROM batches WHERE id = $1", batch_id
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
async def create_topic(batch_id: str, name: str, order_index: int) -> asyncpg.Record:
|
| 111 |
+
async with get_pool().acquire() as conn:
|
| 112 |
+
return await conn.fetchrow(
|
| 113 |
+
"""
|
| 114 |
+
INSERT INTO topics (batch_id, name, order_index)
|
| 115 |
+
VALUES ($1, $2, $3)
|
| 116 |
+
RETURNING *
|
| 117 |
+
""",
|
| 118 |
+
batch_id, name, order_index,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
async def get_topics_by_batch_id(batch_id: str) -> list[asyncpg.Record]:
|
| 123 |
+
async with get_pool().acquire() as conn:
|
| 124 |
+
return await conn.fetch(
|
| 125 |
+
"SELECT * FROM topics WHERE batch_id = $1 ORDER BY order_index ASC",
|
| 126 |
+
batch_id,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
async def get_topic_by_id(topic_id: str) -> Optional[asyncpg.Record]:
|
| 131 |
+
async with get_pool().acquire() as conn:
|
| 132 |
+
return await conn.fetchrow(
|
| 133 |
+
"SELECT * FROM topics WHERE id = $1", topic_id
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
async def set_topic_unlock(topic_id: str, is_unlocked: bool) -> asyncpg.Record:
|
| 138 |
+
async with get_pool().acquire() as conn:
|
| 139 |
+
return await conn.fetchrow(
|
| 140 |
+
"""
|
| 141 |
+
UPDATE topics SET is_unlocked = $1
|
| 142 |
+
WHERE id = $2
|
| 143 |
+
RETURNING *
|
| 144 |
+
""",
|
| 145 |
+
is_unlocked, topic_id,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
async def create_questions_bulk(topic_id: str, rows: list[dict]) -> int:
|
| 150 |
+
async with get_pool().acquire() as conn:
|
| 151 |
+
await conn.executemany(
|
| 152 |
+
"""
|
| 153 |
+
INSERT INTO questions (topic_id, question_text, difficulty)
|
| 154 |
+
VALUES ($1, $2, $3)
|
| 155 |
+
""",
|
| 156 |
+
[(topic_id, r["question_text"], r["difficulty"]) for r in rows],
|
| 157 |
+
)
|
| 158 |
+
return len(rows)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
async def get_question_count_by_topic(topic_id: str) -> int:
|
| 162 |
+
async with get_pool().acquire() as conn:
|
| 163 |
+
result = await conn.fetchrow(
|
| 164 |
+
"SELECT COUNT(*) AS count FROM questions WHERE topic_id = $1",
|
| 165 |
+
topic_id,
|
| 166 |
+
)
|
| 167 |
+
return result["count"]
|