feat: Generated endpoint endpoints/contact-us.post.py via AI for Contact with auto lint fixes
This commit is contained in:
parent
25e4a7091a
commit
a11255b34d
31
alembic/versions/20250415_084526_014faf27_update_contact.py
Normal file
31
alembic/versions/20250415_084526_014faf27_update_contact.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""create table for contacts
|
||||
|
||||
Revision ID: 2a3b4c5d6e7f
|
||||
Revises: 0001
|
||||
Create Date: 2023-05-26 12:34:56
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.sqlite import UUID
|
||||
import uuid
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '2a3b4c5d6e7f'
|
||||
down_revision = '0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'contacts',
|
||||
sa.Column('id', UUID(as_uuid=True), primary_key=True, default=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.DateTime(), server_default=sa.func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now())
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('contacts')
|
@ -0,0 +1,20 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from schemas.contact import ContactCreate, ContactSchema
|
||||
from helpers.contact_helpers import create_contact, validate_contact_data
|
||||
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, response_model=ContactSchema)
|
||||
async def create_new_contact(
|
||||
contact_data: ContactCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Handle contact form submissions"""
|
||||
if not validate_contact_data(contact_data):
|
||||
raise HTTPException(status_code=400, detail="Invalid data provided")
|
||||
|
||||
new_contact = create_contact(db=db, contact_data=contact_data)
|
||||
return new_contact
|
48
helpers/contact_helpers.py
Normal file
48
helpers/contact_helpers.py
Normal file
@ -0,0 +1,48 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import ValidationError
|
||||
from models.contact import Contact
|
||||
from schemas.contact import ContactCreate
|
||||
import re
|
||||
|
||||
def create_contact(db: Session, contact_data: ContactCreate) -> Contact:
|
||||
"""
|
||||
Creates a new contact in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
contact_data (ContactCreate): The data for the contact to create.
|
||||
|
||||
Returns:
|
||||
Contact: The newly created contact object.
|
||||
"""
|
||||
db_contact = Contact(**contact_data.dict())
|
||||
db.add(db_contact)
|
||||
db.commit()
|
||||
db.refresh(db_contact)
|
||||
return db_contact
|
||||
|
||||
def validate_contact_data(contact_data: ContactCreate) -> bool:
|
||||
"""
|
||||
Validates the contact data.
|
||||
|
||||
Args:
|
||||
contact_data (ContactCreate): The contact data to validate.
|
||||
|
||||
Returns:
|
||||
bool: True if the data is valid, False otherwise.
|
||||
"""
|
||||
try:
|
||||
contact_data.name
|
||||
contact_data.email
|
||||
contact_data.message
|
||||
except ValidationError:
|
||||
return False
|
||||
|
||||
if not contact_data.name or not contact_data.message:
|
||||
return False
|
||||
|
||||
email_regex = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
||||
if not re.match(email_regex, contact_data.email):
|
||||
return False
|
||||
|
||||
return True
|
15
models/contact.py
Normal file
15
models/contact.py
Normal file
@ -0,0 +1,15 @@
|
||||
from sqlalchemy import Column, String, Text, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.sql import func
|
||||
from core.database import Base
|
||||
import uuid
|
||||
|
||||
class Contact(Base):
|
||||
__tablename__ = "contacts"
|
||||
|
||||
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(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
25
schemas/contact.py
Normal file
25
schemas/contact.py
Normal file
@ -0,0 +1,25 @@
|
||||
from pydantic import BaseModel, Field, EmailStr
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
class ContactBase(BaseModel):
|
||||
name: str = Field(..., description="Contact's name")
|
||||
email: EmailStr = Field(..., description="Contact's email address")
|
||||
message: str = Field(..., description="Contact's message")
|
||||
|
||||
class ContactCreate(ContactBase):
|
||||
pass
|
||||
|
||||
class ContactUpdate(ContactBase):
|
||||
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 ContactSchema(ContactBase):
|
||||
id: uuid.UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user