This repository has been archived on 2025-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
BreCal/src/server/BreCal/impl/shipcalls.py
Daniel Schick 15cc4bf8da API up and running.
There are still open issues but in principle, it is working
2023-06-22 10:56:13 +02:00

111 lines
2.9 KiB
Python

import json
import logging
import pydapper
from ..schemas import model
from .. import local_db
def GetShipcalls(options):
"""
No parameters, gets all entries
"""
# TODO: validate token
try:
commands = pydapper.using(local_db.connection_pool)
data = commands.query("SELECT id, ship_id, type, eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, " +
"flags, pier_side, bunkering, replenishing, draft, tidal_window_from, tidal_window_to, rain_sensitive_cargo, recommended_tugs, " +
"created, modified FROM shipcall WHERE eta IS NULL OR eta >= DATE(NOW() - INTERVAL 2 DAY) " +
"ORDER BY eta", model=model.Shipcall)
except Exception as ex:
logging.error(ex)
print(ex)
return json.dumps("call failed"), 500
return json.dumps(data, default=model.obj_dict), 200
def PostShipcalls(schemaModel):
"""
:param schemaModel: The deserialized dict of the request
"""
# TODO: Validate the upload data
# This creates a *new* entry
try:
commands = pydapper.using(local_db.connection_pool)
query = "INSERT INTO shipcall ("
isNotFirst = False
for key in schemaModel.keys():
if key == "id":
continue
if isNotFirst:
query += ","
isNotFirst = True
query += key
query += ") VALUES ("
isNotFirst = False
for key in schemaModel.keys():
if key == "id":
continue
if isNotFirst:
query += ","
isNotFirst = True
query += "?" + key + "?"
query += ")"
commands.execute(query, schemaModel)
new_id = commands.execute_scalar("select last_insert_id()")
return json.dumps({"id" : new_id}), 201
except Exception as ex:
logging.error(ex)
print(ex)
return json.dumps("call failed"), 500
def PutShipcalls(schemaModel):
"""
:param schemaModel: The deserialized dict of the request
"""
# This updates an *existing* entry
try:
commands = pydapper.using(local_db.connection_pool)
query = "UPDATE shipcall SET "
isNotFirst = False
for key in schemaModel.keys():
if key == "id":
continue
if isNotFirst:
query += ", "
isNotFirst = True
query += key + " = ?" + key + "? "
query += "WHERE id = ?id?"
affected_rows = commands.execute(query, param=schemaModel)
if affected_rows == 1:
return json.dumps({"id" : schemaModel["id"]}), 200
return json.dumps("no such record"), 404
except Exception as ex:
logging.error(ex)
print(ex)
return json.dumps("call failed"), 500