From 65d48f2826472be1b8f9d9298040edf2df405f31 Mon Sep 17 00:00:00 2001 From: "Gleb O. Ivaniczkij" Date: Tue, 13 Aug 2024 20:22:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BA=D1=80=D0=B8=D1=82=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/main.py | 28 ++++++++++++++-------------- models/main.py | 7 ++++--- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/database/main.py b/database/main.py index 4d167f5..af4c517 100644 --- a/database/main.py +++ b/database/main.py @@ -29,7 +29,7 @@ class Users: async with connection.cursor() as cursor: sql = ''' insert into users ( - telegram_id, + telegram_user_id, first_name, last_name, username @@ -38,7 +38,7 @@ class Users: %(first_name)s, %(last_name)s, %(username)s - ) on conflict (telegram_id) do update set + ) on conflict (telegram_user_id) do update set first_name = excluded.first_name, last_name = excluded.last_name, username = excluded.username @@ -116,7 +116,7 @@ class PollOptions: where poll_options.poll_schema_id = %(poll_schema_id)s and poll_options.ordinal is not null; - ''' if ordinals is not None else ''' + ''' if ordinals is None else ''' select poll_options.id, poll_options.name, @@ -138,6 +138,7 @@ class PollOptions: return [ models.PollOption( id=poll_option_id, + poll_schema=poll_schema, name=name, ordinal=ordinal, ) @@ -159,13 +160,12 @@ class Polls: poll_schemas.id, poll_schemas.name, poll_schemas.question, - poll_schemas.options, polls.created_at, - polls.is_active + polls.is_complete from polls inner join poll_schemas on - polls.schema_id = poll_schemas.id; + polls.poll_schema_id = poll_schemas.id; ''' await cursor.execute(sql) records = await cursor.fetchall() @@ -178,12 +178,11 @@ class Polls: id=poll_schema_id, name=name, question=question, - options=options, ), created_at=created_at, - is_active=is_active, + is_complete=is_complete, ) - for poll_id, telegram_message_id, telegram_poll_id, poll_schema_id, name, question, options, created_at, is_active + for poll_id, telegram_message_id, telegram_poll_id, poll_schema_id, name, question, created_at, is_complete in records ] @@ -199,7 +198,7 @@ class Polls: insert into polls ( telegram_message_id, telegram_poll_id, - poll_schema + poll_schema_id ) values ( %(telegram_message_id)s, %(telegram_poll_id)s, @@ -207,18 +206,18 @@ class Polls: ) returning polls.id, polls.created_at, - polls.is_active; + polls.is_complete; ''' await cursor.execute( sql, { 'telegram_message_id': telegram_message_id, 'telegram_poll_id': telegram_poll_id, - 'poll_schema': poll_schema.id, + 'poll_schema_id': poll_schema.id, }, ) try: - poll_id, created_at, is_active = await cursor.fetchone() + poll_id, created_at, is_complete = await cursor.fetchone() except TypeError: raise NotFoundError() return models.Poll( @@ -227,7 +226,7 @@ class Polls: telegram_poll_id=telegram_poll_id, poll_schema=poll_schema, created_at=created_at, - is_active=is_active, + is_complete=is_complete, ) @@ -264,6 +263,7 @@ class PollAnswers: for poll_option in poll_options ], + returning=True, ) records = await cursor.fetchall() return [ diff --git a/models/main.py b/models/main.py index 0811bc2..45d49a5 100644 --- a/models/main.py +++ b/models/main.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Optional from pydantic import BaseModel, Field @@ -9,8 +10,8 @@ class User(BaseModel): exclude=True, ) first_name: str - last_name: str - username: str + last_name: Optional[str] + username: Optional[str] class PollSchema(BaseModel): @@ -23,7 +24,7 @@ class PollOption(BaseModel): id: int poll_schema: PollSchema name: str - ordinal: int + ordinal: Optional[int] class Poll(BaseModel):