import json import logging import pydapper from ..schemas import model from .. import local_db from BreCal.database.update_database import evaluate_shipcall_state def GetTimes(options): """ :param options: A dictionary containing all the paramters for the Operations options["shipcall_id"]: **Id**. *Example: 42*. Id of referenced ship call. """ # TODO: validate token try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, eta_berth, eta_berth_fixed, etd_berth, etd_berth_fixed, lock_time, lock_time_fixed, " + "zone_entry, zone_entry_fixed, operations_start, operations_end, remarks, shipcall_id, participant_id, " + "berth_id, berth_info, pier_side, participant_type, created, modified FROM times " + "WHERE times.shipcall_id = ?scid?", model=model.Times, param={"scid" : options["shipcall_id"]}) pooledConnection.close() except Exception as ex: logging.error(ex) print(ex) result = {} result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} def PostTimes(schemaModel): """ :param schemaModel: The deserialized model of the record to be inserted """ # TODO: Validate the upload data # This creates a *new* entry try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) query = "INSERT INTO times (" isNotFirst = False for key in schemaModel.keys(): if key == "id": continue if key == "created": continue if key == "modified": continue if isNotFirst: query += "," isNotFirst = True query += key query += ") VALUES (" isNotFirst = False for key in schemaModel.keys(): if key == "id": continue if key == "created": continue if key == "modified": continue if isNotFirst: query += "," isNotFirst = True query += "?" + key + "?" query += ")" commands.execute(query, schemaModel) new_id = commands.execute_scalar("select last_insert_id()") # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall' evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id' pooledConnection.close() return json.dumps({"id" : new_id}), 201, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) print(ex) result = {} result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} def PutTimes(schemaModel): """ :param schemaModel: The deserialized model of the record to be inserted """ # This updates an *existing* entry try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) query = "UPDATE times SET " isNotFirst = False for key in schemaModel.keys(): if key == "id": continue if key == "created": continue if key == "modified": continue if isNotFirst: query += ", " isNotFirst = True query += key + " = ?" + key + "? " query += "WHERE id = ?id?" affected_rows = commands.execute(query, param=schemaModel) # apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall' evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id' pooledConnection.close() # if affected_rows == 1: # this doesn't work as expected return json.dumps({"id" : schemaModel["id"]}), 200, {'Content-Type': 'application/json; charset=utf-8'} # return json.dumps("no such record"), 404, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) print(ex) result = {} result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} def DeleteTimes(options): """ :param options: A dictionary containing all the paramters for the Operations options["id"] """ try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) affected_rows = commands.execute("DELETE FROM times WHERE id = ?id?", param={"id" : options["id"]}) pooledConnection.close() if affected_rows == 1: return json.dumps({"id" : options["id"]}), 200, {'Content-Type': 'application/json; charset=utf-8'} result = {} result["message"] = "no such record" return json.dumps(result), 404, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) print(ex) result = {} result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'}