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 sqlalchemy.orm import Session
|
||||||
|
from typing import Dict, Any
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
from schemas.contact_form import ContactFormCreate
|
|
||||||
from helpers.contact_form_helpers import handle_contact_form_submission
|
from helpers.contact_form_helpers import handle_contact_form_submission
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/contact", status_code=status.HTTP_201_CREATED)
|
@router.post("/contact", status_code=status.HTTP_201_CREATED)
|
||||||
async def submit_contact_form(
|
async def submit_contact_form(
|
||||||
contact_form: ContactFormCreate,
|
contact_form: Dict[str, Any],
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Submit a contact form with name, email, and message.
|
Submit a contact form with name, email, and message.
|
||||||
All fields are required and email must be in valid format.
|
All fields are required and email must be in valid format.
|
||||||
"""
|
"""
|
||||||
# Convert Pydantic model to dict for processing
|
try:
|
||||||
form_data = contact_form.dict()
|
result = handle_contact_form_submission(db=db, form_data=contact_form)
|
||||||
|
return result
|
||||||
# Process the form submission using the helper function
|
except HTTPException:
|
||||||
# This will validate fields and handle database operations
|
# Re-raise any HTTPExceptions from the helper function
|
||||||
result = handle_contact_form_submission(db, form_data)
|
raise
|
||||||
|
except Exception as e:
|
||||||
return result
|
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
|
- 'error': Error message if validation fails, None otherwise
|
||||||
"""
|
"""
|
||||||
# Check for required fields
|
# 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'}
|
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'}
|
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'}
|
return {'valid': False, 'error': 'Message is required'}
|
||||||
|
|
||||||
# Validate email format
|
# Validate email format
|
||||||
try:
|
try:
|
||||||
validate_email(form_data['email'])
|
validate_email(form_data['email'])
|
||||||
except EmailNotValidError:
|
except EmailNotValidError as e:
|
||||||
return {'valid': False, 'error': 'Invalid email format'}
|
return {'valid': False, 'error': f'Invalid email format: {str(e)}'}
|
||||||
|
|
||||||
return {'valid': True, 'error': None}
|
return {'valid': True, 'error': None}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import BaseModel, EmailStr, Field
|
||||||
from typing import Optional
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@ -13,11 +12,11 @@ class ContactFormBase(BaseModel):
|
|||||||
class ContactFormCreate(ContactFormBase):
|
class ContactFormCreate(ContactFormBase):
|
||||||
pass
|
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):
|
class ContactFormUpdate(BaseModel):
|
||||||
name: Optional[str] = Field(None, description="Contact's name")
|
name: str = Field(..., description="Contact's name")
|
||||||
email: Optional[EmailStr] = Field(None, description="Contact's email address")
|
email: EmailStr = Field(..., description="Contact's email address")
|
||||||
message: Optional[str] = Field(None, description="Contact message")
|
message: str = Field(..., description="Contact message")
|
||||||
|
|
||||||
# Schema for representing a ContactForm in responses
|
# Schema for representing a ContactForm in responses
|
||||||
class ContactFormSchema(ContactFormBase):
|
class ContactFormSchema(ContactFormBase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user