60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth import get_current_admin
|
|
from app.database import get_db
|
|
from app import models, schemas
|
|
|
|
router = APIRouter(prefix="/api/contact", tags=["Contact"])
|
|
|
|
|
|
@router.post("", response_model=schemas.ContactOut, status_code=status.HTTP_201_CREATED)
|
|
def submit_contact(payload: schemas.ContactCreate, db: Session = Depends(get_db)):
|
|
"""این اندپوینت عمومی است — فرم تماس سایت آن را صدا میزند."""
|
|
lead = models.ContactLead(**payload.model_dump())
|
|
db.add(lead)
|
|
db.commit()
|
|
db.refresh(lead)
|
|
return lead
|
|
|
|
|
|
@router.get("", response_model=list[schemas.ContactOut])
|
|
def list_contacts(
|
|
db: Session = Depends(get_db),
|
|
_admin=Depends(get_current_admin),
|
|
):
|
|
"""فقط ادمین: لیست همه درخواستهای تماس، جدیدترین اول."""
|
|
return db.query(models.ContactLead).order_by(models.ContactLead.created_at.desc()).all()
|
|
|
|
|
|
@router.put("/{lead_id}", response_model=schemas.ContactOut)
|
|
def update_contact(
|
|
lead_id: int,
|
|
payload: schemas.ContactUpdate,
|
|
db: Session = Depends(get_db),
|
|
_admin=Depends(get_current_admin),
|
|
):
|
|
"""فقط ادمین: مثلاً علامت زدن بهعنوان خواندهشده."""
|
|
lead = db.get(models.ContactLead, lead_id)
|
|
if not lead:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, "درخواست پیدا نشد")
|
|
for field, value in payload.model_dump(exclude_unset=True).items():
|
|
setattr(lead, field, value)
|
|
db.commit()
|
|
db.refresh(lead)
|
|
return lead
|
|
|
|
|
|
@router.delete("/{lead_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_contact(
|
|
lead_id: int,
|
|
db: Session = Depends(get_db),
|
|
_admin=Depends(get_current_admin),
|
|
):
|
|
lead = db.get(models.ContactLead, lead_id)
|
|
if not lead:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, "درخواست پیدا نشد")
|
|
db.delete(lead)
|
|
db.commit()
|
|
return None
|