feat: Generated endpoint endpoints/create-soap.post.py via AI for Soap
This commit is contained in:
parent
79d69ee7a5
commit
7e04f8493d
31
alembic/versions/20250414_200439_eadc37c5_update_soap.py
Normal file
31
alembic/versions/20250414_200439_eadc37c5_update_soap.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"""create table for soaps
|
||||||
|
Revision ID: 8b7d6a1c5f24
|
||||||
|
Revises: 3a7d6c1e86b4
|
||||||
|
Create Date: 2023-05-24 12:00:00
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '8b7d6a1c5f24'
|
||||||
|
down_revision = '3a7d6c1e86b4'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_name = "soaps"
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
table_name,
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Index('ix_soaps_name', 'name'),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
table_name = "soaps"
|
||||||
|
op.drop_table(table_name)
|
@ -0,0 +1,16 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from core.schemas.soap import SoapCreate, SoapSchema
|
||||||
|
from core.helpers.soap_helpers import create_soap
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/create-soap", status_code=status.HTTP_201_CREATED, response_model=SoapSchema)
|
||||||
|
async def create_new_soap(
|
||||||
|
soap_data: SoapCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
new_soap = create_soap(db=db, soap_data=soap_data)
|
||||||
|
return new_soap
|
48
helpers/soap_helpers.py
Normal file
48
helpers/soap_helpers.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.models.soap import Soap
|
||||||
|
from core.schemas.soap import SoapCreate, SoapSchema
|
||||||
|
|
||||||
|
def get_all_soaps(db: Session) -> List[SoapSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves all soaps from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[SoapSchema]: A list of all soap objects.
|
||||||
|
"""
|
||||||
|
soaps = db.query(Soap).all()
|
||||||
|
return [SoapSchema.from_orm(soap) for soap in soaps]
|
||||||
|
|
||||||
|
def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
|
||||||
|
"""
|
||||||
|
Creates a new soap in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
soap_data (SoapCreate): The data for the soap to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
SoapSchema: The newly created soap object.
|
||||||
|
"""
|
||||||
|
db_soap = Soap(name=soap_data.name)
|
||||||
|
db.add(db_soap)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_soap)
|
||||||
|
return SoapSchema.from_orm(db_soap)
|
||||||
|
|
||||||
|
def get_soap_by_name(db: Session, name: str) -> Optional[SoapSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves a single soap by its name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
name (str): The name of the soap to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[SoapSchema]: The soap object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
soap = db.query(Soap).filter(Soap.name == name).first()
|
||||||
|
return SoapSchema.from_orm(soap) if soap else None
|
13
models/soap.py
Normal file
13
models/soap.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from sqlalchemy import Column, String, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from core.database import Base
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class Soap(Base):
|
||||||
|
__tablename__ = "soaps"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
21
schemas/soap.py
Normal file
21
schemas/soap.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class SoapBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Name of the soap")
|
||||||
|
|
||||||
|
class SoapCreate(SoapBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class SoapUpdate(SoapBase):
|
||||||
|
name: Optional[str] = Field(None, description="Name of the soap")
|
||||||
|
|
||||||
|
class SoapSchema(SoapBase):
|
||||||
|
id: UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user