Skip to content

Commit

Permalink
third step - adding chat basic creating and getting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtedn21 committed Oct 3, 2023
1 parent 34d192e commit 8405b28
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from dataclasses import make_dataclass, field
from core import CustomSchema

from marshmallow.fields import Str, Date, Int, Nested, Enum as MarshmallowEnum, DateTime
from marshmallow.fields import Str, Date, Int, Nested, DateTime
from marshmallow_enum import EnumField as MarshmallowEnum

from sqlalchemy import ForeignKey
from sqlalchemy.ext.asyncio import (
Expand Down
32 changes: 32 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ class Message(MessageSchema, metaclass=MarshmallowToDataclass):
user_get_schema = UserSchema(exclude=('pk', 'city_id', 'language_id', 'gender_id'), many=True, json_schema_name='UserGetSchema')
message_get_schema = MessageSchema(exclude=('reply_to_message', 'created_by_id', 'chat_id',), many=True, json_schema_name='MessageGetSchema')
message_create_schema = MessageSchema(exclude=('pk', 'created_by', 'reply_to_message'), json_schema_name='MessageCreateSchema')
#chat_get_schema = ChatSchema(exclude=('participants', 'messages', 'last_message_id'), many=True, json_schema_name='ChatGetSchema')
#chat_create_schema = ChatSchema(exclude=('participants', 'messages', 'last_message', 'last_message_id'), json_schema_name='ChatCreateSchema')

chat_get_schema = ChatSchema(exclude=('last_message_id',), many=True, json_schema_name='ChatGetSchema')
chat_create_schema = ChatSchema(exclude=('last_message_id',), json_schema_name='ChatCreateSchema')

@register_route(
'/users/', ('get', ),
Expand Down Expand Up @@ -185,6 +189,34 @@ async def create_message(new_message: Message) -> Message:
return new_message


@register_route(
'/chats/', ('get', ),
response=chat_get_schema,
)
async def get_messages() -> list[Chat]:
async with db.create_session() as session:
sql_query = (select(ChatOrm))
result = await session.execute(sql_query)
chats = result.fetchall()
return chat_get_schema.dump(map(itemgetter(0), chats))


@register_route(
'/chats/', ('post',),
request=chat_create_schema,
response=chat_create_schema,
)
async def create_message(new_chat: Chat) -> Chat:
async with db.create_session() as session:
async with session.begin():
message_obj = ChatOrm(
name=new_chat.name,
chat_type=new_chat.chat_type,
)
session.add(message_obj)
return new_chat


@register_route('/schema/', ('get', ))
async def get_openapi_schema() -> str:
return json.dumps(openapi_object)
Expand Down
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ psycopg2 = "^2.9.7"
marshmallow = "^3.20.1"
marshmallow-jsonschema = "^0.13.0"
dacite = "^1.8.1"
marshmallow-enum = "^1.5.1"

[tool.poetry.group.dev.dependencies]
ruff = "^0.0.287"
Expand Down

0 comments on commit 8405b28

Please sign in to comment.