You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
6.7 KiB
Python
218 lines
6.7 KiB
Python
from typing import List, Optional
|
|
from uuid import UUID
|
|
from fastapi import APIRouter, HTTPException, status, Query
|
|
|
|
from src.plant_transaction_data_simulations.model import PlantTransactionDataSimulations
|
|
from src.plant_transaction_data_simulations.schema import (
|
|
PlantTransactionDataSimulationsPagination,
|
|
PlantTransactionDataSimulationsRead,
|
|
PlantChartDataSimulations,
|
|
PlantTransactionChartSimulations,
|
|
PlantTransactionDataSimulationsCreate,
|
|
PlantTransactionDataSimulationsUpdate,
|
|
PlantTransactionFSImportSimulations,
|
|
)
|
|
from src.plant_transaction_data_simulations.service import (
|
|
get,
|
|
get_all,
|
|
get_charts,
|
|
create,
|
|
update,
|
|
delete,
|
|
update_fs_charts_from_matrix,
|
|
)
|
|
|
|
from src.database.service import CommonParameters
|
|
from src.database.core import DbSession
|
|
from src.auth.service import CurrentUser
|
|
from src.models import StandardResponse
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=StandardResponse[PlantTransactionDataSimulationsPagination])
|
|
async def get_transaction_datas(
|
|
db_session: DbSession,
|
|
common: CommonParameters,
|
|
simulation_id: UUID = Query(..., description="Simulation identifier"),
|
|
items_per_page: Optional[int] = Query(5),
|
|
search: Optional[str] = Query(None),
|
|
):
|
|
"""Get all transaction_data pagination."""
|
|
plant_transaction_data = await get_all(
|
|
db_session=db_session,
|
|
items_per_page=items_per_page,
|
|
search=search,
|
|
common=common,
|
|
simulation_id=simulation_id,
|
|
)
|
|
# return
|
|
return StandardResponse(
|
|
data=plant_transaction_data,
|
|
message="Data retrieved successfully",
|
|
)
|
|
|
|
@router.get("/charts", response_model=StandardResponse[PlantChartDataSimulations])
|
|
async def get_chart_data(
|
|
db_session: DbSession,
|
|
common: CommonParameters,
|
|
simulation_id: UUID = Query(..., description="Simulation identifier"),
|
|
):
|
|
chart_data, bep_year, bep_total_lcc = await get_charts(
|
|
db_session=db_session,
|
|
common=common,
|
|
simulation_id=simulation_id,
|
|
)
|
|
if not chart_data:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="No chart data found.",
|
|
)
|
|
return StandardResponse(
|
|
data={
|
|
"items": chart_data,
|
|
"bep_year": bep_year,
|
|
"bep_total_lcc": bep_total_lcc,
|
|
},
|
|
message="Data retrieved successfully",
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/charts/fs/import",
|
|
response_model=StandardResponse[List[PlantTransactionDataSimulationsRead]],
|
|
)
|
|
async def import_fs_chart_data(
|
|
db_session: DbSession,
|
|
payload: PlantTransactionFSImportSimulations,
|
|
current_user: CurrentUser,
|
|
):
|
|
updated_records, missing_years = await update_fs_charts_from_matrix(
|
|
db_session=db_session,
|
|
payload=payload,
|
|
updated_by=current_user.name,
|
|
simulation_id=payload.simulation_id,
|
|
)
|
|
|
|
if not updated_records:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="No plant transaction data found for the supplied years.",
|
|
)
|
|
|
|
message = "FS chart data updated successfully"
|
|
if missing_years:
|
|
years_text = ", ".join(str(year) for year in sorted(missing_years))
|
|
message += f"; missing years: {years_text}"
|
|
|
|
return StandardResponse(data=updated_records, message=message)
|
|
|
|
@router.get(
|
|
"/{transaction_data_id}", response_model=StandardResponse[PlantTransactionDataSimulationsRead]
|
|
)
|
|
async def get_transaction_data(db_session: DbSession, transaction_data_id: str):
|
|
transaction_data = await get(
|
|
db_session=db_session, transaction_data_id=transaction_data_id
|
|
)
|
|
if not transaction_data:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="A data with this id does not exist.",
|
|
)
|
|
|
|
return StandardResponse(
|
|
data=transaction_data, message="Data retrieved successfully"
|
|
)
|
|
|
|
|
|
@router.post("", response_model=StandardResponse[PlantTransactionDataSimulationsRead])
|
|
async def create_transaction_data(
|
|
db_session: DbSession,
|
|
transaction_data_in: PlantTransactionDataSimulationsCreate,
|
|
current_user: CurrentUser,
|
|
):
|
|
transaction_data_in.created_by = current_user.name
|
|
transaction_data = await create(
|
|
db_session=db_session, transaction_data_in=transaction_data_in
|
|
)
|
|
|
|
return StandardResponse(data=transaction_data, message="Data created successfully")
|
|
|
|
@router.put(
|
|
"/bulk", response_model=StandardResponse[List[PlantTransactionDataSimulationsRead]]
|
|
)
|
|
async def bulk_update_transaction_data(
|
|
db_session: DbSession,
|
|
ids: List[str],
|
|
updates: List[PlantTransactionDataSimulationsUpdate],
|
|
current_user: CurrentUser,
|
|
):
|
|
if len(ids) != len(updates):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="The number of IDs must match the number of update objects.",
|
|
)
|
|
|
|
# Set updated_by for each update object
|
|
for update_obj in updates:
|
|
update_obj.updated_by = current_user.name
|
|
|
|
updated_records = await update(
|
|
db_session=db_session,
|
|
ids=ids,
|
|
updates=updates,
|
|
)
|
|
|
|
return StandardResponse(
|
|
data=updated_records,
|
|
message="Bulk update completed successfully",
|
|
)
|
|
|
|
@router.put(
|
|
"/{transaction_data_id}", response_model=StandardResponse[PlantTransactionDataSimulationsRead]
|
|
)
|
|
async def update_transaction_data(
|
|
db_session: DbSession,
|
|
transaction_data_id: str,
|
|
transaction_data_in: PlantTransactionDataSimulationsUpdate,
|
|
current_user: CurrentUser,
|
|
):
|
|
transaction_data = await get(
|
|
db_session=db_session, transaction_data_id=transaction_data_id
|
|
)
|
|
|
|
if not transaction_data:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="A data with this id does not exist.",
|
|
)
|
|
transaction_data_in.updated_by = current_user.name
|
|
|
|
return StandardResponse(
|
|
data=await update(
|
|
db_session=db_session,
|
|
transaction_data=transaction_data,
|
|
transaction_data_in=transaction_data_in,
|
|
),
|
|
message="Data updated successfully",
|
|
)
|
|
|
|
|
|
@router.delete(
|
|
"/{transaction_data_id}", response_model=StandardResponse[PlantTransactionDataSimulationsRead]
|
|
)
|
|
async def delete_transaction_data(db_session: DbSession, transaction_data_id: str):
|
|
transaction_data = await get(
|
|
db_session=db_session, transaction_data_id=transaction_data_id
|
|
)
|
|
|
|
if not transaction_data:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=[{"msg": "A data with this id does not exist."}],
|
|
)
|
|
|
|
await delete(db_session=db_session, transaction_data_id=transaction_data_id)
|
|
|
|
return StandardResponse(message="Data deleted successfully", data=transaction_data)
|