From 4c678960442f8b0c948dbaf8087d42a6255273e1 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Thu, 13 Feb 2025 15:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E8=AF=A6=E6=83=85=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/address.py | 40 +++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/app/api/endpoints/address.py b/app/api/endpoints/address.py index d32b755..d327c9b 100644 --- a/app/api/endpoints/address.py +++ b/app/api/endpoints/address.py @@ -143,4 +143,42 @@ async def set_default_address( db_address.is_default = True db.commit() db.refresh(db_address) - return success_response(data=AddressInfo.model_validate(db_address)) \ No newline at end of file + 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)) \ No newline at end of file