feat: Generated endpoint endpoints/names.post.py via AI for Ship
This commit is contained in:
parent
ac8f68ccc7
commit
2e587279d8
29
alembic/versions/20250411_150552_b391bfb4_update_ship.py
Normal file
29
alembic/versions/20250411_150552_b391bfb4_update_ship.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""create ships table
|
||||||
|
Revision ID: 0002
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2024-01-30 12:00:00.000000
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '0002'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'ships',
|
||||||
|
sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4),
|
||||||
|
sa.Column('name', sa.String(), 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())
|
||||||
|
)
|
||||||
|
op.create_index('ix_ships_name', 'ships', ['name'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_index('ix_ships_name', 'ships')
|
||||||
|
op.drop_table('ships')
|
11
endpoints/names.post.py
Normal file
11
endpoints/names.post.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from typing import List
|
||||||
|
from helpers.ship_helpers import get_ship_names
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/names", status_code=status.HTTP_200_OK, response_model=List[str])
|
||||||
|
async def get_names():
|
||||||
|
"""Get list of ship names"""
|
||||||
|
names = get_ship_names()
|
||||||
|
return names
|
124
helpers/ship_helpers.py
Normal file
124
helpers/ship_helpers.py
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from uuid import UUID
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from models.ship import Ship
|
||||||
|
from schemas.ship import ShipCreate, ShipUpdate, ShipSchema
|
||||||
|
|
||||||
|
def get_all_ships(db: Session) -> List[Ship]:
|
||||||
|
"""
|
||||||
|
Retrieves all ships from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Ship]: A list of all ship objects.
|
||||||
|
"""
|
||||||
|
return db.query(Ship).all()
|
||||||
|
|
||||||
|
def get_ship_by_id(db: Session, ship_id: UUID) -> Optional[Ship]:
|
||||||
|
"""
|
||||||
|
Retrieves a single ship by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
ship_id (UUID): The ID of the ship to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Ship]: The ship object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Ship).filter(Ship.id == ship_id).first()
|
||||||
|
|
||||||
|
def get_ship_names(db: Session) -> List[str]:
|
||||||
|
"""
|
||||||
|
Retrieves a list of all ship names.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[str]: A list of ship names.
|
||||||
|
"""
|
||||||
|
return [ship.name for ship in db.query(Ship.name).all()]
|
||||||
|
|
||||||
|
def create_ship(db: Session, ship_data: ShipCreate) -> Ship:
|
||||||
|
"""
|
||||||
|
Creates a new ship in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
ship_data (ShipCreate): The data for the ship to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Ship: The newly created ship object.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
SQLAlchemyError: If there's an error creating the ship.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
db_ship = Ship(**ship_data.dict())
|
||||||
|
db.add(db_ship)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_ship)
|
||||||
|
return db_ship
|
||||||
|
except SQLAlchemyError:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def update_ship(db: Session, ship_id: UUID, ship_data: ShipUpdate) -> Optional[Ship]:
|
||||||
|
"""
|
||||||
|
Updates an existing ship in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
ship_id (UUID): The ID of the ship to update.
|
||||||
|
ship_data (ShipUpdate): The update data for the ship.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Ship]: The updated ship object if found, otherwise None.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
SQLAlchemyError: If there's an error updating the ship.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
db_ship = get_ship_by_id(db, ship_id)
|
||||||
|
if not db_ship:
|
||||||
|
return None
|
||||||
|
|
||||||
|
update_data = ship_data.dict(exclude_unset=True)
|
||||||
|
for field, value in update_data.items():
|
||||||
|
setattr(db_ship, field, value)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_ship)
|
||||||
|
return db_ship
|
||||||
|
except SQLAlchemyError:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def delete_ship(db: Session, ship_id: UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes a ship from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
ship_id (UUID): The ID of the ship to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the ship was deleted, False if not found.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
SQLAlchemyError: If there's an error deleting the ship.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
db_ship = get_ship_by_id(db, ship_id)
|
||||||
|
if not db_ship:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(db_ship)
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
except SQLAlchemyError:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
13
models/ship.py
Normal file
13
models/ship.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 Ship(Base):
|
||||||
|
__tablename__ = "ships"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, unique=True, nullable=False, index=True)
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
29
schemas/ship.py
Normal file
29
schemas/ship.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class ShipBase(BaseModel):
|
||||||
|
name: str = Field(..., min_length=1, description="Ship's name")
|
||||||
|
|
||||||
|
class ShipCreate(ShipBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ShipUpdate(BaseModel):
|
||||||
|
name: Optional[str] = Field(None, min_length=1, description="Ship's name")
|
||||||
|
|
||||||
|
class ShipSchema(ShipBase):
|
||||||
|
id: UUID
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
schema_extra = {
|
||||||
|
"example": {
|
||||||
|
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
||||||
|
"name": "HMS Victory",
|
||||||
|
"created_at": "2023-01-01T12:00:00",
|
||||||
|
"updated_at": "2023-01-01T12:00:00"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user