From 8f0883cc53502f93fa80f3c8b043c11eadf19719 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 28 Apr 2025 12:35:16 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20refactor:=20Enhance=20endpoints/?= =?UTF-8?q?email.post.py=20endpoint=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- endpoints/email.post.py | 25 ++++++++++++++----------- helpers/contact_form_helpers.py | 5 +++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/endpoints/email.post.py b/endpoints/email.post.py index 07937e1..5ee0eb8 100644 --- a/endpoints/email.post.py +++ b/endpoints/email.post.py @@ -1,36 +1,39 @@ from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from core.database import get_db -from schemas.contact_form import ContactFormCreate, ContactFormSchema -from helpers.contact_form_helpers import create_contact_form, validate_contact_form_data +from schemas.contact_form import ContactFormCreate +from helpers.contact_form import create_contact_form, validate_contact_form_data router = APIRouter() -@router.post("/email", status_code=status.HTTP_201_CREATED, response_model=ContactFormSchema) +@router.post("/email", status_code=status.HTTP_201_CREATED) async def submit_contact_form( contact_form: ContactFormCreate, db: Session = Depends(get_db) ): """ - Submit a contact form with name, email, and message. - + Submit a contact form with name, email, and message fields. All fields are required and email must be in valid format. """ # Validate the contact form data - errors = validate_contact_form_data({ + validation_errors = validate_contact_form_data({ "name": contact_form.name, "email": contact_form.email, "message": contact_form.message }) - # If there are validation errors, return a 400 response with the error details - if errors: + # If there are validation errors, return 400 with error details + if validation_errors: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail=errors + detail=validation_errors ) # Create the contact form in the database - new_contact_form = create_contact_form(db=db, contact_form_data=contact_form) + created_form = create_contact_form(db=db, contact_form_data=contact_form) - return new_contact_form \ No newline at end of file + return { + "status": "success", + "message": "Contact form submitted successfully", + "id": created_form.id + } \ No newline at end of file diff --git a/helpers/contact_form_helpers.py b/helpers/contact_form_helpers.py index f75199c..4342e73 100644 --- a/helpers/contact_form_helpers.py +++ b/helpers/contact_form_helpers.py @@ -35,6 +35,7 @@ def create_contact_form(db: Session, contact_form_data: ContactFormCreate) -> Co def validate_contact_form_data(data: Dict[str, Any]) -> Dict[str, str]: """ Validates contact form data manually, checking for required fields and valid email format. + Uses email_validator package for email validation. Args: data (Dict[str, Any]): The contact form data to validate. @@ -54,8 +55,8 @@ def validate_contact_form_data(data: Dict[str, Any]) -> Dict[str, str]: try: # Validate email format using email_validator package validate_email(data["email"]) - except EmailNotValidError: - errors["email"] = "Email format is invalid" + except EmailNotValidError as e: + errors["email"] = f"Email validation failed: {str(e)}" if not data.get("message"): errors["message"] = "Message is required"