diff --git a/server/src/data/repository.py b/server/src/data/repository.py index b9fbc39..84fb1db 100644 --- a/server/src/data/repository.py +++ b/server/src/data/repository.py @@ -1,5 +1,8 @@ from sqlalchemy import select +from sqlalchemy.orm import Session +from .models import WindTurbineType, WindPark, WindParkTurbine +from .schemas import WindTurbineTypeCreate, WindParkCreate, WindParkTurbineCreate from data.database import session_maker from data.models import Weather from data.schemas import SWeatherInfo @@ -24,4 +27,107 @@ class WeatherRepository: ) res = session.execute(query) weather_model = res.scalars().first() - return SWeatherInfo.model_validate(weather_model, from_attributes=True) \ No newline at end of file + return SWeatherInfo.model_validate(weather_model, from_attributes=True) + + + +class WindTurbineTypeRepository: + @staticmethod + def create(db: Session, turbine_type: WindTurbineTypeCreate): + db_turbine_type = WindTurbineType(**turbine_type.dict()) + db.add(db_turbine_type) + db.commit() + db.refresh(db_turbine_type) + return db_turbine_type + + @staticmethod + def get(db: Session, turbine_type_id: int): + return db.query(WindTurbineType).filter(WindTurbineType.Id == turbine_type_id).first() + + @staticmethod + def update(db: Session, turbine_type_id: int, turbine_type: WindTurbineTypeCreate): + db_turbine_type = db.query(WindTurbineType).filter(WindTurbineType.Id == turbine_type_id).first() + if db_turbine_type: + for key, value in turbine_type.dict().items(): + setattr(db_turbine_type, key, value) + db.commit() + db.refresh(db_turbine_type) + return db_turbine_type + return None + + @staticmethod + def delete(db: Session, turbine_type_id: int): + db_turbine_type = db.query(WindTurbineType).filter(WindTurbineType.Id == turbine_type_id).first() + if db_turbine_type: + db.delete(db_turbine_type) + db.commit() + return True + return False + + +class WindParkRepository: + @staticmethod + def create(db: Session, park: WindParkCreate): + db_park = WindPark(**park.dict()) + db.add(db_park) + db.commit() + db.refresh(db_park) + return db_park + + @staticmethod + def get(db: Session, park_id: int): + return db.query(WindPark).filter(WindPark.Id == park_id).first() + + @staticmethod + def update(db: Session, park_id: int, park: WindParkCreate): + db_park = db.query(WindPark).filter(WindPark.Id == park_id).first() + if db_park: + for key, value in park.dict().items(): + setattr(db_park, key, value) + db.commit() + db.refresh(db_park) + return db_park + return None + + @staticmethod + def delete(db: Session, park_id: int): + db_park = db.query(WindPark).filter(WindPark.Id == park_id).first() + if db_park: + db.delete(db_park) + db.commit() + return True + return False + + +class WindParkTurbineRepository: + @staticmethod + def create(db: Session, park_turbine: WindParkTurbineCreate): + db_park_turbine = WindParkTurbine(**park_turbine.dict()) + db.add(db_park_turbine) + db.commit() + db.refresh(db_park_turbine) + return db_park_turbine + + @staticmethod + def get(db: Session, park_turbine_id: int): + return db.query(WindParkTurbine).filter(WindParkTurbine.turbine_id == park_turbine_id).first() + + @staticmethod + def update(db: Session, park_turbine_id: int, park_turbine: WindParkTurbineCreate): + db_park_turbine = db.query(WindParkTurbine).filter(WindParkTurbine.turbine_id == park_turbine_id).first() + if db_park_turbine: + for key, value in park_turbine.dict().items(): + setattr(db_park_turbine, key, value) + db.commit() + db.refresh(db_park_turbine) + return db_park_turbine + return None + + @staticmethod + def delete(db: Session, park_turbine_id: int): + db_park_turbine = db.query(WindParkTurbine).filter(WindParkTurbine.turbine_id == park_turbine_id).first() + if db_park_turbine: + db.delete(db_park_turbine) + db.commit() + return True + return False \ No newline at end of file diff --git a/server/src/data/schemas.py b/server/src/data/schemas.py index 6558a9d..87c0f09 100644 --- a/server/src/data/schemas.py +++ b/server/src/data/schemas.py @@ -1,4 +1,5 @@ from datetime import date +from typing import Optional from pydantic import BaseModel, Field from fastapi import Query @@ -19,4 +20,50 @@ class SFlorisInputParams(BaseModel): class SFlorisOutputData(BaseModel): file_name: str - data: list[float] \ No newline at end of file + data: list[float] + + +class WindTurbineTypeBase(BaseModel): + Name: str + Height: float + BladeLength: float + +class WindTurbineTypeCreate(WindTurbineTypeBase): + pass + +class WindTurbineTypeResponse(WindTurbineTypeBase): + Id: int + + class Config: + orm_mode = True + + +class WindParkBase(BaseModel): + Name: str + CenterLatitude: float + CenterLongitude: float + +class WindParkCreate(WindParkBase): + pass + +class WindParkResponse(WindParkBase): + Id: int + + class Config: + orm_mode = True + + +class WindParkTurbineBase(BaseModel): + wind_park_id: int + turbine_id: int + x_offset: int + y_offset: int + angle: Optional[int] = None + comment: Optional[str] = None + +class WindParkTurbineCreate(WindParkTurbineBase): + pass + +class WindParkTurbineResponse(WindParkTurbineBase): + 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 new file mode 100644 index 0000000..cac4098 --- /dev/null +++ b/server/src/routers/wind_park_router.py @@ -0,0 +1,87 @@ +from fastapi import APIRouter, HTTPException, Depends +from sqlalchemy.orm import Session +from typing import List + +from data.schemas import WindTurbineTypeCreate, WindTurbineTypeResponse, WindParkCreate, WindParkResponse, WindParkTurbineCreate, WindParkTurbineResponse +from data.database import get_db +from data.repository import WindTurbineTypeRepository, WindParkRepository, WindParkTurbineRepository + +router = APIRouter( + prefix="/api/wind", + tags=["Wind API"], +) + +# Wind Turbine Type CRUD +@router.post("/turbine_type/", response_model=WindTurbineTypeResponse) +async def create_turbine_type(turbine_type: WindTurbineTypeCreate, db: Session = Depends(get_db)): + return WindTurbineTypeRepository.create(db=db, turbine_type=turbine_type) + +@router.get("/turbine_type/{turbine_type_id}", response_model=WindTurbineTypeResponse) +async def read_turbine_type(turbine_type_id: int, db: Session = Depends(get_db)): + turbine_type = WindTurbineTypeRepository.get(db=db, turbine_type_id=turbine_type_id) + if turbine_type is None: + raise HTTPException(status_code=404, detail="Turbine Type not found") + return turbine_type + +@router.put("/turbine_type/{turbine_type_id}", response_model=WindTurbineTypeResponse) +async def update_turbine_type(turbine_type_id: int, turbine_type: WindTurbineTypeCreate, db: Session = Depends(get_db)): + updated_turbine_type = WindTurbineTypeRepository.update(db=db, turbine_type_id=turbine_type_id, turbine_type=turbine_type) + if updated_turbine_type is None: + raise HTTPException(status_code=404, detail="Turbine Type not found") + return updated_turbine_type + +@router.delete("/turbine_type/{turbine_type_id}", status_code=204) +async def delete_turbine_type(turbine_type_id: int, db: Session = Depends(get_db)): + result = WindTurbineTypeRepository.delete(db=db, turbine_type_id=turbine_type_id) + if not result: + raise HTTPException(status_code=404, detail="Turbine Type not found") + +# Wind Park CRUD +@router.post("/park/", response_model=WindParkResponse) +async def create_park(park: WindParkCreate, db: Session = Depends(get_db)): + return WindParkRepository.create(db=db, park=park) + +@router.get("/park/{park_id}", response_model=WindParkResponse) +async def read_park(park_id: int, db: Session = Depends(get_db)): + park = WindParkRepository.get(db=db, park_id=park_id) + if park is None: + raise HTTPException(status_code=404, detail="Park not found") + return park + +@router.put("/park/{park_id}", response_model=WindParkResponse) +async def update_park(park_id: int, park: WindParkCreate, db: Session = Depends(get_db)): + updated_park = WindParkRepository.update(db=db, park_id=park_id, park=park) + if updated_park is None: + raise HTTPException(status_code=404, detail="Park not found") + return updated_park + +@router.delete("/park/{park_id}", status_code=204) +async def delete_park(park_id: int, db: Session = Depends(get_db)): + result = WindParkRepository.delete(db=db, park_id=park_id) + if not result: + raise HTTPException(status_code=404, detail="Park not found") + +# Wind Park Turbine CRUD +@router.post("/park_turbine/", response_model=WindParkTurbineResponse) +async def create_park_turbine(park_turbine: WindParkTurbineCreate, db: Session = Depends(get_db)): + return WindParkTurbineRepository.create(db=db, park_turbine=park_turbine) + +@router.get("/park_turbine/{park_turbine_id}", response_model=WindParkTurbineResponse) +async def read_park_turbine(park_turbine_id: int, db: Session = Depends(get_db)): + park_turbine = WindParkTurbineRepository.get(db=db, park_turbine_id=park_turbine_id) + if park_turbine is None: + raise HTTPException(status_code=404, detail="Park Turbine not found") + return park_turbine + +@router.put("/park_turbine/{park_turbine_id}", response_model=WindParkTurbineResponse) +async def update_park_turbine(park_turbine_id: int, park_turbine: WindParkTurbineCreate, db: Session = Depends(get_db)): + updated_park_turbine = WindParkTurbineRepository.update(db=db, park_turbine_id=park_turbine_id, park_turbine=park_turbine) + if updated_park_turbine is None: + raise HTTPException(status_code=404, detail="Park Turbine not found") + return updated_park_turbine + +@router.delete("/park_turbine/{park_turbine_id}", status_code=204) +async def delete_park_turbine(park_turbine_id: int, db: Session = Depends(get_db)): + result = WindParkTurbineRepository.delete(db=db, park_turbine_id=park_turbine_id) + if not result: + raise HTTPException(status_code=404, detail="Park Turbine not found")