|
1 | 1 | from datetime import datetime |
2 | 2 |
|
3 | 3 | import sqlalchemy |
| 4 | +from sqlalchemy import select, update |
4 | 5 | from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session |
5 | 6 |
|
6 | | -from handlers import custom_types |
| 7 | +# from handlers import custom_types |
7 | 8 |
|
8 | 9 | USERNAME = 'umschooluser' |
9 | 10 | PASSWORD = 'umschoolpswd' |
@@ -129,32 +130,71 @@ def get_personal_stat(telegram_id: int, session: Session) -> list[list]: |
129 | 130 |
|
130 | 131 |
|
131 | 132 | @decorator_add_session |
132 | | -def get_random_question(telegram_id: int, session: Session) -> custom_types.Question: |
| 133 | +def get_random_question(telegram_id: int, session: Session): |
133 | 134 | """ |
134 | 135 | получить случайный, неотвеченный вопрос из бд |
135 | 136 |
|
136 | | - return: Question |
| 137 | + return: tuple |
137 | 138 | """ |
138 | | - return custom_types.Question(id=0, text="") |
| 139 | + available_ids = set() |
| 140 | + answered_ids = set() |
| 141 | + text = "" |
| 142 | + id_ = -1 |
| 143 | + with session.begin(): |
| 144 | + stmt = select(Question.id).where(1==1) |
| 145 | + for id_ in session.scalars(stmt): |
| 146 | + available_ids.add(id_) |
| 147 | + stmt = select(UserStat).where(UserStat.tg_user_id == telegram_id) |
| 148 | + for id_ in session.scalars(stmt): |
| 149 | + answered_ids.add(id_) |
| 150 | + id_ = list(available_ids - answered_ids)[0] |
| 151 | + stmt = select(Question.question_text).where(Question.id == id_) |
| 152 | + for txt in session.scalars(stmt): |
| 153 | + text = txt |
| 154 | + return (id_, text) |
139 | 155 |
|
140 | 156 |
|
141 | 157 | @decorator_add_session |
142 | 158 | def get_choices_by_question_id(question_id: int, session: Session) -> list[str]: |
143 | 159 | """ |
144 | 160 | получить ответы по заданному question_id |
145 | 161 | """ |
146 | | - return [] |
| 162 | + choice_text = [] |
| 163 | + with session.begin(): |
| 164 | + stmt = select(Choice).where(Choice.question_id == question_id) |
| 165 | + for choice in session.scalars(stmt): |
| 166 | + choice_text.append(choice.choice_text) |
| 167 | + return choice_text |
147 | 168 |
|
148 | 169 |
|
149 | 170 | @decorator_add_session |
150 | 171 | def add_user_vote_db(choice_id: int, telegram_id: int, session: Session): |
151 | 172 | """ |
152 | 173 | добавить голос пользователя выбранному варианту ответа |
153 | 174 | """ |
154 | | - pass |
| 175 | + with session.begin(): |
| 176 | + stmt = select(Choice).where(Choice.id == choice_id) |
| 177 | + result = session.execute(stmt) |
| 178 | + votes = 0 |
| 179 | + for choice in result.scalars(): |
| 180 | + print(choice) |
| 181 | + votes = choice.votes |
| 182 | + |
| 183 | + new_votes = votes + 1 |
| 184 | + stmt = ( |
| 185 | + update(Choice). |
| 186 | + where(Choice.id == choice_id). |
| 187 | + values(votes=new_votes) |
| 188 | + ) |
| 189 | + print(stmt, new_votes, choice_id) |
| 190 | + session.execute(stmt) |
| 191 | + session.commit() |
| 192 | + |
155 | 193 |
|
156 | 194 |
|
157 | 195 | if __name__ == "__main__": |
158 | | - create_tables_in_db() |
159 | | - add_question_to_db("test-question", datetime.now()) |
160 | | - add_choice_to_db("test-choice", 20) |
| 196 | + print(get_random_question(1)) |
| 197 | + print(get_choices_by_question_id(1)) |
| 198 | + # create_tables_in_db() |
| 199 | + # add_question_to_db("test-question", datetime.now()) |
| 200 | + # add_choice_to_db("test-choice", 20) |
0 commit comments