Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, Depends, HTTPException, Query | |
| from sqlalchemy.orm import Session | |
| from typing import Optional | |
| from crm_api.database import get_db | |
| from crm_api.schemas import ContactCreate, ContactUpdate, ContactResponse, PaginatedResponse | |
| from crm_api.crud import contact_crud | |
| router = APIRouter(prefix="/contacts", tags=["contacts"]) | |
| def create_contact(contact: ContactCreate, db: Session = Depends(get_db)): | |
| return contact_crud.create(db, contact) | |
| def get_contacts( | |
| skip: int = Query(0, ge=0), | |
| limit: int = Query(300, ge=1, le=300), | |
| email: Optional[str] = Query(None, description="Filter contacts by email"), | |
| db: Session = Depends(get_db), | |
| ): | |
| return contact_crud.get_paginated(db, skip=skip, limit=limit, email=email) | |
| def get_contact(contact_id: int, db: Session = Depends(get_db)): | |
| contact = contact_crud.get(db, contact_id) | |
| if not contact: | |
| raise HTTPException(status_code=404, detail="Contact not found") | |
| return contact | |
| def update_contact(contact_id: int, contact: ContactUpdate, db: Session = Depends(get_db)): | |
| return contact_crud.update(db, contact_id, contact) | |
| def delete_contact(contact_id: int, db: Session = Depends(get_db)): | |
| return contact_crud.delete(db, contact_id) | |