增加获取地址详情的接口
This commit is contained in:
parent
1d7d90289c
commit
4c67896044
@ -144,3 +144,41 @@ async def set_default_address(
|
|||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_address)
|
db.refresh(db_address)
|
||||||
return success_response(data=AddressInfo.model_validate(db_address))
|
return success_response(data=AddressInfo.model_validate(db_address))
|
||||||
|
|
||||||
|
@router.get("/{address_id}", response_model=ResponseModel)
|
||||||
|
async def get_address(
|
||||||
|
address_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""获取地址详情"""
|
||||||
|
# 查询地址,并 join 小区名称
|
||||||
|
address = db.query(
|
||||||
|
AddressDB,
|
||||||
|
CommunityDB.name.label('community_name')
|
||||||
|
).join(
|
||||||
|
CommunityDB,
|
||||||
|
AddressDB.community_id == CommunityDB.id
|
||||||
|
).filter(
|
||||||
|
AddressDB.id == address_id,
|
||||||
|
AddressDB.user_id == current_user.userid # 只能查看自己的地址
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if not address:
|
||||||
|
return error_response(code=404, message="地址不存在")
|
||||||
|
|
||||||
|
# 构建返回数据
|
||||||
|
address_data = {
|
||||||
|
"id": address.AddressDB.id,
|
||||||
|
"community_id": address.AddressDB.community_id,
|
||||||
|
"community_name": address.community_name,
|
||||||
|
"community_building_id": address.AddressDB.community_building_id,
|
||||||
|
"community_building_name": address.AddressDB.community_building_name,
|
||||||
|
"address_detail": address.AddressDB.address_detail,
|
||||||
|
"name": address.AddressDB.name,
|
||||||
|
"phone": address.AddressDB.phone,
|
||||||
|
"gender": address.AddressDB.gender,
|
||||||
|
"is_default": address.AddressDB.is_default
|
||||||
|
}
|
||||||
|
|
||||||
|
return success_response(data=AddressInfo(**address_data))
|
||||||
Loading…
Reference in New Issue
Block a user