This commit is contained in:
Bernhard Radermacher (hakisto)
2025-08-31 07:53:17 +02:00
parent 5614ecbba6
commit bd510016de
12 changed files with 404 additions and 74 deletions

View File

@@ -1,3 +1,4 @@
import sys
from typing import Annotated
from fastapi import APIRouter, Query, Depends
@@ -18,7 +19,7 @@ PRIMARY_ANNOTATION = utils.make_primary_annotation('Contact')
# Models
class ContactBase(SQLModel):
name: str = Field(max_length=80, unique=True)
code: str = Field(max_length=80, unique=True)
address: str = Field(max_length=253)
notes: str | None
@@ -38,7 +39,7 @@ class ContactPublic(ContactBase):
class ContactUpdate(ContactBase):
name: str | None = None
code: str | None = None
address: str | None = None
notes: str | None = None
@@ -52,9 +53,11 @@ router = APIRouter(prefix="/contact", tags=["contact"])
@router.get("/", response_model=list[ContactPublic])
async def get_contacts(
offset: int = 0,
limit: Annotated[int, Query(le=100)] = 100,
limit: Annotated[int, Query] = 100,
session=Depends(get_session)):
"""Get list of all contacts"""
if limit < 1:
limit = sys.maxsize
return session.exec(select(alchemy.Contact).offset(offset).limit(limit)).all()
@@ -64,11 +67,11 @@ async def get_contacts(
async def get_contact(
contact_id: PRIMARY_ANNOTATION,
session=Depends(get_session)):
result = utils.get_single_record(session, alchemy.Contact, contact_id)
return result
return utils.get_single_record(session, alchemy.Contact, contact_id)
@router.post("/", response_model=ContactPublic)
@router.post("/",
response_model=ContactPublic)
async def create_contact(
contact: ContactCreate,
current_user: ACTIVE_USER,
@@ -80,7 +83,9 @@ async def create_contact(
data=Contact.model_validate(contact))
@router.patch("/{contact_id}", response_model=ContactPublic)
@router.patch("/{contact_id}",
response_model=ContactPublic,
responses={404: {"description": "Not found"}})
async def update_contact(
contact_id: PRIMARY_ANNOTATION,
contact: ContactUpdate,
@@ -93,7 +98,9 @@ async def update_contact(
data=contact)
@router.put("/{contact_id}/activate", response_model=ContactPublic)
@router.put("/{contact_id}/activate",
response_model=ContactPublic,
responses={404: {"description": "Not found"}})
async def activate_contact(
contact_id: PRIMARY_ANNOTATION,
current_user: ACTIVE_USER,
@@ -105,7 +112,9 @@ async def activate_contact(
status='A')
@router.put("/{contact_id}/deactivate", response_model=ContactPublic)
@router.put("/{contact_id}/deactivate",
response_model=ContactPublic,
responses={404: {"description": "Not found"}})
async def deactivate_contact(
contact_id: PRIMARY_ANNOTATION,
current_user: ACTIVE_USER,