feat: add POST /CargoTrace/location endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
0
app/api/v1/__init__.py
Normal file
0
app/api/v1/__init__.py
Normal file
18
app/api/v1/location.py
Normal file
18
app/api/v1/location.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.schemas.location import LocationRequest, LocationResponse
|
||||||
|
from app.services.location_service import register_location
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/location", response_model=LocationResponse)
|
||||||
|
def create_location(req: LocationRequest, db: Session = Depends(get_db)):
|
||||||
|
record = register_location(db, req)
|
||||||
|
return LocationResponse(
|
||||||
|
zongpai_no=record.zongpai_no,
|
||||||
|
location_code=record.location_code,
|
||||||
|
created_at=record.created_at.isoformat(),
|
||||||
|
)
|
||||||
24
app/main.py
24
app/main.py
@@ -1,7 +1,29 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
from app.api.v1.location import router as location_router
|
||||||
|
|
||||||
app = FastAPI(title="CargoTrace API", version="0.1.0")
|
app = FastAPI(title="CargoTrace API", version="0.1.0")
|
||||||
|
|
||||||
|
app.include_router(location_router, prefix="/CargoTrace")
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(ValueError)
|
||||||
|
async def value_error_handler(request: Request, exc: ValueError):
|
||||||
|
error_code = str(exc)
|
||||||
|
status_map = {
|
||||||
|
"INVALID_ZONGPAI": 400,
|
||||||
|
"INVALID_LOCATION": 400,
|
||||||
|
}
|
||||||
|
status = status_map.get(error_code, 400)
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status,
|
||||||
|
content={
|
||||||
|
"error_code": error_code,
|
||||||
|
"message": str(exc),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
|
|||||||
Reference in New Issue
Block a user