feat: Updated endpoint endpoints/names.post.py via AI
This commit is contained in:
parent
dba0d9b0f3
commit
23072ff369
@ -2,15 +2,16 @@ from fastapi import APIRouter, Depends, HTTPException
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from typing import List
|
from typing import List
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
from schemas.ship import ShipSchema
|
from helpers.ship_helpers import get_ship_names
|
||||||
from helpers.ship_helpers import get_all_ships
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/names", status_code=200, response_model=List[ShipSchema])
|
@router.post("/names", status_code=200, response_model=List[str])
|
||||||
async def get_all_ship_details(db: Session = Depends(get_db)):
|
async def get_names(
|
||||||
"""Return all ships"""
|
db: Session = Depends(get_db)
|
||||||
ships = get_all_ships(db)
|
):
|
||||||
if not ships:
|
"""Get all ship names"""
|
||||||
raise HTTPException(status_code=404, detail="No ships found")
|
names = get_ship_names(db)
|
||||||
return ships
|
if not names:
|
||||||
|
raise HTTPException(status_code=404, detail="No ship names found")
|
||||||
|
return names
|
@ -3,19 +3,20 @@ from uuid import UUID
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from models.ship import Ship
|
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:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session.
|
||||||
|
|
||||||
Returns:
|
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]:
|
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()
|
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.
|
Retrieves a list of all ship names.
|
||||||
|
|
||||||
Note: This function is deprecated. Use get_all_ships() instead to get full ship objects.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db (Session): The database session.
|
db (Session): The database session.
|
||||||
|
|
||||||
Returns:
|
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:
|
def create_ship(db: Session, ship_data: ShipCreate) -> Ship:
|
||||||
"""
|
"""
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import Optional
|
from typing import Optional, List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@ -26,4 +26,25 @@ class ShipSchema(ShipBase):
|
|||||||
"created_at": "2023-01-01T12:00:00",
|
"created_at": "2023-01-01T12:00:00",
|
||||||
"updated_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"]
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user