git_brcal/src/server/BreCal/impl/times.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

126 lines
3.4 KiB
Python

import json
import logging
import pydapper
from ..schemas import model
from .. import local_db
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:
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
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:
commands = pydapper.using(local_db.connection_pool)
query = "INSERT INTO times ("
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 PutTimes(schemaModel):
"""
:param schemaModel: The deserialized model of the record to be inserted
"""
# This updates an *existing* entry
try:
commands = pydapper.using(local_db.connection_pool)
query = "UPDATE times 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
def DeleteTimes(options):
"""
:param options: A dictionary containing all the paramters for the Operations
options["id"]
"""
try:
commands = pydapper.using(local_db.connection_pool)
affected_rows = commands.execute("DELETE FROM times WHERE id = ?id?", param={"id" : options["id"]})
if affected_rows == 1:
return json.dumps({"id" : options["id"]}), 200
return json.dumps("no such record"), 404
except Exception as ex:
logging.error(ex)
print(ex)
return json.dumps("call failed"), 500