From d0a77b3f0f8ce270ee14518714604672adbcbd2b Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 19 Jun 2023 16:04:02 +0200 Subject: [PATCH] die meisten GET calls sind drin, 2 funktionieren noch nicht --- src/server/BreCal/__init__.py | 2 ++ src/server/BreCal/api/ships.py | 13 +++++++++ src/server/BreCal/impl/__init__.py | 1 + src/server/BreCal/impl/notifications.py | 4 +++ src/server/BreCal/impl/shipcalls.py | 31 +++++++++++++------- src/server/BreCal/impl/ships.py | 29 +++++++++++++++++++ src/server/BreCal/impl/times.py | 38 ++++++++++++++----------- src/server/BreCal/schemas/model.py | 6 ++-- 8 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 src/server/BreCal/api/ships.py create mode 100644 src/server/BreCal/impl/ships.py diff --git a/src/server/BreCal/__init__.py b/src/server/BreCal/__init__.py index 6af3f86..c8e5362 100644 --- a/src/server/BreCal/__init__.py +++ b/src/server/BreCal/__init__.py @@ -11,6 +11,7 @@ from .api import participant from .api import times from .api import notifications from .api import berths +from .api import ships sessions = dict() @@ -38,6 +39,7 @@ def create_app(test_config=None): app.register_blueprint(times.bp) app.register_blueprint(notifications.bp) app.register_blueprint(berths.bp) + app.register_blueprint(ships.bp) logging.basicConfig(filename='brecal.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s') local_db.initPool() diff --git a/src/server/BreCal/api/ships.py b/src/server/BreCal/api/ships.py new file mode 100644 index 0000000..9c72b83 --- /dev/null +++ b/src/server/BreCal/api/ships.py @@ -0,0 +1,13 @@ +from flask import Blueprint, request +from webargs.flaskparser import parser +from marshmallow import Schema, fields +from ..schemas import model +from .. import impl + +bp = Blueprint('ships', __name__) + + +@bp.route('/ships', methods=['get']) +def GetShips(): + token = request.headers.get('Authentication') + return impl.ships.GetShips(token) diff --git a/src/server/BreCal/impl/__init__.py b/src/server/BreCal/impl/__init__.py index 692ca46..1e4e005 100644 --- a/src/server/BreCal/impl/__init__.py +++ b/src/server/BreCal/impl/__init__.py @@ -4,3 +4,4 @@ from . import participant from . import shipcalls from . import times from . import verify +from . import ships diff --git a/src/server/BreCal/impl/notifications.py b/src/server/BreCal/impl/notifications.py index 120f248..a73095f 100644 --- a/src/server/BreCal/impl/notifications.py +++ b/src/server/BreCal/impl/notifications.py @@ -1,5 +1,9 @@ import json import logging +import pydapper + +from ..schemas import model +from .. import local_db def GetNotifications(options): """ diff --git a/src/server/BreCal/impl/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index c6f1a05..707f95a 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -1,21 +1,32 @@ import json import logging +import pydapper + +from ..schemas import model +from .. import local_db + def GetShipcalls(options): """ - :param options: A dictionary containing all the paramters for the Operations - options["participant_id"]: **Id of participant**. *Example: 2*. Id of participant entity requesting ship calls - + No parameters, gets all entries """ - # Implement your business logic here - # All the parameters are present in the options argument + # TODO: validate token - return json.dumps([{ - "description": "", - "id": "", - "type": "", - }]), 200 + 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(body): diff --git a/src/server/BreCal/impl/ships.py b/src/server/BreCal/impl/ships.py new file mode 100644 index 0000000..d2a7659 --- /dev/null +++ b/src/server/BreCal/impl/ships.py @@ -0,0 +1,29 @@ +import json +import logging +import pydapper + +from ..schemas import model +from .. import local_db + +def GetShips(token): + """ + No parameters, gets all entries + """ + + # TODO: validate token + + try: + + commands = pydapper.using(local_db.connection_pool) + data = commands.query("SELECT id, name, imo, callsign, participant_id, length, width, created, modified FROM ship ORDER BY name", model=model.Ship) + + except Exception as ex: + logging.error(ex) + print(ex) + return json.dumps("call failed"), 500 + + return json.dumps(data, default=model.obj_dict), 200 + + + + diff --git a/src/server/BreCal/impl/times.py b/src/server/BreCal/impl/times.py index dd45f03..06381a8 100644 --- a/src/server/BreCal/impl/times.py +++ b/src/server/BreCal/impl/times.py @@ -1,5 +1,9 @@ import json import logging +import pydapper + +from ..schemas import model +from .. import local_db def GetTimes(options): """ @@ -8,20 +12,20 @@ def GetTimes(options): """ - # Implement your business logic here - # All the parameters are present in the options argument + # TODO: validate token + + try: + commands = pydapper.using(local_db.connection_pool) + data = commands.query("SELECT id, start_planned, end_planned, duration_planned, start_actual, end_actual, duration_actual, shipcall_id, participant_id, created, modified FROM times " + + "WHERE times.shipcall_id = ?scid?", model=model.Times, param={"scid" : options["shipcall_id"]}) + + except Exception as ex: + logging.error(ex) + print(ex) + return json.dumps("call failed"), 500 + + return json.dumps(data, default=model.obj_dict), 200 - return json.dumps([{ - "duration_planned": "", - "end_actual": "", - "end_planned": "", - "id": "", - "participant_id": "", - "shipcall_id": "", - "start_actual": "", - "start_planned": "", - "remark":"" - }]), 200 def PostTimes(body): @@ -30,8 +34,7 @@ def PostTimes(body): :param body: The parsed body of the request """ - # Implement your business logic here - # All the parameters are present in the options argument + # This creates a *new* entry return 400 @@ -42,8 +45,9 @@ def PutTimes(body): :param body: The parsed body of the request """ - # Implement your business logic here - # All the parameters are present in the options argument + # This updates an *existing* entry + + return 400 diff --git a/src/server/BreCal/schemas/model.py b/src/server/BreCal/schemas/model.py index 15ea755..8e76db4 100644 --- a/src/server/BreCal/schemas/model.py +++ b/src/server/BreCal/schemas/model.py @@ -31,7 +31,7 @@ class Error(Schema): class GetVerifyInlineResp(Schema): pass - +@dataclass class Notification(Schema): id: int @@ -75,6 +75,7 @@ class Participant(Schema): class ParticipantList(Participant): pass +@dataclass class Shipcall(Schema): id: int ship_id: int @@ -124,7 +125,7 @@ class Shipcall(Schema): class ShipcallId(Schema): pass - +@dataclass class Times(Schema): id: int start_planned: datetime @@ -148,6 +149,7 @@ class Times(Schema): created = fields.DateTime() modified = fields.DateTime() +@dataclass class Ship(Schema): id: int name: str