diff --git a/src/server/BreCal/impl/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index 587e321..55a98c1 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -108,6 +108,14 @@ def PutShipcalls(schemaModel): pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) + # test if object to update is found + + sentinel = object() + theshipcall = commands.query_single_or_default("SELECT * FROM shipcall where id = ?id?", sentinel, param={"id" : schemaModel["id"]}) + if theshipcall is sentinel: + pooledConnection.close() + return json.dumps("no such record"), 404, {'Content-Type': 'application/json; charset=utf-8'} + query = "UPDATE shipcall SET " isNotFirst = False for key in schemaModel.keys(): @@ -124,19 +132,34 @@ def PutShipcalls(schemaModel): affected_rows = commands.execute(query, param=schemaModel) pquery = "SELECT id, participant_id FROM shipcall_participant_map where shipcall_id = ?id?" - pdata = commands.query(pquery,param={"id" : schemaModel.id}) + pdata = commands.query(pquery,param={"id" : schemaModel["id"]}) # existing list of assignments # loop across passed participant ids, creating entries for those not present in pdata + for participant_id in schemaModel["participants"]: + found_participant = False + for elem in pdata: + if elem["participant_id"] == participant_id: + found_participant = True + break + if not found_participant: + nquery = "INSERT INTO shipcall_participant_map (shipcall_id, participant_id) VALUES (?shipcall_id?, ?participant_id?)" + commands.execute(nquery, param={"shipcall_id" : schemaModel["id"], "participant_id" : participant_id}) + # loop across existing pdata entries, deleting those not present in participant list - + for elem in pdata: + found_participant = False + for participant_id in schemaModel["participants"]: + if(participant_id == elem["participant_id"]): + found_participant = True + break; + if not found_participant: + dquery = "DELETE FROM shipcall_participant_map WHERE id = ?existing_id?" + commands.execute(dquery, param={"existing_id" : elem["id"]}) pooledConnection.close() - if affected_rows == 1: - return json.dumps({"id" : schemaModel["id"]}), 200 - - return json.dumps("no such record"), 404, {'Content-Type': 'application/json; charset=utf-8'} + return json.dumps({"id" : schemaModel["id"]}), 200 except Exception as ex: logging.error(ex)