Files
vpsx-fast/app/routers/contact.py

103 lines
3.0 KiB
Python
Raw Normal View History

2025-08-31 07:53:17 +02:00
import sys
2025-08-30 10:06:47 +02:00
from typing import Annotated
from fastapi import APIRouter, Query, Depends
from sqlmodel import SQLModel, Field
from sqlmodel import select
import alchemy
import utils
from dependencies import get_session
from utils import update_item, create_item
from .user import ACTIVE_USER
2025-09-01 11:36:22 +00:00
from .models import Contact, ContactCreate, ContactUpdate
2025-08-30 10:06:47 +02:00
2025-09-01 11:36:22 +00:00
PRIMARY_ANNOTATION = utils.make_primary_annotation('Contact')
2025-08-30 10:06:47 +02:00
# ----------------------------------------------------------------
# Routes
router = APIRouter(prefix="/contact", tags=["contact"])
2025-09-01 11:36:22 +00:00
@router.get("/",
response_model=list[Contact])
2025-08-30 10:06:47 +02:00
async def get_contacts(
offset: int = 0,
2025-08-31 07:53:17 +02:00
limit: Annotated[int, Query] = 100,
2025-08-30 10:06:47 +02:00
session=Depends(get_session)):
2025-09-01 11:36:22 +00:00
"""Get list of all Contacts"""
return session.exec(
select(
alchemy.Contact
).offset(offset).limit(limit or sys.maxsize)).all()
2025-08-30 10:06:47 +02:00
2025-09-01 11:36:22 +00:00
# noinspection PyTypeHints
@router.get("/{contact}",
response_model=Contact,
2025-08-30 10:06:47 +02:00
responses={404: {"description": "Not found"}})
async def get_contact(
2025-09-01 11:36:22 +00:00
contact: PRIMARY_ANNOTATION,
2025-08-30 10:06:47 +02:00
session=Depends(get_session)):
2025-09-01 11:36:22 +00:00
return utils.get_single_record(session, alchemy.Contact, 'Contact', contact)
2025-08-30 10:06:47 +02:00
2025-09-01 11:36:22 +00:00
# noinspection PyTypeHints
2025-08-31 07:53:17 +02:00
@router.post("/",
2025-09-01 11:36:22 +00:00
response_model=Contact)
2025-08-30 10:06:47 +02:00
async def create_contact(
2025-09-01 11:36:22 +00:00
data: ContactCreate,
2025-08-30 10:06:47 +02:00
current_user: ACTIVE_USER,
session=Depends(get_session)):
return create_item(
session=session,
cls=alchemy.Contact,
2025-09-01 11:36:22 +00:00
name='Contact',
2025-08-30 10:06:47 +02:00
current_user=current_user,
2025-09-01 11:36:22 +00:00
data=data)
2025-08-30 10:06:47 +02:00
2025-09-01 11:36:22 +00:00
@router.patch("/{contact}",
response_model=Contact,
2025-08-31 07:53:17 +02:00
responses={404: {"description": "Not found"}})
2025-08-30 10:06:47 +02:00
async def update_contact(
2025-09-01 11:36:22 +00:00
contact: PRIMARY_ANNOTATION,
data: ContactUpdate,
2025-08-30 10:06:47 +02:00
current_user: ACTIVE_USER,
session=Depends(get_session)):
return update_item(
session=session,
current_user=current_user,
2025-09-01 11:36:22 +00:00
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
data=data)
2025-08-30 10:06:47 +02:00
2025-09-01 11:36:22 +00:00
@router.put("/{contact}/activate",
response_model=Contact,
responses={404: {"description": "Not found"}})
2025-08-30 10:06:47 +02:00
async def activate_contact(
2025-09-01 11:36:22 +00:00
contact: PRIMARY_ANNOTATION,
2025-08-30 10:06:47 +02:00
current_user: ACTIVE_USER,
session=Depends(get_session)):
return utils.set_item_status(
session=session,
current_user=current_user,
2025-09-01 11:36:22 +00:00
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
2025-08-30 10:06:47 +02:00
status='A')
2025-09-01 11:36:22 +00:00
@router.put("/{contact}/deactivate",
response_model=Contact,
responses={404: {"description": "Not found"}})
2025-08-30 10:06:47 +02:00
async def deactivate_contact(
2025-09-01 11:36:22 +00:00
contact: PRIMARY_ANNOTATION,
2025-08-30 10:06:47 +02:00
current_user: ACTIVE_USER,
session=Depends(get_session)):
return utils.set_item_status(
session=session,
current_user=current_user,
2025-09-01 11:36:22 +00:00
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
2025-08-30 10:06:47 +02:00
status='I')