80 lines
2.0 KiB
Python
80 lines
2.0 KiB
Python
"""
|
|
Client Repository - Data Access Layer for Client operations
|
|
|
|
Following Python Quick Start Guide:
|
|
- Repository layer contains ONLY database operations
|
|
- No business logic (that goes in services)
|
|
- Simple CRUD operations and queries
|
|
"""
|
|
from typing import Optional, List
|
|
from app.core.database import db
|
|
from models import Client
|
|
|
|
|
|
class ClientRepository:
|
|
"""
|
|
Client repository - handles all Client database operations.
|
|
|
|
Responsibilities:
|
|
- CRUD operations
|
|
- Database queries
|
|
- No business logic
|
|
"""
|
|
|
|
def __init__(self, db_session=None):
|
|
"""Initialize repository with database session."""
|
|
self.db = db_session or db.session
|
|
|
|
def find_by_id(self, client_id_pk: int) -> Optional[Client]:
|
|
"""Find client by primary key ID."""
|
|
return Client.query.get(client_id_pk)
|
|
|
|
def find_by_client_id(self, client_id: str) -> Optional[Client]:
|
|
"""Find client by client_id (OIDC identifier)."""
|
|
return Client.query.filter_by(client_id=client_id).first()
|
|
|
|
def find_all(self) -> List[Client]:
|
|
"""Find all clients."""
|
|
return Client.query.all()
|
|
|
|
def create(self, client: Client) -> Client:
|
|
"""
|
|
Create a new client.
|
|
|
|
Args:
|
|
client: Client object to create
|
|
|
|
Returns:
|
|
Created client with ID
|
|
"""
|
|
self.db.add(client)
|
|
self.db.commit()
|
|
return client
|
|
|
|
def update(self, client: Client) -> Client:
|
|
"""
|
|
Update an existing client.
|
|
|
|
Args:
|
|
client: Client object with updated fields
|
|
|
|
Returns:
|
|
Updated client
|
|
"""
|
|
self.db.commit()
|
|
return client
|
|
|
|
def delete(self, client: Client) -> None:
|
|
"""
|
|
Delete a client.
|
|
|
|
Args:
|
|
client: Client object to delete
|
|
"""
|
|
self.db.delete(client)
|
|
self.db.commit()
|
|
|
|
def rollback(self) -> None:
|
|
"""Rollback current transaction."""
|
|
self.db.rollback()
|