mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 20:30:38 +04:00
Add game card
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
content
|
content
|
||||||
dev_database.db
|
dev_database.db
|
||||||
|
run.bat
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from time import strftime
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.future import select
|
from sqlalchemy.future import select
|
||||||
|
|
||||||
@@ -9,7 +10,9 @@ from ..database import add_transaction
|
|||||||
async def add_game(db: AsyncSession,
|
async def add_game(db: AsyncSession,
|
||||||
game_info: sch.GameCreate,
|
game_info: sch.GameCreate,
|
||||||
user_id: int):
|
user_id: int):
|
||||||
game = mdl.Game(**game_info.model_dump(), owner_id=user_id)
|
game = mdl.Game(**game_info.model_dump(),
|
||||||
|
upload_date=strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
owner_id=user_id)
|
||||||
return await add_transaction(db, game)
|
return await add_transaction(db, game)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
1
database/schemas/__init__.py
Normal file
1
database/schemas/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .games import *
|
||||||
@@ -1,15 +1,22 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class GameBase(BaseModel):
|
class GameCardBase(BaseModel):
|
||||||
title: str
|
title: str
|
||||||
cover: str | None = None
|
cover: str | None = None
|
||||||
description: str | None = None
|
description: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class GameCard(GameCardBase):
|
||||||
|
id: int
|
||||||
|
upload_date: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class GameBase(GameCardBase):
|
||||||
torrent_file: str
|
torrent_file: str
|
||||||
language: str | None = None
|
language: str | None = None
|
||||||
version: str | None = None
|
version: str | None = None
|
||||||
download_size: str | None = None
|
download_size: str | None = None
|
||||||
upload_date: str | None = None
|
|
||||||
|
|
||||||
system: str | None = None
|
system: str | None = None
|
||||||
processor: str | None = None
|
processor: str | None = None
|
||||||
@@ -24,24 +31,8 @@ class GameCreate(GameBase):
|
|||||||
|
|
||||||
class Game(GameBase):
|
class Game(GameBase):
|
||||||
id: int
|
id: int
|
||||||
|
upload_date: str | None
|
||||||
owner_id: int
|
owner_id: int
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
from_attributes = True
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
class UserBase(BaseModel):
|
|
||||||
email: str
|
|
||||||
name: str
|
|
||||||
|
|
||||||
|
|
||||||
class UserCreate(UserBase):
|
|
||||||
password: str
|
|
||||||
|
|
||||||
|
|
||||||
class User(UserBase):
|
|
||||||
id: int
|
|
||||||
games: list[Game] = []
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
from_attributes = True
|
|
||||||
@@ -9,11 +9,13 @@ from PIL import Image
|
|||||||
|
|
||||||
|
|
||||||
def create_hash_name(filename: str):
|
def create_hash_name(filename: str):
|
||||||
|
# TODO: Hash from file data
|
||||||
return str(hashlib.sha1(filename.encode()).hexdigest())
|
return str(hashlib.sha1(filename.encode()).hexdigest())
|
||||||
|
|
||||||
|
|
||||||
async def save_torrent_file(torrent: UploadFile):
|
async def save_torrent_file(torrent: UploadFile):
|
||||||
if(torrent.filename is None): raise ValueError("Filename not found")
|
if (torrent.filename is None):
|
||||||
|
raise ValueError("Filename not found")
|
||||||
hash_filename = create_hash_name(torrent.filename)+".torrent"
|
hash_filename = create_hash_name(torrent.filename)+".torrent"
|
||||||
async with aiofiles.open(Path() / "content" / "torrent"
|
async with aiofiles.open(Path() / "content" / "torrent"
|
||||||
/ hash_filename, 'wb') as file:
|
/ hash_filename, 'wb') as file:
|
||||||
@@ -25,13 +27,17 @@ async def save_torrent_file(torrent: UploadFile):
|
|||||||
|
|
||||||
|
|
||||||
async def save_image(cover: UploadFile, type: Literal["cover", "screenshot"]):
|
async def save_image(cover: UploadFile, type: Literal["cover", "screenshot"]):
|
||||||
if(cover.filename is None): raise ValueError("Filename not found")
|
if (cover.filename is None):
|
||||||
if(cover.content_type is None): raise ValueError("File content type unknown")
|
raise ValueError("Filename not found")
|
||||||
|
if (cover.content_type is None):
|
||||||
|
raise ValueError("File content type unknown")
|
||||||
|
|
||||||
hash_filename = create_hash_name(cover.filename)
|
hash_filename = create_hash_name(cover.filename)
|
||||||
file_extension = mimetypes.guess_extension(cover.content_type)
|
file_extension = mimetypes.guess_extension(cover.content_type)
|
||||||
if (file_extension is None): raise NameError("File extension not found")
|
if (file_extension is None):
|
||||||
else: hash_filename += file_extension
|
raise NameError("File extension not found")
|
||||||
|
else:
|
||||||
|
hash_filename += file_extension
|
||||||
|
|
||||||
async with aiofiles.open(Path() / "content" / "images" / type / "full_size"
|
async with aiofiles.open(Path() / "content" / "images" / type / "full_size"
|
||||||
/ hash_filename, 'wb') as full_size_file, \
|
/ hash_filename, 'wb') as full_size_file, \
|
||||||
|
|||||||
@@ -14,6 +14,14 @@ async def get_games(db: AsyncSession = Depends(get_session)):
|
|||||||
raise HTTPException(500)
|
raise HTTPException(500)
|
||||||
|
|
||||||
|
|
||||||
|
@games_router.get("/cards", response_model=list[GameCard])
|
||||||
|
async def get_games_cards(db: AsyncSession = Depends(get_session)):
|
||||||
|
try:
|
||||||
|
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=Game)
|
||||||
async def get_game(game_id: int, db: AsyncSession = Depends(get_session)):
|
async def get_game(game_id: int, db: AsyncSession = Depends(get_session)):
|
||||||
return await crud.get_game(db, game_id)
|
return await crud.get_game(db, game_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user