import json import logging import pydapper from ..schemas import model from .. import local_db from BreCal.database.sql_queries import SQLQuery def GetShips(token): """ No parameters, gets all entries """ try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) # query = SQLQuery.get_ships() # data = commands.query(query, model=model.Ship) data = commands.query("SELECT id, name, imo, callsign, participant_id, length, width, is_tug, bollard_pull, eni, created, modified, deleted FROM ship ORDER BY name", model=model.Ship) return json.dumps(data, default=model.obj_dict), 200, {'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'} finally: if pooledConnection is not None: pooledConnection.close() def PostShip(schemaModel): """ :param schemaModel: The deserialized model of the record to be inserted """ # TODO: Validate the incoming data # This creates a *new* entry try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) # query = SQLQuery.create_sql_query_ship_post(schemaModel) query = "INSERT INTO ship (" 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) # nquery = SQLQuery.get_last_insert_id() # new_id = commands.execute_scalar(nquery) new_id = commands.execute_scalar("select last_insert_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 PutShip(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 = SQLQuery.create_sql_query_ship_put(schemaModel) query = "UPDATE ship 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) pooledConnection.close() return json.dumps({"id" : schemaModel["id"]}), 200, {'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 DeleteShip(options): """ :param options: A dictionary containing all the paramters for the Operations options["id"] """ try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) # query = SQLQuery.get_ship_delete_by_id() # affected_rows = commands.execute(query, param={"id" : options["id"]}) affected_rows = commands.execute("UPDATE ship SET deleted = 1 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'}