Fixed SQL and added endpoint for history data

This commit is contained in:
Daniel Schick 2023-12-21 07:25:08 +01:00
parent 783f9f5089
commit a52cc27d69
11 changed files with 75 additions and 18 deletions

View File

@ -54,7 +54,7 @@ ADD CONSTRAINT `FK_USER_PART`
CREATE TABLE `bremen_calling_devel`.`history` ( CREATE TABLE `bremen_calling_devel`.`history` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`participant_id` INT UNSIGNED NOT NULL, `participant_id` INT UNSIGNED NOT NULL,
`ship_id` INT UNSIGNED NOT NULL, `shipcall_id` INT UNSIGNED NOT NULL,
`timestamp` DATETIME NOT NULL COMMENT 'Time of saving', `timestamp` DATETIME NOT NULL COMMENT 'Time of saving',
`eta` DATETIME NOT NULL COMMENT 'Current ETA / ETD value (depends if shipcall or times were saved)', `eta` DATETIME NOT NULL COMMENT 'Current ETA / ETD value (depends if shipcall or times were saved)',
`type` INT NOT NULL COMMENT 'shipcall or times', `type` INT NOT NULL COMMENT 'shipcall or times',
@ -66,7 +66,7 @@ COMMENT = 'This table stores a history of changes made to shipcalls so that ever
ALTER TABLE `bremen_calling_devel`.`history` ALTER TABLE `bremen_calling_devel`.`history`
ADD INDEX `FK_HISTORY_PARTICIPANT_idx` (`participant_id` ASC) VISIBLE, ADD INDEX `FK_HISTORY_PARTICIPANT_idx` (`participant_id` ASC) VISIBLE,
ADD INDEX `FK_HISTORY_SHIP_idx` (`ship_id` ASC) VISIBLE; ADD INDEX `FK_HISTORY_SHIPCALL_idx` (`shipcall_id` ASC) VISIBLE;
; ;
ALTER TABLE `bremen_calling_devel`.`history` ALTER TABLE `bremen_calling_devel`.`history`
ADD CONSTRAINT `FK_HISTORY_PARTICIPANT` ADD CONSTRAINT `FK_HISTORY_PARTICIPANT`
@ -74,8 +74,8 @@ ADD CONSTRAINT `FK_HISTORY_PARTICIPANT`
REFERENCES `bremen_calling_devel`.`participant` (`id`) REFERENCES `bremen_calling_devel`.`participant` (`id`)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION, ON UPDATE NO ACTION,
ADD CONSTRAINT `FK_HISTORY_SHIP` ADD CONSTRAINT `FK_HISTORY_SHIPCALL`
FOREIGN KEY (`ship_id`) FOREIGN KEY (`shipcall_id`)
REFERENCES `bremen_calling_devel`.`ship` (`id`) REFERENCES `bremen_calling_devel`.`shipcall` (`id`)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION; ON UPDATE NO ACTION;

View File

@ -12,6 +12,7 @@ from .api import berths
from .api import ships from .api import ships
from .api import login from .api import login
from .api import user from .api import user
from .api import history
from BreCal.brecal_utils.file_handling import get_project_root, ensure_path from BreCal.brecal_utils.file_handling import get_project_root, ensure_path
from BreCal.brecal_utils.test_handling import execute_test_with_pytest, execute_coverage_test from BreCal.brecal_utils.test_handling import execute_test_with_pytest, execute_coverage_test
@ -59,7 +60,7 @@ def create_app(test_config=None):
app.register_blueprint(ships.bp) app.register_blueprint(ships.bp)
app.register_blueprint(login.bp) app.register_blueprint(login.bp)
app.register_blueprint(user.bp) app.register_blueprint(user.bp)
app.register_blueprint(history.bp)
logging.basicConfig(filename='brecaldevel.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s') logging.basicConfig(filename='brecaldevel.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')
local_db.initPool(os.path.dirname(app.instance_path)) local_db.initPool(os.path.dirname(app.instance_path))

View File

@ -0,0 +1,21 @@
from flask import Blueprint, request
from .. import impl
from ..services.auth_guard import auth_guard
import json
bp = Blueprint('history', __name__)
@bp.route('/history', methods=['get'])
@auth_guard() # no restriction by role
def GetParticipant():
if 'Authorization' in request.headers:
token = request.headers.get('Authorization')
options = {}
if not 'shipcall_id' in request.args:
return json.dumps("missing parameter"), 400
options["shipcall_id"] = request.args.get("shipcall_id")
return impl.history.GetHistory(options)
else:
return json.dumps("not authenticated"), 403

View File

@ -6,3 +6,4 @@ from . import times
from . import ships from . import ships
from . import login from . import login
from . import user from . import user
from . import history

View File

@ -10,8 +10,6 @@ def GetBerths(token):
No parameters, gets all entries No parameters, gets all entries
""" """
# TODO: validate token
try: try:
pooledConnection = local_db.getPoolConnection() pooledConnection = local_db.getPoolConnection()
commands = pydapper.using(pooledConnection) commands = pydapper.using(pooledConnection)

View File

@ -0,0 +1,31 @@
import json
import logging
import pydapper
from ..schemas import model
from .. import local_db
def GetHistory(options):
"""
:param options: A dictionary containing all the paramters for the Operations
options["shipcall_id"]: **Id of shipcall**.
"""
try:
pooledConnection = local_db.getPoolConnection()
commands = pydapper.using(pooledConnection)
if "shipcall_id" in options and options["shipcall_id"]:
data = commands.query("SELECT id, participant_id, shipcall_id, timestamp, eta, type, operation FROM history WHERE shipcall_id = ?shipcallid?", model=model.History, param={"shipcallid" : options["shipcall_id"]})
pooledConnection.close()
except Exception as ex:
logging.error(ex)
print(ex)
result = {}
result["message"] = "call failed"
return json.dumps("call failed"), 500
return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'}

View File

@ -11,7 +11,6 @@ def GetParticipant(options):
options["user_id"]: **Id of user**. *Example: 2*. User id returned by login call. options["user_id"]: **Id of user**. *Example: 2*. User id returned by login call.
""" """
# TODO: validate token
try: try:
pooledConnection = local_db.getPoolConnection() pooledConnection = local_db.getPoolConnection()

View File

@ -13,8 +13,6 @@ def GetShipcalls(options):
No parameters, gets all entries No parameters, gets all entries
""" """
# TODO: validate token
try: try:
pooledConnection = local_db.getPoolConnection() pooledConnection = local_db.getPoolConnection()

View File

@ -10,8 +10,6 @@ def GetShips(token):
No parameters, gets all entries No parameters, gets all entries
""" """
# TODO: validate token
try: try:
pooledConnection = local_db.getPoolConnection() pooledConnection = local_db.getPoolConnection()

View File

@ -14,8 +14,6 @@ def GetTimes(options):
""" """
# TODO: validate token
try: try:
pooledConnection = local_db.getPoolConnection() pooledConnection = local_db.getPoolConnection()

View File

@ -257,6 +257,18 @@ class ShipSchema(Schema):
modified = fields.DateTime(allow_none=True, metadata={'Required':False}) modified = fields.DateTime(allow_none=True, metadata={'Required':False})
deleted = fields.Int(allow_none=True, metadata={'Required':False}) deleted = fields.Int(allow_none=True, metadata={'Required':False})
@dataclass
class History(Schema):
id: int
participant_id: int
ship_id: int
timestamp: datetime
eta: datetime
type: int
operation: int
class TimesId(Schema): class TimesId(Schema):
pass pass