feat: Generated endpoint endpoints/test.post.py via AI for Contact_form
This commit is contained in:
parent
51e2d3cc15
commit
0790085d5a
@ -0,0 +1,32 @@
|
|||||||
|
"""create table for contact_forms
|
||||||
|
Revision ID: 2b7d8ff8d456
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-29 14:54:03.991959
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2b7d8ff8d456'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'contact_forms',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
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.Text(), nullable=False, server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.Text(), nullable=False, server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('contact_forms')
|
@ -0,0 +1,14 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException, status
|
||||||
|
from schemas.contact_form import ContactFormCreate
|
||||||
|
from helpers.contact_form_helpers import create_contact_form, validate_contact_form_data
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/contact", status_code=status.HTTP_201_CREATED, response_model=ContactFormCreate)
|
||||||
|
async def create_contact(contact_data: ContactFormCreate):
|
||||||
|
try:
|
||||||
|
validate_contact_form_data(contact_data)
|
||||||
|
new_contact = create_contact_form(contact_data=contact_data)
|
||||||
|
return new_contact
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
54
helpers/contact_form_helpers.py
Normal file
54
helpers/contact_form_helpers.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.contact_form import ContactForm
|
||||||
|
from schemas.contact_form import ContactFormCreate
|
||||||
|
import re
|
||||||
|
|
||||||
|
def create_contact_form(db: Session, contact_data: ContactFormCreate) -> ContactForm:
|
||||||
|
"""
|
||||||
|
Creates a new contact form in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
contact_data (ContactFormCreate): The data for the contact form to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
ContactForm: The newly created contact form object.
|
||||||
|
"""
|
||||||
|
validate_contact_form_data(contact_data)
|
||||||
|
db_contact_form = ContactForm(**contact_data.dict())
|
||||||
|
db.add(db_contact_form)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_contact_form)
|
||||||
|
return db_contact_form
|
||||||
|
|
||||||
|
def validate_contact_form_data(contact_data: ContactFormCreate) -> None:
|
||||||
|
"""
|
||||||
|
Validates the contact form data.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
contact_data (ContactFormCreate): The contact form data to validate.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If any of the required fields are missing or the email is invalid.
|
||||||
|
"""
|
||||||
|
if not contact_data.name:
|
||||||
|
raise ValueError("Name is required")
|
||||||
|
if not contact_data.email:
|
||||||
|
raise ValueError("Email is required")
|
||||||
|
if not contact_data.message:
|
||||||
|
raise ValueError("Message is required")
|
||||||
|
if not is_valid_email(contact_data.email):
|
||||||
|
raise ValueError("Invalid email format")
|
||||||
|
|
||||||
|
def is_valid_email(email: str) -> bool:
|
||||||
|
"""
|
||||||
|
Checks if the given email is in a valid format.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
email (str): The email address to validate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the email is valid, False otherwise.
|
||||||
|
"""
|
||||||
|
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
||||||
|
return bool(re.match(pattern, email))
|
15
models/contact_form.py
Normal file
15
models/contact_form.py
Normal file
@ -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__ = "contact_forms"
|
||||||
|
|
||||||
|
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(Text, default=func.now())
|
||||||
|
updated_at = Column(Text, default=func.now(), onupdate=func.now())
|
25
schemas/contact_form.py
Normal file
25
schemas/contact_form.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from pydantic import BaseModel, Field, EmailStr
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class ContactFormBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Contact's name")
|
||||||
|
email: EmailStr = Field(..., description="Contact's email address")
|
||||||
|
message: str = Field(..., description="Contact's message")
|
||||||
|
|
||||||
|
class ContactFormCreate(ContactFormBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ContactFormUpdate(ContactFormBase):
|
||||||
|
name: Optional[str] = Field(None, description="Contact's name")
|
||||||
|
email: Optional[EmailStr] = Field(None, description="Contact's email address")
|
||||||
|
message: Optional[str] = Field(None, description="Contact's message")
|
||||||
|
|
||||||
|
class ContactFormSchema(ContactFormBase):
|
||||||
|
id: uuid.UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user