Simplify endpoint paths and enhance readability

This commit is contained in:
Felix Zett 2025-08-09 21:35:45 +02:00
parent 9dc1241728
commit 269bfaab2c
3 changed files with 13 additions and 14 deletions

View file

@ -1,12 +1,11 @@
from fastapi import FastAPI
from routes.items import router as items_router
from routes.trips import router as trips_router
from routes import items, trips
from models import Base
from database import engine
app = FastAPI()
app.include_router(items_router)
app.include_router(trips_router)
app.include_router(items.router)
app.include_router(trips.router)
@app.on_event("startup")
async def startup():

View file

@ -4,13 +4,13 @@ from sqlalchemy.ext.asyncio import AsyncSession
from schemas import ItemCreate
from models import Item, Tag
from database import get_db
from sqlalchemy import select, update
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from config import FIXED_USER_ID
router = APIRouter()
router = APIRouter(prefix="/items", tags=["Items"])
@router.post("/items/")
@router.post("/")
async def create_item(item: ItemCreate, db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
@ -30,7 +30,7 @@ async def create_item(item: ItemCreate, db: AsyncSession = Depends(get_db)):
await db.commit()
return {"status": "item created", "item_id": str(db_item.id)}
@router.get("/items/")
@router.get("/")
async def read_items(db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
result = await db.execute(select(Item).where(Item.user_id == user_id).options(selectinload(Item.tags)))
@ -46,7 +46,7 @@ async def read_items(db: AsyncSession = Depends(get_db)):
]
}
@router.put("/items/{item_id}")
@router.put("/{item_id}")
async def update_item(item_id: str, item: ItemCreate, db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
@ -71,7 +71,7 @@ async def update_item(item_id: str, item: ItemCreate, db: AsyncSession = Depends
await db.commit()
return {"status": "item updated", "item_id": str(db_item.id)}
@router.get("/items/{item_id}")
@router.get("/{item_id}")
async def get_item(item_id: str, db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
result = await db.execute(

View file

@ -9,9 +9,9 @@ from sqlalchemy import select
from sqlalchemy.orm import selectinload
from config import FIXED_USER_ID
router = APIRouter()
router = APIRouter(prefix="/trips", tags=["Trips"])
@router.post("/trips/")
@router.post("/")
async def create_trip(trip: TripCreate, db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
@ -76,7 +76,7 @@ async def create_trip(trip: TripCreate, db: AsyncSession = Depends(get_db)):
return {"status": "trip created", "trip_id": str(db_trip.id)}
@router.get("/trips/{trip_id}/items")
@router.get("/{trip_id}/items")
async def get_trip_items(trip_id: UUID, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(TripItem).where(TripItem.trip_id == trip_id))
items = result.scalars().all()
@ -88,7 +88,7 @@ async def get_trip_items(trip_id: UUID, db: AsyncSession = Depends(get_db)):
} for item in items
]
@router.get("/trips/")
@router.get("/")
async def get_trips(db: AsyncSession = Depends(get_db)):
user_id = FIXED_USER_ID
result = await db.execute(select(Trip).where(Trip.user_id == user_id))