diff --git a/endpoints/email4.post.py b/endpoints/email4.post.py index e69de29..75761b1 100644 --- a/endpoints/email4.post.py +++ b/endpoints/email4.post.py @@ -0,0 +1,41 @@ +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 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..ec13e88 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,7 @@ sqlalchemy>=1.4.0 python-dotenv>=0.19.0 bcrypt>=3.2.0 alembic>=1.13.1 +email_validator +jose +passlib +pydantic