41 lines
1.3 KiB
Python
41 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.contactform import ContactFormCreate, ContactFormSchema
|
|
from helpers.contactform_helpers import (
|
|
validate_contact_form_data,
|
|
create_contact_form,
|
|
format_validation_error_response,
|
|
send_notification_email
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/email4", status_code=status.HTTP_201_CREATED, response_model=ContactFormSchema)
|
|
async def submit_contact_form(
|
|
form_data: ContactFormCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Handle contact form submissions with validation for required fields and email format.
|
|
"""
|
|
# Validate the form data
|
|
errors = validate_contact_form_data(form_data.dict())
|
|
|
|
# If there are validation errors, return a 400 response
|
|
if errors:
|
|
error_response = format_validation_error_response(errors)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=error_response
|
|
)
|
|
|
|
# Create the contact form in the database
|
|
contact_form = create_contact_form(db=db, form_data=form_data)
|
|
|
|
# Send notification email (optional step)
|
|
send_notification_email(contact_form)
|
|
|
|
# Return the created contact form
|
|
return contact_form |