add singup endpoint
This commit is contained in:
@@ -4,10 +4,11 @@ from fastapi import Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.core.database import async_session_maker
|
||||
from src.utils.db_manager import DBManager
|
||||
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with async_session_maker() as db:
|
||||
async with DBManager(async_session_maker) as db:
|
||||
yield db
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from src.api.dependacies.db_dep import sessionDep
|
||||
from src.schemas.users import UserCreate
|
||||
from src.schemas.users import UserRequest
|
||||
from src.core.settings import settings
|
||||
from src.services.auth import AuthService
|
||||
|
||||
@@ -9,5 +9,5 @@ router = APIRouter(prefix=settings.api.v1.auth, tags=['Auth'])
|
||||
|
||||
|
||||
@router.post(path='/signup')
|
||||
async def registration(session: sessionDep, credential: UserCreate):
|
||||
async def registration(session: sessionDep, credential: UserRequest):
|
||||
await AuthService(session).registration(credential)
|
||||
|
||||
@@ -3,7 +3,8 @@ from pathlib import Path
|
||||
from pydantic import BaseModel
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
BASE_DIR = Path(__file__).parent
|
||||
BASE_DIR = Path(__file__).parent.parent
|
||||
print(BASE_DIR)
|
||||
DB_PATH = BASE_DIR / "db/taskncoffee.db"
|
||||
|
||||
|
||||
@@ -39,3 +40,4 @@ class Settings(BaseSettings):
|
||||
|
||||
|
||||
settings = Settings()
|
||||
print(settings.db.url)
|
||||
@@ -1,3 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import insert
|
||||
|
||||
from src.core.database import Base
|
||||
|
||||
|
||||
@@ -6,3 +9,9 @@ class BaseRepo:
|
||||
|
||||
def __init__(self, session):
|
||||
self.session = session
|
||||
|
||||
async def create_one(self, data: BaseModel):
|
||||
print(self.session)
|
||||
statement = insert(self.model).values(data.model_dump()).returning(self.model)
|
||||
result = await self.session.execute(statement)
|
||||
return result
|
||||
|
||||
6
src/repository/users.py
Normal file
6
src/repository/users.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from src.models import UsersORM
|
||||
from src.repository.base import BaseRepo
|
||||
|
||||
|
||||
class UsersRepo(BaseRepo):
|
||||
model = UsersORM
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ from pydantic import BaseModel, EmailStr, ConfigDict
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: int
|
||||
email: EmailStr | None
|
||||
username: str
|
||||
is_active: bool
|
||||
is_superuser: bool
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class UserWithHashedPass(User):
|
||||
hashed_password: str
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
class UserRequest(BaseModel):
|
||||
username: str
|
||||
email: EmailStr | None = None
|
||||
password: str
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
from src.schemas.users import UserCreate
|
||||
from src.schemas.users import UserWithHashedPass, UserRequest
|
||||
from src.services.base import BaseService
|
||||
from src.utils.auth_manager import AuthManger
|
||||
|
||||
|
||||
class AuthService(BaseService):
|
||||
|
||||
async def registration(self, cred: UserCreate):
|
||||
async def registration(self, cred: UserRequest):
|
||||
hashed_pass = AuthManger.get_password_hash(cred.password)
|
||||
print(cred.password)
|
||||
print(hashed_pass)
|
||||
user_to_insert = UserWithHashedPass(
|
||||
username=cred.username,
|
||||
email=cred.email,
|
||||
hashed_password=hashed_pass,
|
||||
is_active=True,
|
||||
is_superuser=False
|
||||
)
|
||||
result = await self.session.user.create_one(user_to_insert)
|
||||
await self.session.commit()
|
||||
return result
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from src.repository.users import UsersRepo
|
||||
|
||||
|
||||
class DBManager:
|
||||
@@ -6,6 +7,8 @@ class DBManager:
|
||||
|
||||
async def __aenter__(self):
|
||||
self.session = self.session_factory()
|
||||
self.user = UsersRepo(self.session)
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
await self.session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user