mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 12:20:38 +04:00
Add swagger examples and description
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,5 @@
|
|||||||
content/*
|
content/*
|
||||||
dev_database.db
|
dev_database*.db
|
||||||
run.bat
|
run.bat
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"python.analysis.typeCheckingMode": "basic"
|
||||||
|
}
|
||||||
@@ -17,9 +17,22 @@ async def add_game(db: AsyncSession,
|
|||||||
return await add_transaction(db, game)
|
return await add_transaction(db, game)
|
||||||
|
|
||||||
|
|
||||||
|
async def edit_game(db: AsyncSession,
|
||||||
|
game_id: int,
|
||||||
|
game_info: sch.GameCreate):
|
||||||
|
game = await db.get(mdl.Game, game_id)
|
||||||
|
game_fields = [c.name for c in mdl.Game.__table__.columns]
|
||||||
|
new_game_info = {
|
||||||
|
**{k: v for k, v in vars(game).items() if k in game_fields},
|
||||||
|
**game_info.model_dump()}
|
||||||
|
print(game_fields, new_game_info)
|
||||||
|
game = mdl.Game(**new_game_info)
|
||||||
|
await db.commit()
|
||||||
|
return game
|
||||||
|
|
||||||
|
|
||||||
async def get_games(db: AsyncSession):
|
async def get_games(db: AsyncSession):
|
||||||
return (await db.execute(select(mdl.Game))).scalars().all()
|
return (await db.execute(select(mdl.Game))).scalars().all()
|
||||||
|
|
||||||
|
async def get_game(db: AsyncSession, game_id: int):
|
||||||
async def get_game(db: AsyncSession, game_id: int):
|
return await db.get(mdl.Game, game_id)
|
||||||
return await db.get(mdl.Game, game_id)
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ Base = declarative_base()
|
|||||||
|
|
||||||
async def get_session() -> AsyncSession: # type: ignore
|
async def get_session() -> AsyncSession: # type: ignore
|
||||||
async with async_session() as session: # type: ignore
|
async with async_session() as session: # type: ignore
|
||||||
yield session
|
yield session # type: ignore
|
||||||
|
|
||||||
|
|
||||||
async def drop_all():
|
async def drop_all():
|
||||||
|
|||||||
@@ -1,33 +1,43 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from fastapi import Body
|
||||||
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
|
|
||||||
class GameCardBase(BaseModel):
|
class GameCardBase(BaseModel):
|
||||||
title: str
|
title: str = Field(examples=["DwarfFortress", "RimWorld"])
|
||||||
cover: Optional[str] = None
|
cover: Optional[str] = \
|
||||||
description: Optional[str] = None
|
Field(default=None, examples=["cover_filename.jpg"])
|
||||||
version: Optional[str] = None
|
description: Optional[str] = \
|
||||||
|
Field(default=None,
|
||||||
|
examples=["Dwarf Fortress - это игра, которая"
|
||||||
|
" находится в стадии разработки уже"
|
||||||
|
" довольно долгое время, но уже собрала"
|
||||||
|
" большую базу поклонников и довольно хорошие отзывы"])
|
||||||
|
version: Optional[str] = \
|
||||||
|
Field(default=None, examples=["50.08 (Steam edition)"])
|
||||||
|
|
||||||
|
|
||||||
class GameCard(GameCardBase):
|
class GameCard(GameCardBase):
|
||||||
id: int
|
id: int = Field(examples=[1])
|
||||||
|
|
||||||
|
|
||||||
class GameBase(GameCardBase):
|
class GameBase(GameCardBase):
|
||||||
torrent_file: str
|
torrent_file: str = Field(examples=["torrent_filename.torrent"])
|
||||||
trailer: Optional[str] = None
|
trailer: Optional[str] = \
|
||||||
|
Field(default=None, examples=[
|
||||||
|
"https://www.youtube.com/watch?v=xawsp16oxb0"])
|
||||||
|
|
||||||
system: Optional[str] = None
|
system: Optional[str] = Field(default=None, examples=["Windows"])
|
||||||
processor: Optional[str] = None
|
processor: Optional[str] = \
|
||||||
memory: Optional[str] = None
|
Field(default=None, examples=["Любой (от 2Ghz)"])
|
||||||
graphics: Optional[str] = None
|
memory: Optional[str] = Field(default=None, examples=["512Mb"])
|
||||||
storage: Optional[str] = None
|
graphics: Optional[str] = Field(default=None, examples=["Любая"])
|
||||||
|
storage: Optional[str] = Field(default=None, examples=["100Mb"])
|
||||||
developer: Optional[str] = None
|
|
||||||
language: Optional[str] = None
|
|
||||||
release_date: Optional[str] = None
|
|
||||||
download_size: Optional[str] = None
|
|
||||||
|
|
||||||
|
developer: Optional[str] = Field(default=None, examples=["Bay12Games"])
|
||||||
|
language: Optional[str] = Field(default=None, examples=["eng/рус"])
|
||||||
|
release_date: Optional[str] = Field(default=None, examples=["2014"])
|
||||||
|
download_size: Optional[str] = Field(default=None, examples=["80Mb"])
|
||||||
|
|
||||||
|
|
||||||
class GameCreate(GameBase):
|
class GameCreate(GameBase):
|
||||||
@@ -35,10 +45,9 @@ class GameCreate(GameBase):
|
|||||||
|
|
||||||
|
|
||||||
class Game(GameBase):
|
class Game(GameBase):
|
||||||
id: int
|
id: int = Field(examples=[1])
|
||||||
update_date: str
|
update_date: str = Field(examples=["2024-05-13 12:00:00"])
|
||||||
upload_date: str
|
upload_date: str = Field(examples=["2024-05-13 12:00:00"])
|
||||||
owner_id: int
|
owner_id: int = Field(examples=[1])
|
||||||
|
|
||||||
class Config:
|
model_config = ConfigDict(from_attributes=True)
|
||||||
from_attributes = True
|
|
||||||
|
|||||||
9
main.py
9
main.py
@@ -5,7 +5,14 @@ import typer
|
|||||||
import cli_commands
|
import cli_commands
|
||||||
from routes import *
|
from routes import *
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI(
|
||||||
|
title=".Torrent",
|
||||||
|
description=".Torrent - сервис обмена .torrent файлами видеоигр, фильмов и аудиокниг",
|
||||||
|
contact={
|
||||||
|
"name": "Платон (разработчик)",
|
||||||
|
"url": "https://github.com/StepanovPlaton"
|
||||||
|
},
|
||||||
|
)
|
||||||
app.include_router(startup_router)
|
app.include_router(startup_router)
|
||||||
app.include_router(games_router)
|
app.include_router(games_router)
|
||||||
app.include_router(files_router)
|
app.include_router(files_router)
|
||||||
|
|||||||
@@ -1,37 +1,36 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
|
||||||
from database import *
|
import database as db
|
||||||
from file_handler import *
|
from file_handler import *
|
||||||
|
|
||||||
games_router = APIRouter(prefix="/games", tags=["Games"])
|
games_router = APIRouter(prefix="/games", tags=["Games"])
|
||||||
|
|
||||||
|
|
||||||
@games_router.get("/", response_model=list[Game])
|
@games_router.get("/", response_model=list[db.Game])
|
||||||
async def get_games(db: AsyncSession = Depends(get_session)):
|
async def get_games(db_session: AsyncSession = Depends(db.get_session)):
|
||||||
try:
|
return await db.get_games(db_session)
|
||||||
return await crud.get_games(db)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(500)
|
|
||||||
|
|
||||||
|
|
||||||
@games_router.get("/cards", response_model=list[GameCard])
|
@games_router.get("/cards", response_model=list[db.GameCard])
|
||||||
async def get_games_cards(db: AsyncSession = Depends(get_session)):
|
async def get_games_cards(db_session: AsyncSession = Depends(db.get_session)):
|
||||||
try:
|
return await db.get_games(db_session)
|
||||||
return await crud.get_games(db)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(500)
|
|
||||||
|
|
||||||
|
|
||||||
@games_router.get("/{game_id}", response_model=Game)
|
@games_router.get("/{game_id}", response_model=db.Game)
|
||||||
async def get_game(game_id: int, db: AsyncSession = Depends(get_session)):
|
async def get_game(game_id: int, db_session: AsyncSession = Depends(db.get_session)):
|
||||||
return await crud.get_game(db, game_id)
|
return await db.get_game(db_session, game_id)
|
||||||
|
|
||||||
|
|
||||||
@games_router.post("/", response_model=Game)
|
@games_router.put("/{game_id}", response_model=db.Game)
|
||||||
async def add_game(game: GameCreate,
|
async def edit_game(game_id: int,
|
||||||
|
game: db.GameCreate,
|
||||||
|
db_session: AsyncSession = Depends(db.get_session)):
|
||||||
|
return await db.edit_game(db_session, game_id, game)
|
||||||
|
|
||||||
|
|
||||||
|
@games_router.post("/", response_model=db.Game)
|
||||||
|
async def add_game(game: db.GameCreate,
|
||||||
user_id: int,
|
user_id: int,
|
||||||
db: AsyncSession = Depends(get_session)):
|
db_session: AsyncSession = Depends(db.get_session)):
|
||||||
try:
|
return await db.add_game(db_session, game, user_id)
|
||||||
return await crud.add_game(db, game, user_id)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(500)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user