update
This commit is contained in:
parent
d80555bbad
commit
78901fee1e
@ -7,6 +7,7 @@ from app.models.community_building import (
|
||||
CommunityBuildingUpdate,
|
||||
CommunityBuildingInfo
|
||||
)
|
||||
from app.models.community import CommunityDB
|
||||
from app.models.database import get_db
|
||||
from app.api.deps import get_admin_user
|
||||
from app.models.user import UserDB
|
||||
@ -22,7 +23,15 @@ async def get_buildings(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""获取楼栋列表"""
|
||||
query = db.query(CommunityBuildingDB)
|
||||
# 联表查询,获取社区名称
|
||||
query = db.query(
|
||||
CommunityBuildingDB,
|
||||
CommunityDB.name.label('community_name')
|
||||
).join(
|
||||
CommunityDB,
|
||||
CommunityBuildingDB.community_id == CommunityDB.id
|
||||
)
|
||||
|
||||
if community_id:
|
||||
query = query.filter(CommunityBuildingDB.community_id == community_id)
|
||||
|
||||
@ -30,11 +39,18 @@ async def get_buildings(
|
||||
total = query.count()
|
||||
|
||||
# 查询数据
|
||||
buildings = query.offset(skip).limit(limit).all()
|
||||
results = query.offset(skip).limit(limit).all()
|
||||
|
||||
# 处理返回数据
|
||||
building_list = []
|
||||
for building, community_name in results:
|
||||
building_info = CommunityBuildingInfo.model_validate(building)
|
||||
building_info.community_name = community_name
|
||||
building_list.append(building_info)
|
||||
|
||||
return success_response(data={
|
||||
"total": total,
|
||||
"items": [CommunityBuildingInfo.model_validate(b) for b in buildings]
|
||||
"items": building_list
|
||||
})
|
||||
|
||||
@router.post("", response_model=ResponseModel)
|
||||
|
||||
@ -6,6 +6,7 @@ from app.models.database import get_db
|
||||
from app.api.deps import get_admin_user
|
||||
from app.models.user import UserDB
|
||||
from app.core.response import success_response, error_response, ResponseModel
|
||||
from app.models.community import CommunityDB
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -30,7 +31,15 @@ async def get_stations(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""获取驿站列表"""
|
||||
query = db.query(StationDB)
|
||||
# 联表查询,获取社区名称
|
||||
query = db.query(
|
||||
StationDB,
|
||||
CommunityDB.name.label('community_name')
|
||||
).join(
|
||||
CommunityDB,
|
||||
StationDB.community_id == CommunityDB.id
|
||||
)
|
||||
|
||||
if community_id:
|
||||
query = query.filter(StationDB.community_id == community_id)
|
||||
|
||||
@ -38,11 +47,18 @@ async def get_stations(
|
||||
total = query.count()
|
||||
|
||||
# 查询数据
|
||||
stations = query.offset(skip).limit(limit).all()
|
||||
results = query.offset(skip).limit(limit).all()
|
||||
|
||||
# 处理返回数据
|
||||
station_list = []
|
||||
for station, community_name in results:
|
||||
station_info = StationInfo.model_validate(station)
|
||||
station_info.community_name = community_name
|
||||
station_list.append(station_info)
|
||||
|
||||
return success_response(data={
|
||||
"total": total,
|
||||
"items": [StationInfo.model_validate(s) for s in stations]
|
||||
"items": station_list
|
||||
})
|
||||
|
||||
@router.get("/{station_id}", response_model=ResponseModel)
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from .database import Base
|
||||
from datetime import datetime
|
||||
|
||||
# 数据库模型
|
||||
class CommunityBuildingDB(Base):
|
||||
@ -32,9 +32,9 @@ class CommunityBuildingUpdate(BaseModel):
|
||||
class CommunityBuildingInfo(BaseModel):
|
||||
id: int
|
||||
community_id: int
|
||||
community_name: Optional[str] = None # 通过join查询获取的社区名称
|
||||
building_name: str
|
||||
building_number: str
|
||||
create_time: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@ -26,7 +26,9 @@ class StationUpdate(BaseModel):
|
||||
class StationInfo(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
address: str
|
||||
community_id: int
|
||||
community_name: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Loading…
Reference in New Issue
Block a user