diff --git a/alembic/versions/20250414_193713_8afec105_update_contactform.py b/alembic/versions/20250414_193713_8afec105_update_contactform.py new file mode 100644 index 0000000..95da021 --- /dev/null +++ b/alembic/versions/20250414_193713_8afec105_update_contactform.py @@ -0,0 +1,32 @@ +"""create table for contactforms +Revision ID: 2a3b4c5d6e7f +Revises: 0001 +Create Date: 2023-05-22 14:39:12.123456 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func + + +# revision identifiers, used by Alembic. +revision = '2a3b4c5d6e7f' +down_revision = '0001' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'contactforms', + sa.Column('id', sa.String(36), primary_key=True), + sa.Column('name', sa.String(), nullable=False), + sa.Column('email', sa.String(), nullable=False), + sa.Column('message', sa.Text(), nullable=False), + sa.Column('created_at', sa.String(36), server_default=func.now()), + sa.Column('updated_at', sa.String(36), onupdate=func.now()) + ) + + +def downgrade(): + op.drop_table('contactforms') \ No newline at end of file diff --git a/endpoints/contact-us.post.py b/endpoints/contact-us.post.py index e69de29..adbe1e8 100644 --- a/endpoints/contact-us.post.py +++ b/endpoints/contact-us.post.py @@ -0,0 +1,22 @@ +from fastapi import APIRouter, HTTPException, status +from schemas.contactform import ContactFormCreate +from helpers.contactform_helpers import create_contactform, validate_email +from sqlalchemy.orm import Session +from fastapi import Depends +from core.database import get_db + +router = APIRouter() + +@router.post("/contact-us", status_code=status.HTTP_201_CREATED) +async def create_contact_form( + contactform_data: ContactFormCreate, + db: Session = Depends(get_db) +): + if not contactform_data.name or not contactform_data.email or not contactform_data.message: + raise HTTPException(status_code=400, detail="All fields are required") + + if not validate_email(contactform_data.email): + raise HTTPException(status_code=400, detail="Invalid email format") + + new_contactform = create_contactform(db=db, contactform_data=contactform_data) + return new_contactform \ No newline at end of file diff --git a/helpers/contactform_helpers.py b/helpers/contactform_helpers.py new file mode 100644 index 0000000..6c947b6 --- /dev/null +++ b/helpers/contactform_helpers.py @@ -0,0 +1,54 @@ +import re +from typing import List +from sqlalchemy.orm import Session +from models.contactform import ContactForm +from schemas.contactform import ContactFormCreate, ContactFormSchema +from fastapi import HTTPException + +def validate_email(email: str) -> bool: + """ + Validates if the provided email is in a valid format. + + Args: + email (str): The email address to validate. + + Returns: + bool: True if the email is valid, False otherwise. + """ + email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' + return bool(re.match(email_regex, email)) + +def create_contactform(db: Session, contactform_data: ContactFormCreate) -> ContactForm: + """ + Creates a new contact form in the database. + + Args: + db (Session): The database session. + contactform_data (ContactFormCreate): The data for the contact form to create. + + Returns: + ContactForm: The newly created contact form object. + """ + if not contactform_data.name or not contactform_data.email or not contactform_data.message: + raise HTTPException(status_code=400, detail="All fields are required") + + if not validate_email(contactform_data.email): + raise HTTPException(status_code=400, detail="Invalid email format") + + db_contactform = ContactForm(**contactform_data.dict()) + db.add(db_contactform) + db.commit() + db.refresh(db_contactform) + return db_contactform + +def get_all_contactforms(db: Session) -> List[ContactFormSchema]: + """ + Retrieves all contact forms from the database. + + Args: + db (Session): The database session. + + Returns: + List[ContactFormSchema]: A list of all contact form objects. + """ + return [ContactFormSchema.from_orm(contactform) for contactform in db.query(ContactForm).all()] \ No newline at end of file diff --git a/models/contactform.py b/models/contactform.py new file mode 100644 index 0000000..c2e8076 --- /dev/null +++ b/models/contactform.py @@ -0,0 +1,15 @@ +from sqlalchemy import Column, String, Text +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class ContactForm(Base): + __tablename__ = "contactforms" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False) + email = Column(String, nullable=False) + message = Column(Text, nullable=False) + created_at = Column(UUID(as_uuid=True), server_default=func.now()) + updated_at = Column(UUID(as_uuid=True), onupdate=func.now()) \ No newline at end of file diff --git a/schemas/contactform.py b/schemas/contactform.py new file mode 100644 index 0000000..6a354e7 --- /dev/null +++ b/schemas/contactform.py @@ -0,0 +1,25 @@ +from pydantic import BaseModel, Field, EmailStr +from typing import Optional +from datetime import datetime +from uuid import UUID + +class ContactFormBase(BaseModel): + name: str = Field(..., description="Name of the contact") + email: EmailStr = Field(..., description="Email address of the contact") + message: str = Field(..., description="Message from the contact") + +class ContactFormCreate(ContactFormBase): + pass + +class ContactFormUpdate(ContactFormBase): + name: Optional[str] = Field(None, description="Name of the contact") + email: Optional[EmailStr] = Field(None, description="Email address of the contact") + message: Optional[str] = Field(None, description="Message from the contact") + +class ContactFormSchema(ContactFormBase): + id: UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file