From 9ca0430fda9e78019da17e4cf7a37dcfed6dfc52 Mon Sep 17 00:00:00 2001 From: maxim Date: Sun, 24 Nov 2024 12:55:59 +0400 Subject: [PATCH] [test-entity]: router fix --- front/package-lock.json | 6 ++-- front/src/api/wind/service.ts | 9 ++++- .../components/pages/park-page/component.tsx | 35 +++++++++++++++---- .../components/pages/parks-page/component.tsx | 5 +-- server/src/data/repository.py | 4 +-- server/src/routers/wind_park_router.py | 6 ++-- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/front/package-lock.json b/front/package-lock.json index e3e3155..a0bcd0d 100644 --- a/front/package-lock.json +++ b/front/package-lock.json @@ -2811,7 +2811,7 @@ "version": "15.7.13", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.13.tgz", "integrity": "sha512-hCZTSvwbzWGvhqxp/RqVqwU999pBf2vp7hzIjiYOsl8wqOmUxkQ6ddw1cV3l8811+kdUFus/q4d1Y3E3SyEifA==", - "devOptional": true + "dev": true }, "node_modules/@types/qs": { "version": "6.9.16", @@ -2829,7 +2829,7 @@ "version": "18.3.8", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.8.tgz", "integrity": "sha512-syBUrW3/XpnW4WJ41Pft+I+aPoDVbrBVQGEnbD7NijDGlVC+8gV/XKRY+7vMDlfPpbwYt0l1vd/Sj8bJGMbs9Q==", - "devOptional": true, + "dev": true, "dependencies": { "@types/prop-types": "*", "csstype": "^3.0.2" @@ -4262,7 +4262,7 @@ "version": "3.1.3", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==", - "devOptional": true + "dev": true }, "node_modules/debug": { "version": "4.3.7", diff --git a/front/src/api/wind/service.ts b/front/src/api/wind/service.ts index af8604d..ca2b2ea 100644 --- a/front/src/api/wind/service.ts +++ b/front/src/api/wind/service.ts @@ -98,7 +98,9 @@ export const updatePark = async ( ); } if (t.delete) { - return api.delete(`${WIND_ENDPOINTS.parkTurbine}/${t.id}`); + return api.delete( + `${WIND_ENDPOINTS.parkTurbine}/${parkPesponse.data.id}/${t.id}`, + ); } return api.put( `${WIND_ENDPOINTS.parkTurbine}/${parkPesponse.data.id}/${t.id}`, @@ -108,3 +110,8 @@ export const updatePark = async ( ); return getParkWithTurbines(id); }; + +export const deletePark = (id: number) => { + const url = `${WIND_ENDPOINTS.park}/${id}`; + return api.delete(url); +}; diff --git a/front/src/components/pages/park-page/component.tsx b/front/src/components/pages/park-page/component.tsx index 1272d75..0cbcb8f 100644 --- a/front/src/components/pages/park-page/component.tsx +++ b/front/src/components/pages/park-page/component.tsx @@ -4,7 +4,13 @@ import { ParkWithTurbines, updatePark, } from '@api/wind'; -import { Button, Heading, NumberField, TextInput } from '@components/ui'; +import { + Button, + Dialog, + Heading, + NumberField, + TextInput, +} from '@components/ui'; import { ParkTurbines } from '@components/ux'; import { Controller, useForm } from '@utils/form'; import { ROUTES, useRoute } from '@utils/route'; @@ -18,6 +24,7 @@ import { unpackPark } from './utils'; export function ParkPage() { const [park, setPark] = useState(null); const [pending, setPending] = useState(false); + const [error, setError] = useState(null); const params = useParams(); const navigate = useNavigate(); const route = useRoute(); @@ -45,12 +52,20 @@ export function ParkPage() { event.preventDefault(); setPending(true); if (isEdit) { - const response = await updatePark(getValues(), id); - setPark(response.data); - reset(unpackPark(response.data)); + const { data, error } = await updatePark(getValues(), id); + if (error) { + setError('Something went wrong'); + return; + } + setPark(data); + reset(unpackPark(data)); } else { - const response = await createPark(getValues()); - navigate(ROUTES.park.path.replace(':id', String(response.data.id))); + const { data, error } = await createPark(getValues()); + if (error) { + setError('Something went wrong'); + return; + } + navigate(ROUTES.park.path.replace(':id', String(data.id))); } setPending(false); }; @@ -100,6 +115,14 @@ export function ParkPage() { )} /> + setError(null)} + > + + ); } diff --git a/front/src/components/pages/parks-page/component.tsx b/front/src/components/pages/parks-page/component.tsx index e7dd82e..a006c1d 100644 --- a/front/src/components/pages/parks-page/component.tsx +++ b/front/src/components/pages/parks-page/component.tsx @@ -1,4 +1,4 @@ -import { getParks, Park } from '@api/wind'; +import { deletePark, getParks, Park } from '@api/wind'; import { Button, Heading } from '@components/ui'; import { DataGrid } from '@components/ui/data-grid'; import { ROUTES, useRoute } from '@utils/route'; @@ -27,7 +27,8 @@ export function ParksPage() { }; const handleDeleteButtonClick = async () => { - // + await deletePark(selected.id); + fetchParks(); }; return ( diff --git a/server/src/data/repository.py b/server/src/data/repository.py index edcadc5..926c134 100644 --- a/server/src/data/repository.py +++ b/server/src/data/repository.py @@ -158,8 +158,8 @@ class WindParkTurbineRepository: 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() + def delete(db: Session, park_id: int, turbine_id: int): + db_park_turbine = db.query(WindParkTurbine).filter(WindParkTurbine.wind_park_id == park_id, WindParkTurbine.turbine_id == turbine_id).first() if db_park_turbine: db.delete(db_park_turbine) db.commit() diff --git a/server/src/routers/wind_park_router.py b/server/src/routers/wind_park_router.py index 07a40aa..3256723 100644 --- a/server/src/routers/wind_park_router.py +++ b/server/src/routers/wind_park_router.py @@ -94,9 +94,9 @@ async def update_park_turbine(park_id: int, turbine_id: int, park_turbine: WindP 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) +@router.delete("/park_turbine/{park_id}/{turbine_id}", status_code=204) +async def delete_park_turbine(park_id: int, turbine_id: int, db: Session = Depends(get_db)): + result = WindParkTurbineRepository.delete(db=db, park_id=park_id, turbine_id=turbine_id) if not result: raise HTTPException(status_code=404, detail="Park Turbine not found")