Commit 460139e0 authored by ZeinabRm13's avatar ZeinabRm13

Enable Register

parent ebbdeac4
...@@ -2,12 +2,26 @@ from fastapi import FastAPI ...@@ -2,12 +2,26 @@ from fastapi import FastAPI
from src.infrastructure.api.fastapi.routes import auth, charts from src.infrastructure.api.fastapi.routes import auth, charts
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
app = FastAPI( from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
app = FastAPI(debug = "True",
title="Chart Analyzer API", title="Chart Analyzer API",
description="API for analyzing charts and managing users.", description="API for analyzing charts and managing users.",
version="1.0.0", version="1.0.0",
) )
@app.on_event("startup")
async def startup_event():
engine = create_engine("postgresql+psycopg://chart_analyzer_user:chartanalyzer13@localhost:5432/chart_analyzer")
try:
with engine.connect() as conn:
print("✅ Database connection successful")
print(f"🔗 Connection string: postgresql+psycopg://chart_analyzer_user:chartanalyzer13@localhost:5432/chart_analyzer")
except OperationalError as e:
print("❌ Database connection failed")
print(f"Error: {e}")
# Configure CORS # Configure CORS
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
......
...@@ -4,6 +4,7 @@ from jose import jwt ...@@ -4,6 +4,7 @@ from jose import jwt
from passlib.context import CryptContext from passlib.context import CryptContext
from src.domain.ports.repositories.user_repository import UserRepositoryPort from src.domain.ports.repositories.user_repository import UserRepositoryPort
from src.domain.entities.user import User from src.domain.entities.user import User
from src.config import Settings
import uuid import uuid
from src.application.ports.authentication_service_port import AuthServicePort from src.application.ports.authentication_service_port import AuthServicePort
from src.application.dtos.authentication import ( from src.application.dtos.authentication import (
...@@ -12,12 +13,12 @@ from src.application.dtos.authentication import ( ...@@ -12,12 +13,12 @@ from src.application.dtos.authentication import (
UserResponseDTO, UserResponseDTO,
TokenResponseDTO TokenResponseDTO
) )
settings = Settings()
class AuthService(AuthServicePort): class AuthService(AuthServicePort):
def __init__( def __init__(
self, self,
user_repo: UserRepositoryPort, user_repo: UserRepositoryPort,
secret_key: str, secret_key: str = settings.JWT_SECRET,
algorithm: str = "HS256", algorithm: str = "HS256",
expires_minutes: int = 30 expires_minutes: int = 30
): ):
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
from pydantic import ConfigDict from pydantic import ConfigDict
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
class Settings(BaseSettings): class Settings(BaseSettings):
DATABASE_URL: str = "postgresql+psycopg://chart_analyzer_user:chartanalyzer13@localhost:5432/chart_analyzer" DATABASE_URL: str = "postgresql+psycopg://chart_analyzer_user:chartanalyzer13@localhost:5432/chart_analyzer"
JWT_SECRET: str = "a_very_secret_key" JWT_SECRET: str = "zKReJQaoTK_F9Y2EIkDKZS1hgnfOZsplgzbjXY7IWyc"
JWT_ALGORITHM: str = "HS256" JWT_ALGORITHM: str = "HS256"
JWT_EXPIRE_MINUTES: int = 30 JWT_EXPIRE_MINUTES: int = 30
......
# src/infrastructure/adapters/sqlserver/mappers/mappers.py
from src.domain.entities.user import User as UserEntity
from src.infrastructure.adapters.sqlserver.models import User as UserModel
def user_model_to_entity(model: UserModel) -> UserEntity:
return UserEntity(
id=str(model.id),
email=model.email,
password_hash=model.password_hash,
is_active=model.is_active,
created_at=model.created_at,
last_login=model.last_login
)
def user_entity_to_model(entity: UserEntity) -> UserModel:
return UserModel(
id=entity.id,
email=entity.email,
password_hash=entity.password_hash,
is_active=entity.is_active,
created_at=entity.created_at,
last_login=entity.last_login
)
\ No newline at end of file
# src/infrastructure/adapters/sql_user_repository.py # src/infrastructure/adapters/sqlserver/sql_user_repository.py
from sqlalchemy import select from sqlalchemy import select
from src.domain.ports import UserRepositoryPort
from src.domain.entities.user import User
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from pydantic import EmailStr from src.domain.ports import UserRepositoryPort
from src.infrastructure.adapters.sqlserver.models import User as UserModel
from src.domain.entities.user import User as UserEntity
from src.infrastructure.adapters.sqlserver.mappers.mappers import user_model_to_entity, user_entity_to_model
class SqlUserRepository(UserRepositoryPort): class SqlUserRepository(UserRepositoryPort):
def __init__(self, session: AsyncSession): def __init__(self, session: AsyncSession):
self._session = session self._session = session
async def get_by_email(self, email: EmailStr) -> User | None: async def get_by_email(self, email: str) -> UserEntity | None:
result = await self._session.execute( result = await self._session.execute(
select(User).where(User.email == email)) select(UserModel).where(UserModel.email == email)
return result.scalar_one_or_none() )
model = result.scalar_one_or_none()
return user_model_to_entity(model) if model else None
async def create_user(self, user: User) -> None: async def create_user(self, user: UserEntity) -> UserEntity:
self._session.add(user) model = user_entity_to_model(user)
await self._session.commit() self._session.add(model)
\ No newline at end of file await self._session.commit()
await self._session.refresh(model)
return user_model_to_entity(model)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment