WIP: initial fastapi endpoint implementation
Client endpoint - GET & POST implemented
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
"""
|
||||
from fastapi import Depends
|
||||
from sqlmodel import Session, SQLModel, select
|
||||
from app.core.db import get_session
|
||||
from app.schemas.models import Client
|
||||
from typing import Type, Optional, Annotated
|
||||
|
||||
|
||||
SessionDep = Annotated[Session, Depends(get_session)]
|
||||
|
||||
|
||||
def exists(session: Session, model: Type[SQLModel], **filters) -> Optional[bool]:
|
||||
"""
|
||||
Checks if a request exists in the given model using any filters.
|
||||
|
||||
Example:
|
||||
exists(session, Client, phone="0781232465", tax_number="TIN123")
|
||||
"""
|
||||
if not filters:
|
||||
raise ValueError("At least one filter must be provided")
|
||||
|
||||
stmt = select(model)
|
||||
for field, value in filters.items():
|
||||
if not hasattr(model, field):
|
||||
raise ValueError(f"Invalid filter field: {field}")
|
||||
stmt = stmt.where(getattr(model, field) == value)
|
||||
|
||||
result = session.exec(stmt).first()
|
||||
return result is not None
|
||||
Reference in New Issue
Block a user