46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from sqlalchemy.orm import Session
|
|
from sqlalchemy.exc import IntegrityError
|
|
from pydantic import ValidationError
|
|
from models.contact_form import ContactForm
|
|
from schemas.contact_form import ContactFormCreate
|
|
|
|
def create_contact_form(db: Session, contact_form_data: ContactFormCreate) -> ContactForm:
|
|
"""
|
|
Create a new contact form submission in the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
contact_form_data (ContactFormCreate): The data for the contact form submission.
|
|
|
|
Returns:
|
|
ContactForm: The newly created contact form submission object.
|
|
|
|
Raises:
|
|
ValidationError: If the input data is invalid.
|
|
IntegrityError: If there is a database integrity issue.
|
|
"""
|
|
try:
|
|
db_contact_form = ContactForm(**contact_form_data.dict())
|
|
db.add(db_contact_form)
|
|
db.commit()
|
|
db.refresh(db_contact_form)
|
|
return db_contact_form
|
|
except ValidationError as e:
|
|
raise e
|
|
except IntegrityError as e:
|
|
db.rollback()
|
|
raise e
|
|
|
|
def validate_contact_form_data(contact_form_data: ContactFormCreate) -> bool:
|
|
"""
|
|
Validate the input data for the contact form submission.
|
|
|
|
Args:
|
|
contact_form_data (ContactFormCreate): The data for the contact form submission.
|
|
|
|
Returns:
|
|
bool: True if the input data is valid, False otherwise.
|
|
"""
|
|
if not contact_form_data.name or not contact_form_data.email or not contact_form_data.message:
|
|
return False
|
|
return True |