chore: update project structure and add testing framework

This commit is contained in:
Davidson Gomes
2025-04-28 20:41:10 -03:00
parent 7af234ef48
commit e7e030dfd5
49 changed files with 1261 additions and 619 deletions

View File

@@ -11,6 +11,7 @@ import logging
logger = logging.getLogger(__name__)
def get_client(db: Session, client_id: uuid.UUID) -> Optional[Client]:
"""Search for a client by ID"""
try:
@@ -23,9 +24,10 @@ def get_client(db: Session, client_id: uuid.UUID) -> Optional[Client]:
logger.error(f"Error searching for client {client_id}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error searching for client"
detail="Error searching for client",
)
def get_clients(db: Session, skip: int = 0, limit: int = 100) -> List[Client]:
"""Search for all clients with pagination"""
try:
@@ -34,9 +36,10 @@ def get_clients(db: Session, skip: int = 0, limit: int = 100) -> List[Client]:
logger.error(f"Error searching for clients: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error searching for clients"
detail="Error searching for clients",
)
def create_client(db: Session, client: ClientCreate) -> Client:
"""Create a new client"""
try:
@@ -51,19 +54,22 @@ def create_client(db: Session, client: ClientCreate) -> Client:
logger.error(f"Error creating client: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error creating client"
detail="Error creating client",
)
def update_client(db: Session, client_id: uuid.UUID, client: ClientCreate) -> Optional[Client]:
def update_client(
db: Session, client_id: uuid.UUID, client: ClientCreate
) -> Optional[Client]:
"""Updates an existing client"""
try:
db_client = get_client(db, client_id)
if not db_client:
return None
for key, value in client.model_dump().items():
setattr(db_client, key, value)
db.commit()
db.refresh(db_client)
logger.info(f"Client updated successfully: {client_id}")
@@ -73,16 +79,17 @@ def update_client(db: Session, client_id: uuid.UUID, client: ClientCreate) -> Op
logger.error(f"Error updating client {client_id}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error updating client"
detail="Error updating client",
)
def delete_client(db: Session, client_id: uuid.UUID) -> bool:
"""Removes a client"""
try:
db_client = get_client(db, client_id)
if not db_client:
return False
db.delete(db_client)
db.commit()
logger.info(f"Client removed successfully: {client_id}")
@@ -92,18 +99,21 @@ def delete_client(db: Session, client_id: uuid.UUID) -> bool:
logger.error(f"Error removing client {client_id}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error removing client"
detail="Error removing client",
)
def create_client_with_user(db: Session, client_data: ClientCreate, user_data: UserCreate) -> Tuple[Optional[Client], str]:
def create_client_with_user(
db: Session, client_data: ClientCreate, user_data: UserCreate
) -> Tuple[Optional[Client], str]:
"""
Creates a new client with an associated user
Args:
db: Database session
client_data: Client data to be created
user_data: User data to be created
Returns:
Tuple[Optional[Client], str]: Tuple with the created client (or None in case of error) and status message
"""
@@ -112,27 +122,27 @@ def create_client_with_user(db: Session, client_data: ClientCreate, user_data: U
client = Client(**client_data.model_dump())
db.add(client)
db.flush() # Get client ID without committing the transaction
# Use client ID to create the associated user
user, message = create_user(db, user_data, is_admin=False, client_id=client.id)
if not user:
# If there was an error creating the user, rollback
db.rollback()
logger.error(f"Error creating user for client: {message}")
return None, f"Error creating user: {message}"
# If everything went well, commit the transaction
db.commit()
logger.info(f"Client and user created successfully: {client.id}")
return client, "Client and user created successfully"
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating client with user: {str(e)}")
return None, f"Error creating client with user: {str(e)}"
except Exception as e:
db.rollback()
logger.error(f"Unexpected error creating client with user: {str(e)}")
return None, f"Unexpected error: {str(e)}"
return None, f"Unexpected error: {str(e)}"