get_turbines_by_park_id route fix

This commit is contained in:
Павел Сорокин 2024-11-19 17:16:09 +04:00
parent 6e87595e2f
commit 4d40a2cacb
3 changed files with 30 additions and 9 deletions

View File

@ -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:

View File

@ -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

View File

@ -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)