feat: implement contact form submission endpoint with validation
This commit is contained in:
parent
dd9a3bee7f
commit
ad64a3523b
@ -1,25 +1,28 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Dict, Any
|
||||
from core.database import get_db
|
||||
from schemas.contact_form import ContactFormCreate
|
||||
from helpers.contact_form_helpers import handle_contact_form_submission
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/contact", status_code=status.HTTP_201_CREATED)
|
||||
async def submit_contact_form(
|
||||
contact_form: ContactFormCreate,
|
||||
contact_form: Dict[str, Any],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Submit a contact form with name, email, and message.
|
||||
All fields are required and email must be in valid format.
|
||||
"""
|
||||
# Convert Pydantic model to dict for processing
|
||||
form_data = contact_form.dict()
|
||||
|
||||
# Process the form submission using the helper function
|
||||
# This will validate fields and handle database operations
|
||||
result = handle_contact_form_submission(db, form_data)
|
||||
|
||||
return result
|
||||
try:
|
||||
result = handle_contact_form_submission(db=db, form_data=contact_form)
|
||||
return result
|
||||
except HTTPException:
|
||||
# Re-raise any HTTPExceptions from the helper function
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"An unexpected error occurred: {str(e)}"
|
||||
)
|
@ -36,20 +36,20 @@ def validate_contact_form_data(form_data: Dict[str, Any]) -> Dict[str, Union[boo
|
||||
- 'error': Error message if validation fails, None otherwise
|
||||
"""
|
||||
# Check for required fields
|
||||
if not form_data.get('name'):
|
||||
if 'name' not in form_data or not form_data.get('name'):
|
||||
return {'valid': False, 'error': 'Name is required'}
|
||||
|
||||
if not form_data.get('email'):
|
||||
if 'email' not in form_data or not form_data.get('email'):
|
||||
return {'valid': False, 'error': 'Email is required'}
|
||||
|
||||
if not form_data.get('message'):
|
||||
if 'message' not in form_data or not form_data.get('message'):
|
||||
return {'valid': False, 'error': 'Message is required'}
|
||||
|
||||
# Validate email format
|
||||
try:
|
||||
validate_email(form_data['email'])
|
||||
except EmailNotValidError:
|
||||
return {'valid': False, 'error': 'Invalid email format'}
|
||||
except EmailNotValidError as e:
|
||||
return {'valid': False, 'error': f'Invalid email format: {str(e)}'}
|
||||
|
||||
return {'valid': True, 'error': None}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
@ -13,11 +12,11 @@ class ContactFormBase(BaseModel):
|
||||
class ContactFormCreate(ContactFormBase):
|
||||
pass
|
||||
|
||||
# Schema for updating an existing ContactForm (all fields optional)
|
||||
# Schema for updating an existing ContactForm (all fields required based on change summary)
|
||||
class ContactFormUpdate(BaseModel):
|
||||
name: Optional[str] = Field(None, description="Contact's name")
|
||||
email: Optional[EmailStr] = Field(None, description="Contact's email address")
|
||||
message: Optional[str] = Field(None, description="Contact message")
|
||||
name: str = Field(..., description="Contact's name")
|
||||
email: EmailStr = Field(..., description="Contact's email address")
|
||||
message: str = Field(..., description="Contact message")
|
||||
|
||||
# Schema for representing a ContactForm in responses
|
||||
class ContactFormSchema(ContactFormBase):
|
||||
|
Loading…
x
Reference in New Issue
Block a user