mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 20:30:38 +04:00
Add requirements, cli commands. New project structure
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from .crud import *
|
||||
from .schemas import *
|
||||
from .database import get_session, init_models
|
||||
from .database import get_session, drop_all, create_all, recreate_all
|
||||
from .crud import *
|
||||
@@ -1,8 +0,0 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
|
||||
from .models import *
|
||||
|
||||
|
||||
async def get_user(db: AsyncSession, user_id: int):
|
||||
return await db.get(User, user_id)
|
||||
1
database/crud/__init__.py
Normal file
1
database/crud/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .games import *
|
||||
16
database/crud/games.py
Normal file
16
database/crud/games.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
|
||||
from .. import models as mdl
|
||||
from .. import schemas as sch
|
||||
from ..database import add_transaction
|
||||
|
||||
async def add_game(db: AsyncSession, game_info: sch.GameCreate, user_id: int):
|
||||
game = mdl.Game(**game_info.model_dump(), owner_id=user_id)
|
||||
return await add_transaction(db, game)
|
||||
|
||||
async def get_games(db: AsyncSession):
|
||||
return (await db.execute(select(mdl.Game))).scalars().all()
|
||||
|
||||
async def get_game(db: AsyncSession, game_id: int):
|
||||
return await db.get(mdl.Game, game_id)
|
||||
@@ -19,8 +19,23 @@ async def get_session() -> AsyncSession: # type: ignore
|
||||
async with async_session() as session: # type: ignore
|
||||
yield session
|
||||
|
||||
|
||||
async def init_models():
|
||||
async def drop_all():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
async def create_all():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
async def recreate_all():
|
||||
await drop_all()
|
||||
await create_all()
|
||||
|
||||
async def add_transaction[T](db: AsyncSession, entity: T) -> T:
|
||||
try:
|
||||
db.add(entity)
|
||||
await db.commit()
|
||||
await db.refresh(entity)
|
||||
return entity
|
||||
except Exception as ex:
|
||||
await db.rollback()
|
||||
raise ex
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .database import Base
|
||||
@@ -9,8 +9,10 @@ class Game(Base):
|
||||
__tablename__ = "games"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(String)
|
||||
cover = Column(String)
|
||||
title = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
torrent_file = Column(String, nullable=False)
|
||||
language = Column(String)
|
||||
version = Column(String)
|
||||
download_size = Column(String)
|
||||
@@ -30,8 +32,8 @@ class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
email = Column(String, unique=True)
|
||||
name = Column(String)
|
||||
hash_of_password = Column(String)
|
||||
email = Column(String, nullable=False, unique=True)
|
||||
name = Column(String, nullable=False)
|
||||
hash_of_password = Column(String, nullable=False)
|
||||
|
||||
games = relationship("Game", back_populates="owner")
|
||||
|
||||
@@ -2,8 +2,10 @@ from pydantic import BaseModel
|
||||
|
||||
|
||||
class GameBase(BaseModel):
|
||||
cover: str | None = None
|
||||
title: str
|
||||
description: str | None = None
|
||||
torrent_file: str
|
||||
language: str | None = None
|
||||
version: str | None = None
|
||||
download_size: str | None = None
|
||||
@@ -25,7 +27,7 @@ class Game(GameBase):
|
||||
owner_id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
@@ -42,4 +44,4 @@ class User(UserBase):
|
||||
games: list[Game] = []
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
from_attributes = True
|
||||
|
||||
Reference in New Issue
Block a user