feat: Updated endpoint endpoints/create-soap.post.py via AI

This commit is contained in:
Backend IM Bot 2025-04-15 12:49:38 +00:00
parent 3e2cc50d28
commit b13a4482a8
5 changed files with 42 additions and 8 deletions

View File

@ -0,0 +1,22 @@
"""add color column to soaps table
Revision ID: 2c3d4e5f6a7b
Revises: 8b7d6a1c5f24
Create Date: 2023-05-25 12:34:56
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '2c3d4e5f6a7b'
down_revision = '8b7d6a1c5f24'
branch_labels = None
depends_on = None
def upgrade():
table_name = "soaps"
op.add_column(table_name, sa.Column('color', sa.String(), nullable=False))
def downgrade():
table_name = "soaps"
op.drop_column(table_name, 'color')

View File

@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from core.database import get_db from core.database import get_db
from schemas.soap import SoapCreate, SoapSchema from schemas.soap import SoapCreate, SoapSchema
@ -7,12 +7,9 @@ from helpers.soap_helpers import create_soap
router = APIRouter() router = APIRouter()
@router.post("/create-soap", status_code=status.HTTP_201_CREATED, response_model=SoapSchema) @router.post("/create-soap", status_code=status.HTTP_201_CREATED, response_model=SoapSchema)
async def create_new_soap( async def add_color_to_soap(
soap_data: SoapCreate, soap_data: SoapCreate,
db: Session = Depends(get_db) db: Session = Depends(get_db)
): ):
try: new_soap = create_soap(db=db, soap_data=soap_data)
new_soap = create_soap(db=db, soap_data=soap_data) return new_soap
return new_soap
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))

View File

@ -28,7 +28,7 @@ def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
SoapSchema: The newly created soap object. SoapSchema: The newly created soap object.
""" """
try: try:
db_soap = Soap(name=soap_data.name) db_soap = Soap(name=soap_data.name, color=soap_data.color)
db.add(db_soap) db.add(db_soap)
db.commit() db.commit()
db.refresh(db_soap) db.refresh(db_soap)

View File

@ -3,11 +3,18 @@ from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func from sqlalchemy.sql import func
from core.database import Base from core.database import Base
import uuid import uuid
from sqlalchemy import Enum
class SoapColor(str, Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class Soap(Base): class Soap(Base):
__tablename__ = "soaps" __tablename__ = "soaps"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, nullable=False, unique=True, index=True) name = Column(String, nullable=False, unique=True, index=True)
color = Column(Enum(SoapColor), nullable=False)
created_at = Column(DateTime, default=func.now()) created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

View File

@ -2,15 +2,23 @@ from pydantic import BaseModel, Field
from typing import Optional from typing import Optional
from datetime import datetime from datetime import datetime
from uuid import UUID from uuid import UUID
from enum import Enum
class SoapColor(str, Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
class SoapBase(BaseModel): class SoapBase(BaseModel):
name: str = Field(..., description="Name of the soap") name: str = Field(..., description="Name of the soap")
color: SoapColor = Field(..., description="Color of the soap")
class SoapCreate(SoapBase): class SoapCreate(SoapBase):
pass pass
class SoapUpdate(SoapBase): class SoapUpdate(SoapBase):
name: Optional[str] = Field(None, description="Updated name of the soap") name: Optional[str] = Field(None, description="Updated name of the soap")
color: Optional[SoapColor] = Field(None, description="Updated color of the soap")
class SoapSchema(SoapBase): class SoapSchema(SoapBase):
id: UUID id: UUID