fix bugs add consumer
This commit is contained in:
parent
3b9bd19531
commit
5917b06453
@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
from typing import AsyncGenerator, Optional
|
||||||
|
|
||||||
from environs import Env
|
from environs import Env
|
||||||
from sqlalchemy.dialects.postgresql import insert
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
@ -19,6 +19,11 @@ engine = create_async_engine(DATABASE_URL, echo=False)
|
|||||||
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||||
|
async with async_session as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
async def initialize_database():
|
async def initialize_database():
|
||||||
try:
|
try:
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
|
@ -45,6 +45,9 @@ class Account(Base):
|
|||||||
region: Mapped[str] = mapped_column(String(6))
|
region: Mapped[str] = mapped_column(String(6))
|
||||||
skin_count: Mapped[int] = mapped_column(Integer)
|
skin_count: Mapped[int] = mapped_column(Integer)
|
||||||
price: Mapped[float] = mapped_column(Float)
|
price: Mapped[float] = mapped_column(Float)
|
||||||
|
|
||||||
|
valorant_points: Mapped[str] = mapped_column(Integer)
|
||||||
|
|
||||||
is_sold: Mapped[bool] = mapped_column(Boolean, default=False)
|
is_sold: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||||
buyer: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
|
buyer: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
|
||||||
date_purchase: Mapped[Optional[datetime.datetime]] = mapped_column(
|
date_purchase: Mapped[Optional[datetime.datetime]] = mapped_column(
|
||||||
|
@ -1,8 +1,43 @@
|
|||||||
from faststream.nats import NatsRouter
|
from faststream import Context
|
||||||
|
from faststream.nats import NatsBroker, NatsRouter
|
||||||
|
|
||||||
|
from consumers.enums.market import SubjectsEnum
|
||||||
|
from consumers.models import AccountAutoPriceInfo, ShopPublishResult
|
||||||
|
from consumers.utils.autoprice import calculate_valorant_account_price
|
||||||
|
from shopbot.bot.database.db import async_session
|
||||||
|
from shopbot.bot.database.models import Account
|
||||||
|
|
||||||
router = NatsRouter()
|
router = NatsRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.subscriber("shop.new_account")
|
@router.subscriber(SubjectsEnum.ACCOUNT_SHOP_NOTIFY)
|
||||||
async def new_account_proceed(msg: int):
|
async def new_account_proceed(
|
||||||
print(msg)
|
data: AccountAutoPriceInfo,
|
||||||
|
broker: NatsBroker = Context(), # noqa: B008
|
||||||
|
):
|
||||||
|
new_price = calculate_valorant_account_price(data.account_info)
|
||||||
|
async with async_session() as session:
|
||||||
|
db_account = Account(
|
||||||
|
login=data.meta_info.account.username,
|
||||||
|
password=data.meta_info.account.password,
|
||||||
|
region=data.meta_info.account.region,
|
||||||
|
price=new_price,
|
||||||
|
agent_count=0,
|
||||||
|
inventory_value=0,
|
||||||
|
knife_count=0,
|
||||||
|
skin_count=0,
|
||||||
|
last_rank="0",
|
||||||
|
level=0,
|
||||||
|
current_rank="0",
|
||||||
|
valorant_points=0,
|
||||||
|
# last_game=0,
|
||||||
|
)
|
||||||
|
session.add(db_account)
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
|
await broker.publish(
|
||||||
|
ShopPublishResult(
|
||||||
|
account_id=data.account_id, shop_id=db_account.id, new_price=new_price
|
||||||
|
),
|
||||||
|
SubjectsEnum.ACCOUNT_SHOP_PUBLISH_RESULT.format(result="success"),
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user