mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 20:30:38 +04:00
Add authorization
This commit is contained in:
@@ -1 +1,2 @@
|
||||
from .games import *
|
||||
from .users import *
|
||||
|
||||
@@ -7,6 +7,14 @@ from .. import schemas as sch
|
||||
from ..database import add_transaction
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
async def add_game(db: AsyncSession,
|
||||
game_info: sch.GameCreate,
|
||||
user_id: int):
|
||||
@@ -21,18 +29,16 @@ 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)
|
||||
for key, value in vars(game_info).items():
|
||||
if (value and value is not None and getattr(game, key) != value):
|
||||
setattr(game, key, value)
|
||||
await db.commit()
|
||||
return 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)
|
||||
async def delete_game(db: AsyncSession,
|
||||
game_id: int):
|
||||
game = await get_game(db, game_id)
|
||||
await db.delete(game)
|
||||
await db.commit()
|
||||
return game
|
||||
|
||||
26
database/crud/users.py
Normal file
26
database/crud/users.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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 get_user(db: AsyncSession, username: str):
|
||||
return (await db.execute(select(mdl.User).where(mdl.User.name == username))).scalar()
|
||||
|
||||
|
||||
async def add_user(db: AsyncSession,
|
||||
user_data: sch.UserCreate, hash_of_password: str):
|
||||
user_data_db = \
|
||||
{k: v for k, v in user_data.model_dump().items()
|
||||
if k != "password"}
|
||||
user = mdl.User(**user_data_db,
|
||||
hash_of_password=hash_of_password)
|
||||
return await add_transaction(db, user)
|
||||
|
||||
|
||||
async def check_email(db: AsyncSession, email: str):
|
||||
users = (await db.execute(select(mdl.User)
|
||||
.where(mdl.User.email == email))).scalars().all()
|
||||
return True if len(users) == 0 else False
|
||||
@@ -2,7 +2,9 @@ from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
|
||||
DATABASE_URL = "sqlite+aiosqlite:///./dev_database.db"
|
||||
from env import Env
|
||||
|
||||
DATABASE_URL = Env.get_strict("SQLALCHEMY_DATABASE_URL", str)
|
||||
# DATABASE_URL = "postgresql://user:password@postgresserver/db"
|
||||
|
||||
engine = create_async_engine(
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
from .games import *
|
||||
from .users import *
|
||||
|
||||
19
database/schemas/users.py
Normal file
19
database/schemas/users.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from typing import Optional
|
||||
from fastapi import Body
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: str = Field(examples=["email@gmail.com"])
|
||||
name: str = Field(examples=["username"])
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str = Field(examples=["password"])
|
||||
|
||||
|
||||
class User(UserBase):
|
||||
id: int = Field(examples=[1])
|
||||
hash_of_password: str = Field(examples=["hash_of_password"])
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
Reference in New Issue
Block a user