feat: Generated endpoint endpoints/create-soap.post.py via AI for Soap
This commit is contained in:
parent
7e04f8493d
commit
3e2cc50d28
29
alembic/versions/20250414_200458_4a1e322b_update_soap.py
Normal file
29
alembic/versions/20250414_200458_4a1e322b_update_soap.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""create table for soaps
|
||||||
|
Revision ID: 2f6d7a8c2e3d
|
||||||
|
Revises: 3a7d6c1e86b4
|
||||||
|
Create Date: 2023-05-31 15:17:06.885290
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2f6d7a8c2e3d'
|
||||||
|
down_revision = '3a7d6c1e86b4'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'soaps',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_soaps_name'), 'soaps', ['name'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_index(op.f('ix_soaps_name'), table_name='soaps')
|
||||||
|
op.drop_table('soaps')
|
@ -1,9 +1,8 @@
|
|||||||
from fastapi import APIRouter, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from core.schemas.soap import SoapCreate, SoapSchema
|
|
||||||
from core.helpers.soap_helpers import create_soap
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from fastapi import Depends
|
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
|
from schemas.soap import SoapCreate, SoapSchema
|
||||||
|
from helpers.soap_helpers import create_soap
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -12,5 +11,8 @@ async def create_new_soap(
|
|||||||
soap_data: SoapCreate,
|
soap_data: SoapCreate,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
new_soap = create_soap(db=db, soap_data=soap_data)
|
try:
|
||||||
return new_soap
|
new_soap = create_soap(db=db, soap_data=soap_data)
|
||||||
|
return new_soap
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
@ -1,7 +1,8 @@
|
|||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from core.models.soap import Soap
|
from sqlalchemy import exc
|
||||||
from core.schemas.soap import SoapCreate, SoapSchema
|
from models.soap import Soap
|
||||||
|
from schemas.soap import SoapCreate, SoapSchema
|
||||||
|
|
||||||
def get_all_soaps(db: Session) -> List[SoapSchema]:
|
def get_all_soaps(db: Session) -> List[SoapSchema]:
|
||||||
"""
|
"""
|
||||||
@ -13,8 +14,7 @@ def get_all_soaps(db: Session) -> List[SoapSchema]:
|
|||||||
Returns:
|
Returns:
|
||||||
List[SoapSchema]: A list of all soap objects.
|
List[SoapSchema]: A list of all soap objects.
|
||||||
"""
|
"""
|
||||||
soaps = db.query(Soap).all()
|
return db.query(Soap).all()
|
||||||
return [SoapSchema.from_orm(soap) for soap in soaps]
|
|
||||||
|
|
||||||
def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
|
def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
|
||||||
"""
|
"""
|
||||||
@ -27,22 +27,25 @@ def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
|
|||||||
Returns:
|
Returns:
|
||||||
SoapSchema: The newly created soap object.
|
SoapSchema: The newly created soap object.
|
||||||
"""
|
"""
|
||||||
db_soap = Soap(name=soap_data.name)
|
try:
|
||||||
db.add(db_soap)
|
db_soap = Soap(name=soap_data.name)
|
||||||
db.commit()
|
db.add(db_soap)
|
||||||
db.refresh(db_soap)
|
db.commit()
|
||||||
return SoapSchema.from_orm(db_soap)
|
db.refresh(db_soap)
|
||||||
|
return db_soap
|
||||||
|
except exc.IntegrityError:
|
||||||
|
db.rollback()
|
||||||
|
raise ValueError("Soap name already exists")
|
||||||
|
|
||||||
def get_soap_by_name(db: Session, name: str) -> Optional[SoapSchema]:
|
def get_soap_by_name(db: Session, soap_name: str) -> Optional[SoapSchema]:
|
||||||
"""
|
"""
|
||||||
Retrieves a single soap by its name.
|
Retrieves a single soap by its name.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session.
|
||||||
name (str): The name of the soap to retrieve.
|
soap_name (str): The name of the soap to retrieve.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Optional[SoapSchema]: The soap object if found, otherwise None.
|
Optional[SoapSchema]: The soap object if found, otherwise None.
|
||||||
"""
|
"""
|
||||||
soap = db.query(Soap).filter(Soap.name == name).first()
|
return db.query(Soap).filter(Soap.name == soap_name).first()
|
||||||
return SoapSchema.from_orm(soap) if soap else None
|
|
@ -10,7 +10,7 @@ class SoapCreate(SoapBase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class SoapUpdate(SoapBase):
|
class SoapUpdate(SoapBase):
|
||||||
name: Optional[str] = Field(None, description="Name of the soap")
|
name: Optional[str] = Field(None, description="Updated name of the soap")
|
||||||
|
|
||||||
class SoapSchema(SoapBase):
|
class SoapSchema(SoapBase):
|
||||||
id: UUID
|
id: UUID
|
||||||
|
Loading…
x
Reference in New Issue
Block a user