TT-003: complete request-module integration into modular platform
- integrated request-module into module registry - moved backend into modules/request-module - added manifest.json and module.py - implemented dynamic router mounting - implemented dynamic static mounting - removed direct dependencies from platform core - verified startup with request-module - verified startup without request-module - added implementation_report_003.md
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
"""Repository package."""
|
||||
|
||||
from request_app.repositories.ticket_repository import TicketRepository
|
||||
from request_app.repositories.user_repository import UserRepository
|
||||
|
||||
__all__ = [
|
||||
"TicketRepository",
|
||||
"UserRepository",
|
||||
]
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Base repository."""
|
||||
|
||||
from typing import Type, TypeVar, Optional, List
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class BaseRepository:
|
||||
"""Base repository class."""
|
||||
|
||||
def __init__(self, model: Type[T], db: Session):
|
||||
self.model = model
|
||||
self.db = db
|
||||
|
||||
def get_by_id(self, id: int) -> Optional[T]:
|
||||
"""Get entity by ID."""
|
||||
return self.db.query(self.model).filter(self.model.id == id).first()
|
||||
|
||||
def get_all(
|
||||
self,
|
||||
page: int = 1,
|
||||
page_size: int = 10,
|
||||
**filters
|
||||
) -> tuple[List[T], int]:
|
||||
"""Get all entities with pagination and filters."""
|
||||
query = self.db.query(self.model)
|
||||
|
||||
# Apply filters
|
||||
for attr, value in filters.items():
|
||||
if hasattr(self.model, attr) and value is not None:
|
||||
query = query.filter(getattr(self.model, attr) == value)
|
||||
|
||||
# Get total count
|
||||
total = query.count()
|
||||
|
||||
# Get paginated results
|
||||
entities = query.order_by(self.model.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
|
||||
|
||||
return entities, total
|
||||
|
||||
def create(self, **kwargs) -> T:
|
||||
"""Create new entity."""
|
||||
entity = self.model(**kwargs)
|
||||
self.db.add(entity)
|
||||
self.db.commit()
|
||||
self.db.refresh(entity)
|
||||
return entity
|
||||
|
||||
def update(self, id: int, **kwargs) -> Optional[T]:
|
||||
"""Update entity."""
|
||||
entity = self.get_by_id(id)
|
||||
if entity:
|
||||
for key, value in kwargs.items():
|
||||
if hasattr(entity, key):
|
||||
setattr(entity, key, value)
|
||||
self.db.commit()
|
||||
self.db.refresh(entity)
|
||||
return entity
|
||||
|
||||
def delete(self, id: int) -> bool:
|
||||
"""Delete entity."""
|
||||
entity = self.get_by_id(id)
|
||||
if entity:
|
||||
self.db.delete(entity)
|
||||
self.db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def exists(self, id: int) -> bool:
|
||||
"""Check if entity exists."""
|
||||
return self.db.query(self.model).filter(self.model.id == id).first() is not None
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Ticket repository."""
|
||||
|
||||
from typing import List, Optional, Tuple
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, and_, or_
|
||||
from request_app.models.ticket import Ticket
|
||||
from request_app.repositories.base_repository import BaseRepository
|
||||
|
||||
|
||||
class TicketRepository(BaseRepository):
|
||||
"""Ticket repository."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
super().__init__(Ticket, db)
|
||||
|
||||
def get_tickets_with_joins(
|
||||
self,
|
||||
page: int = 1,
|
||||
page_size: int = 10,
|
||||
status_id: Optional[int] = None,
|
||||
priority_id: Optional[int] = None,
|
||||
assigned_to_id: Optional[int] = None,
|
||||
search: Optional[str] = None
|
||||
) -> Tuple[List[Ticket], int]:
|
||||
"""Get tickets with joins for efficient querying."""
|
||||
query = self.db.query(Ticket)
|
||||
|
||||
# Apply filters
|
||||
if status_id:
|
||||
query = query.filter(Ticket.status_id == status_id)
|
||||
if priority_id:
|
||||
query = query.filter(Ticket.priority_id == priority_id)
|
||||
if assigned_to_id:
|
||||
query = query.filter(Ticket.assigned_to_id == assigned_to_id)
|
||||
|
||||
# Search filter
|
||||
if search:
|
||||
search_term = f"%{search}%"
|
||||
query = query.filter(
|
||||
or_(
|
||||
Ticket.subject.ilike(search_term),
|
||||
Ticket.description.ilike(search_term)
|
||||
)
|
||||
)
|
||||
|
||||
# Get total count
|
||||
total = query.count()
|
||||
|
||||
# Get paginated results
|
||||
tickets = query.order_by(Ticket.created_at.desc()).offset((page - 1) * page_size).limit(page_size).all()
|
||||
|
||||
return tickets, total
|
||||
|
||||
def get_tickets_by_user(self, user_id: int) -> List[Ticket]:
|
||||
"""Get tickets created by user."""
|
||||
return self.db.query(Ticket).filter(Ticket.opened_by == user_id).order_by(Ticket.created_at.desc()).all()
|
||||
|
||||
def get_tickets_assigned_to(self, user_id: int) -> List[Ticket]:
|
||||
"""Get tickets assigned to user."""
|
||||
return self.db.query(Ticket).filter(Ticket.assigned_to_id == user_id).order_by(Ticket.created_at.desc()).all()
|
||||
|
||||
def get_active_tickets_count(self) -> int:
|
||||
"""Get count of active tickets."""
|
||||
return self.db.query(func.count(Ticket.id)).filter(Ticket.status_id != 4).scalar()
|
||||
|
||||
def get_tickets_by_status(self, status_id: int) -> List[Ticket]:
|
||||
"""Get tickets by status."""
|
||||
return self.db.query(Ticket).filter(Ticket.status_id == status_id).order_by(Ticket.created_at.desc()).all()
|
||||
|
||||
def get_tickets_by_priority(self, priority_id: int) -> List[Ticket]:
|
||||
"""Get tickets by priority."""
|
||||
return self.db.query(Ticket).filter(Ticket.priority_id == priority_id).order_by(Ticket.created_at.desc()).all()
|
||||
@@ -0,0 +1,33 @@
|
||||
"""User repository."""
|
||||
|
||||
from typing import List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import or_
|
||||
from request_app.models.user import User
|
||||
from request_app.repositories.base_repository import BaseRepository
|
||||
|
||||
|
||||
class UserRepository(BaseRepository):
|
||||
"""User repository."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
super().__init__(User, db)
|
||||
|
||||
def get_user_by_email(self, email: str) -> Optional[User]:
|
||||
"""Get user by email."""
|
||||
return self.db.query(User).filter(User.email == email).first()
|
||||
|
||||
def get_active_users(self) -> List[User]:
|
||||
"""Get active users."""
|
||||
return self.db.query(User).filter(User.is_active == True).order_by(User.created_at.desc()).all()
|
||||
|
||||
def search_users(self, search_term: str) -> List[User]:
|
||||
"""Search users by name or email."""
|
||||
search_term = f"%{search_term}%"
|
||||
return self.db.query(User).filter(
|
||||
or_(
|
||||
User.first_name.ilike(search_term),
|
||||
User.last_name.ilike(search_term),
|
||||
User.email.ilike(search_term)
|
||||
)
|
||||
).all()
|
||||
Reference in New Issue
Block a user