38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from schemas.contact_form import ContactFormCreate
|
|
from helpers.contact_form_helpers import create_contact_form, validate_contact_form_data
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/email", status_code=status.HTTP_201_CREATED)
|
|
async def submit_contact_form(
|
|
contact_form: ContactFormCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Submit a new contact form with name, email, and message.
|
|
All fields are required and email must be in valid format.
|
|
"""
|
|
# Validate the contact form data
|
|
validation_errors = validate_contact_form_data(contact_form.dict())
|
|
|
|
# If there are validation errors, return a 400 response with the errors
|
|
if validation_errors:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=validation_errors
|
|
)
|
|
|
|
# Create the contact form submission
|
|
new_contact_form = create_contact_form(db=db, contact_form_data=contact_form)
|
|
|
|
return {
|
|
"id": new_contact_form.id,
|
|
"name": new_contact_form.name,
|
|
"email": new_contact_form.email,
|
|
"message": new_contact_form.message,
|
|
"created_at": new_contact_form.created_at,
|
|
"status": "success"
|
|
} |