From 2e587279d8a9228efb172ba749571c0b44a75329 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 11 Apr 2025 15:06:10 +0000 Subject: [PATCH] feat: Generated endpoint endpoints/names.post.py via AI for Ship --- .../20250411_150552_b391bfb4_update_ship.py | 29 ++++ endpoints/names.post.py | 11 ++ helpers/ship_helpers.py | 124 ++++++++++++++++++ models/ship.py | 13 ++ schemas/ship.py | 29 ++++ 5 files changed, 206 insertions(+) create mode 100644 alembic/versions/20250411_150552_b391bfb4_update_ship.py create mode 100644 endpoints/names.post.py create mode 100644 helpers/ship_helpers.py create mode 100644 models/ship.py create mode 100644 schemas/ship.py diff --git a/alembic/versions/20250411_150552_b391bfb4_update_ship.py b/alembic/versions/20250411_150552_b391bfb4_update_ship.py new file mode 100644 index 0000000..663c64d --- /dev/null +++ b/alembic/versions/20250411_150552_b391bfb4_update_ship.py @@ -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') \ No newline at end of file diff --git a/endpoints/names.post.py b/endpoints/names.post.py new file mode 100644 index 0000000..f61e6fa --- /dev/null +++ b/endpoints/names.post.py @@ -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 \ No newline at end of file diff --git a/helpers/ship_helpers.py b/helpers/ship_helpers.py new file mode 100644 index 0000000..eb73d74 --- /dev/null +++ b/helpers/ship_helpers.py @@ -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 \ No newline at end of file diff --git a/models/ship.py b/models/ship.py new file mode 100644 index 0000000..e78380d --- /dev/null +++ b/models/ship.py @@ -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()) \ No newline at end of file diff --git a/schemas/ship.py b/schemas/ship.py new file mode 100644 index 0000000..b55856a --- /dev/null +++ b/schemas/ship.py @@ -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" + } + } \ No newline at end of file