diff --git a/server/src/data/repository.py b/server/src/data/repository.py index 760446a..1de7dfd 100644 --- a/server/src/data/repository.py +++ b/server/src/data/repository.py @@ -4,7 +4,8 @@ from sqlalchemy import select from sqlalchemy.orm import Session from .models import WindTurbineType, WindPark, WindParkTurbine -from .schemas import WindTurbineTypeCreate, WindParkCreate, WindParkTurbineCreate, WindParkResponse, WindTurbineResponse +from .schemas import WindTurbineTypeCreate, WindParkCreate, WindParkTurbineCreate, WindParkResponse, \ + WindTurbineResponse, WindParkTurbineResponse, WindTurbineWithParkDetailsResponse from data.database import SessionLocal from data.models import Weather from data.schemas import SWeatherInfo @@ -109,9 +110,19 @@ class WindParkRepository: return db.query(WindPark).all() @staticmethod - def get_turbines_by_park_id(park_id: int, db: Session) -> list[WindTurbineResponse]: + def get_turbines_by_park_id(park_id: int, db: Session) -> list[WindTurbineWithParkDetailsResponse]: turbines = ( - db.query(WindParkTurbine) + db.query( + WindTurbineType.Id.label("Id"), + WindTurbineType.Name.label("Name"), + WindTurbineType.Height.label("Height"), + WindTurbineType.BladeLength.label("BladeLength"), + WindParkTurbine.x_offset.label("x_offset"), + WindParkTurbine.y_offset.label("y_offset"), + WindParkTurbine.angle.label("angle"), + WindParkTurbine.comment.label("comment"), + ) + .join(WindParkTurbine, WindTurbineType.Id == WindParkTurbine.turbine_id) .filter(WindParkTurbine.wind_park_id == park_id) .all() ) @@ -119,10 +130,7 @@ class WindParkRepository: if not turbines: return [] # Возвращаем пустой список, если не найдено - turbine_ids = [turbine.turbine_id for turbine in turbines] - turbine_details = db.query(WindTurbineType).filter(WindTurbineType.Id.in_(turbine_ids)).all() - - return turbine_details + return [WindTurbineWithParkDetailsResponse(**turbine._asdict()) for turbine in turbines] class WindParkTurbineRepository: diff --git a/server/src/data/schemas.py b/server/src/data/schemas.py index 5c72545..36825f9 100644 --- a/server/src/data/schemas.py +++ b/server/src/data/schemas.py @@ -96,3 +96,16 @@ class WindTurbineResponse(BaseModel): class Config: orm_mode = True + +class WindTurbineWithParkDetailsResponse(BaseModel): + Id: int + Name: str + Height: float + BladeLength: float + x_offset: int + y_offset: int + angle: Optional[int] + comment: Optional[str] + + class Config: + orm_mode = True \ No newline at end of file diff --git a/server/src/routers/wind_park_router.py b/server/src/routers/wind_park_router.py index 200ca21..4269155 100644 --- a/server/src/routers/wind_park_router.py +++ b/server/src/routers/wind_park_router.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException, Depends from sqlalchemy.orm import Session from data.schemas import WindTurbineTypeCreate, WindTurbineTypeResponse, WindParkCreate, WindParkResponse, \ - WindParkTurbineCreate, WindParkTurbineResponse, WindTurbineResponse + WindParkTurbineCreate, WindParkTurbineResponse, WindTurbineResponse, WindTurbineWithParkDetailsResponse from data.database import get_db from data.repository import WindTurbineTypeRepository, WindParkRepository, WindParkTurbineRepository @@ -107,7 +107,7 @@ async def get_all_parks(db: Session = Depends(get_db)): return WindParkRepository.get_all_parks(db) -@router.get("/parks/{park_id}/turbines/", response_model=list[WindTurbineResponse]) +@router.get("/parks/{park_id}/turbines/", response_model=list[WindTurbineWithParkDetailsResponse]) async def get_turbines_by_park_id(park_id: int, db: Session = Depends(get_db)): """Получить все турбины в ветропарке по ID""" turbines = WindParkRepository.get_turbines_by_park_id(park_id, db)