From 23072ff369c78dcae7d152463fe3c4102b60d372 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 11 Apr 2025 17:18:33 +0000 Subject: [PATCH] feat: Updated endpoint endpoints/names.post.py via AI --- endpoints/names.post.py | 19 ++++++++++--------- helpers/ship_helpers.py | 20 ++++++++++---------- schemas/ship.py | 23 ++++++++++++++++++++++- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/endpoints/names.post.py b/endpoints/names.post.py index 672c4b7..1566d4e 100644 --- a/endpoints/names.post.py +++ b/endpoints/names.post.py @@ -2,15 +2,16 @@ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List from core.database import get_db -from schemas.ship import ShipSchema -from helpers.ship_helpers import get_all_ships +from helpers.ship_helpers import get_ship_names router = APIRouter() -@router.post("/names", status_code=200, response_model=List[ShipSchema]) -async def get_all_ship_details(db: Session = Depends(get_db)): - """Return all ships""" - ships = get_all_ships(db) - if not ships: - raise HTTPException(status_code=404, detail="No ships found") - return ships \ No newline at end of file +@router.post("/names", status_code=200, response_model=List[str]) +async def get_names( + db: Session = Depends(get_db) +): + """Get all ship names""" + names = get_ship_names(db) + if not names: + raise HTTPException(status_code=404, detail="No ship names found") + return names \ No newline at end of file diff --git a/helpers/ship_helpers.py b/helpers/ship_helpers.py index f7090e6..b9240bc 100644 --- a/helpers/ship_helpers.py +++ b/helpers/ship_helpers.py @@ -3,19 +3,20 @@ 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 +from schemas.ship import ShipCreate, ShipUpdate, ShipNamesListResponse, ShipNameResponse -def get_all_ships(db: Session) -> List[Ship]: +def get_all_ships(db: Session) -> List[ShipNameResponse]: """ - Retrieves all ships from the database. + Retrieves all ships from the database, returning only their names. Args: db (Session): The database session. Returns: - List[Ship]: A list of all ship objects. + List[ShipNameResponse]: A list of ship name response objects. """ - return db.query(Ship).all() + ships = db.query(Ship.name).all() + return [ShipNameResponse(name=ship.name) for ship in ships] def get_ship_by_id(db: Session, ship_id: UUID) -> Optional[Ship]: """ @@ -30,19 +31,18 @@ def get_ship_by_id(db: Session, ship_id: UUID) -> Optional[Ship]: """ return db.query(Ship).filter(Ship.id == ship_id).first() -def get_ship_names(db: Session) -> List[str]: +def get_ship_names(db: Session) -> ShipNamesListResponse: """ Retrieves a list of all ship names. - - Note: This function is deprecated. Use get_all_ships() instead to get full ship objects. Args: db (Session): The database session. Returns: - List[str]: A list of ship names. + ShipNamesListResponse: A response object containing the list of ship names. """ - return [ship.name for ship in db.query(Ship.name).all()] + names = [ship.name for ship in db.query(Ship.name).all()] + return ShipNamesListResponse(ships=names) def create_ship(db: Session, ship_data: ShipCreate) -> Ship: """ diff --git a/schemas/ship.py b/schemas/ship.py index b55856a..2a6107e 100644 --- a/schemas/ship.py +++ b/schemas/ship.py @@ -1,5 +1,5 @@ from pydantic import BaseModel, Field -from typing import Optional +from typing import Optional, List from datetime import datetime from uuid import UUID @@ -26,4 +26,25 @@ class ShipSchema(ShipBase): "created_at": "2023-01-01T12:00:00", "updated_at": "2023-01-01T12:00:00" } + } + +class ShipNameResponse(BaseModel): + name: str = Field(..., description="Ship's name") + + class Config: + orm_mode = True + schema_extra = { + "example": { + "name": "HMS Victory" + } + } + +class ShipNamesListResponse(BaseModel): + ships: List[str] = Field(..., description="List of ship names") + + class Config: + schema_extra = { + "example": { + "ships": ["HMS Victory", "HMS Endeavour", "HMS Bounty"] + } } \ No newline at end of file