wip
This commit is contained in:
@@ -9,118 +9,94 @@ import alchemy
|
||||
import utils
|
||||
from dependencies import get_session
|
||||
from utils import update_item, create_item
|
||||
from .status_model import Status
|
||||
from .user import ACTIVE_USER
|
||||
from .models import Contact, ContactCreate, ContactUpdate
|
||||
|
||||
|
||||
PRIMARY_ANNOTATION = utils.make_primary_annotation('Contact')
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Models
|
||||
|
||||
class ContactBase(SQLModel):
|
||||
code: str = Field(max_length=80, unique=True)
|
||||
address: str = Field(max_length=253)
|
||||
notes: str | None
|
||||
|
||||
|
||||
class Contact(ContactBase):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
|
||||
class ContactCreate(ContactBase):
|
||||
address: str | None = None
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
class ContactPublic(ContactBase):
|
||||
id: int
|
||||
status: Status
|
||||
|
||||
|
||||
class ContactUpdate(ContactBase):
|
||||
code: str | None = None
|
||||
address: str | None = None
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Routes
|
||||
|
||||
router = APIRouter(prefix="/contact", tags=["contact"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[ContactPublic])
|
||||
@router.get("/",
|
||||
response_model=list[Contact])
|
||||
async def get_contacts(
|
||||
offset: int = 0,
|
||||
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()
|
||||
"""Get list of all Contacts"""
|
||||
return session.exec(
|
||||
select(
|
||||
alchemy.Contact
|
||||
).offset(offset).limit(limit or sys.maxsize)).all()
|
||||
|
||||
|
||||
@router.get("/{contact_id}",
|
||||
response_model=ContactPublic,
|
||||
# noinspection PyTypeHints
|
||||
@router.get("/{contact}",
|
||||
response_model=Contact,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def get_contact(
|
||||
contact_id: PRIMARY_ANNOTATION,
|
||||
contact: PRIMARY_ANNOTATION,
|
||||
session=Depends(get_session)):
|
||||
return utils.get_single_record(session, alchemy.Contact, contact_id)
|
||||
return utils.get_single_record(session, alchemy.Contact, 'Contact', contact)
|
||||
|
||||
|
||||
# noinspection PyTypeHints
|
||||
@router.post("/",
|
||||
response_model=ContactPublic)
|
||||
response_model=Contact)
|
||||
async def create_contact(
|
||||
contact: ContactCreate,
|
||||
data: ContactCreate,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return create_item(
|
||||
session=session,
|
||||
cls=alchemy.Contact,
|
||||
name='Contact',
|
||||
current_user=current_user,
|
||||
data=Contact.model_validate(contact))
|
||||
data=data)
|
||||
|
||||
|
||||
@router.patch("/{contact_id}",
|
||||
response_model=ContactPublic,
|
||||
@router.patch("/{contact}",
|
||||
response_model=Contact,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def update_contact(
|
||||
contact_id: PRIMARY_ANNOTATION,
|
||||
contact: ContactUpdate,
|
||||
contact: PRIMARY_ANNOTATION,
|
||||
data: ContactUpdate,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return update_item(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Contact, contact_id),
|
||||
data=contact)
|
||||
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
|
||||
data=data)
|
||||
|
||||
|
||||
@router.put("/{contact_id}/activate",
|
||||
response_model=ContactPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
@router.put("/{contact}/activate",
|
||||
response_model=Contact,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def activate_contact(
|
||||
contact_id: PRIMARY_ANNOTATION,
|
||||
contact: PRIMARY_ANNOTATION,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return utils.set_item_status(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Contact, contact_id),
|
||||
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
|
||||
status='A')
|
||||
|
||||
|
||||
@router.put("/{contact_id}/deactivate",
|
||||
response_model=ContactPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
@router.put("/{contact}/deactivate",
|
||||
response_model=Contact,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def deactivate_contact(
|
||||
contact_id: PRIMARY_ANNOTATION,
|
||||
contact: PRIMARY_ANNOTATION,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return utils.set_item_status(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Contact, contact_id),
|
||||
item=utils.get_single_record(session, alchemy.Contact, 'Contact', contact),
|
||||
status='I')
|
||||
|
||||
Reference in New Issue
Block a user