Skip to content
Snippets Groups Projects
Commit cbad53cd authored by Sabine Schröder's avatar Sabine Schröder
Browse files

#9: added API for deleting fields (f. ex. roles) from stationmeta

parent 9710d33e
Branches
No related tags found
No related merge requests found
Pipeline #47349 passed
...@@ -4,7 +4,7 @@ Create, Read, Update, Delete functionality ...@@ -4,7 +4,7 @@ Create, Read, Update, Delete functionality
""" """
from sqlalchemy import cast, Text, insert, update from sqlalchemy import cast, Text, insert, update, delete
from typing import List from typing import List
from geoalchemy2.types import Geometry from geoalchemy2.types import Geometry
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
...@@ -207,18 +207,11 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc ...@@ -207,18 +207,11 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc
db_object = get_unique_stationmeta_role(db, db_role.role, db_role.contact_id, db_role.status) db_object = get_unique_stationmeta_role(db, db_role.role, db_role.contact_id, db_role.status)
if db_object: if db_object:
role_id = db_object.id role_id = db_object.id
print("found role_id:",role_id)
else: else:
print("did not find role: now inserting new role")
print(r)
print(db_role.role)
print(db_role.status)
print(db_role.contact_id)
db.add(db_role) db.add(db_role)
db.commit() db.commit()
db.refresh(db_role) db.refresh(db_role)
role_id = db_role.id role_id = db_role.id
print("did not find role: inserted new role_id ",role_id)
db.execute(insert(stationmeta_core_stationmeta_roles_table).values(station_id=station_id, role_id=role_id)) db.execute(insert(stationmeta_core_stationmeta_roles_table).values(station_id=station_id, role_id=role_id))
db.commit() db.commit()
# store annotations and update association table # store annotations and update association table
...@@ -279,3 +272,18 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc ...@@ -279,3 +272,18 @@ def patch_stationmeta(db: Session, station_id: int, stationmeta: StationmetaPatc
# there's a mismatch with coordinates --> how to automatically switch back and forth?! # there's a mismatch with coordinates --> how to automatically switch back and forth?!
db_stationmeta.coordinates = tmp_coordinates db_stationmeta.coordinates = tmp_coordinates
return db_stationmeta return db_stationmeta
def delete_stationmeta_field(db: Session, station_id: int, keyword: str):
if keyword == 'roles':
db.execute(delete(stationmeta_core_stationmeta_roles_table).where(stationmeta_core_stationmeta_roles_table.c.station_id==station_id))
# stationmeta_core_stationmeta_roles_table.delete().where(stationmeta_core_stationmeta_roles_table.c.station_id==station_id)
# problem with automatic conversion of coordinates (although not explicitly fetched from database)
# ==> next two lines are a workaround
db_stationmeta = db.query(models.StationmetaCore).get(station_id)
tmp_coordinates = db_stationmeta.coordinates
db_stationmeta.coordinates = get_geom_from_coordinates(db_stationmeta.coordinates)
db.commit()
# there's a mismatch with coordinates --> how to automatically switch back and forth?!
db_stationmeta.coordinates = tmp_coordinates
return db_stationmeta
...@@ -65,3 +65,10 @@ def patch_stationmeta_core(station_code: str, stationmeta: schemas.StationmetaPa ...@@ -65,3 +65,10 @@ def patch_stationmeta_core(station_code: str, stationmeta: schemas.StationmetaPa
if db_stationmeta is None: if db_stationmeta is None:
raise HTTPException(status_code=404, detail="Station for patching not found.") raise HTTPException(status_code=404, detail="Station for patching not found.")
return crud.patch_stationmeta(db=db, station_id=db_stationmeta.id, stationmeta=stationmeta) return crud.patch_stationmeta(db=db, station_id=db_stationmeta.id, stationmeta=stationmeta)
@router.patch('/stationmeta/delete_field/{station_code}', response_model=schemas.StationmetaPatch)
def patch_stationmeta_core(station_code: str, field: str, db: Session = Depends(get_db)):
db_stationmeta = crud.get_stationmeta(db, station_code=station_code)
if db_stationmeta is None:
raise HTTPException(status_code=404, detail="Station for deleting field not found.")
return crud.delete_stationmeta_field(db=db, station_id=db_stationmeta.id, keyword=field)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment