更新地址接口

This commit is contained in:
aaron 2025-01-24 13:50:46 +08:00
parent a9a381fedd
commit c7f76ed1e6
2 changed files with 22 additions and 4 deletions

View File

@ -1,8 +1,9 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from sqlalchemy import and_
from typing import List
from typing import List, Optional
from app.models.address import AddressDB, AddressCreate, AddressUpdate, AddressInfo
from app.models.community import CommunityDB
from app.models.database import get_db
from app.api.deps import get_current_user
from app.models.user import UserDB
@ -36,14 +37,30 @@ async def create_address(
@router.get("/", response_model=ResponseModel)
async def get_addresses(
community_id: Optional[int] = None,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""获取用户的所有配送地址"""
addresses = db.query(AddressDB).filter(
addresses = db.query(
AddressDB,
CommunityDB.name.label('community_name')
).join(
CommunityDB,
AddressDB.community_id == CommunityDB.id
).filter(
AddressDB.user_id == current_user.userid
).all()
return success_response(data=[AddressInfo.model_validate(a) for a in addresses])
)
if community_id is not None:
addresses = addresses.filter(AddressDB.community_id == community_id)
addresses = addresses.all()
return success_response(data=[{
**AddressInfo.model_validate(a[0]).model_dump(),
"community_name": a[1]
} for a in addresses])
@router.put("/{address_id}", response_model=ResponseModel)
async def update_address(

View File

@ -46,6 +46,7 @@ class AddressUpdate(BaseModel):
class AddressInfo(BaseModel):
id: int
community_id: int
community_name: Optional[str] = None
community_building_id: Optional[int]
community_building_name: Optional[str]
address_detail: str