Implemented bot endpoint with DB requests and other tweaks
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
from quart import Blueprint, jsonify, request, abort
|
||||
from internal.db_wrappers import SystemDbController
|
||||
from quart import Blueprint, jsonify, request, abort, current_app
|
||||
from werkzeug.exceptions import HTTPException
|
||||
from internal.types import System
|
||||
|
||||
systems_bp = Blueprint('systems', __name__)
|
||||
db_h = SystemDbController()
|
||||
|
||||
|
||||
@systems_bp.route("/", methods=['POST'])
|
||||
@@ -19,24 +17,24 @@ async def create_system_route():
|
||||
abort(400, "Request body must be JSON") # Bad Request
|
||||
|
||||
if '_id' in request_data:
|
||||
id_search_result = await db_h.find_system({"_id": request_data["_id"]})
|
||||
id_search_result = await current_app.sys_db_h.find_system({"_id": request_data["_id"]})
|
||||
if id_search_result:
|
||||
# If _id is provided and exists, return conflict
|
||||
abort(409, f"System with ID '{request_data['_id']}' already exists")
|
||||
|
||||
# Check if name exists (optional, depending on requirements)
|
||||
if 'name' in request_data:
|
||||
name_search_result = await db_h.find_system({"name": request_data["name"]})
|
||||
name_search_result = await current_app.sys_db_h.find_system({"name": request_data["name"]})
|
||||
if name_search_result:
|
||||
abort(409, f"System with name '{request_data['name']}' already exists")
|
||||
|
||||
# Check if frequency_khz exists (optional, depending on requirements)
|
||||
if 'frequency_khz' in request_data:
|
||||
freq_search_result = await db_h.find_system({"frequency_khz": request_data["frequency_khz"]})
|
||||
freq_search_result = await current_app.sys_db_h.find_system({"frequency_khz": request_data["frequency_khz"]})
|
||||
if freq_search_result:
|
||||
abort(409, f"System with frequency '{request_data['frequency_khz']}' already exists")
|
||||
|
||||
created_system = await db_h.create_system(request_data)
|
||||
created_system = await current_app.sys_db_h.create_system(request_data)
|
||||
|
||||
if created_system:
|
||||
print("Created new system:", created_system)
|
||||
@@ -56,7 +54,7 @@ async def list_systems_route():
|
||||
"""API endpoint to get a list of all systems."""
|
||||
print("\n--- Handling GET /systems ---")
|
||||
try:
|
||||
all_systems = await db_h.find_all_systems()
|
||||
all_systems = await current_app.sys_db_h.find_all_systems()
|
||||
|
||||
return jsonify([system.to_dict() for system in all_systems]), 200 # 200 OK status code
|
||||
except HTTPException:
|
||||
@@ -72,7 +70,7 @@ async def get_system_route(system_id: str):
|
||||
print(f"\n--- Handling GET /systems/{system_id} ---")
|
||||
try:
|
||||
# Fix the query dictionary syntax
|
||||
system = await db_h.find_system({'_id': system_id})
|
||||
system = await current_app.sys_db_h.find_system({'_id': system_id})
|
||||
|
||||
if system:
|
||||
# system is a System object, jsonify will convert it
|
||||
@@ -93,7 +91,7 @@ async def get_system_by_client_route(client_id: str):
|
||||
print(f"\n--- Handling GET /systems/client/{client_id} ---")
|
||||
try:
|
||||
# Fix the query dictionary syntax
|
||||
systems = await db_h.find_systems({'avail_on_nodes': client_id})
|
||||
systems = await current_app.sys_db_h.find_systems({'avail_on_nodes': client_id})
|
||||
|
||||
if systems:
|
||||
# system is a System object, jsonify will convert it
|
||||
@@ -111,7 +109,7 @@ async def get_system_by_client_route(client_id: str):
|
||||
@systems_bp.route('/<string:system_id>', methods=['PUT'])
|
||||
async def update_system_route(system_id: str, updated_system_data):
|
||||
try:
|
||||
update_system = await db_h.update_system({"_id", system_id}, updated_system_data)
|
||||
update_system = await current_app.sys_db_h.update_system({"_id", system_id}, updated_system_data)
|
||||
|
||||
if update_system:
|
||||
print("Updated system:", update_system)
|
||||
@@ -130,7 +128,7 @@ async def update_system_route(system_id: str, updated_system_data):
|
||||
@systems_bp.route('/<string:system_id>', methods=['DELETE'])
|
||||
async def delete_system_route(system_id: str):
|
||||
try:
|
||||
deleted_system = await db_h.delete_system({"_id", system_id})
|
||||
deleted_system = await current_app.sys_db_h.delete_system({"_id", system_id})
|
||||
|
||||
if deleted_system:
|
||||
print("Deleted system:", deleted_system)
|
||||
@@ -166,7 +164,7 @@ async def assign_client_to_system_route(system_id: str):
|
||||
abort(400, "'client_id' must be a non-empty string")
|
||||
|
||||
# First, check if the system exists
|
||||
existing_system = await db_h.find_system({"_id": system_id})
|
||||
existing_system = await current_app.sys_db_h.find_system({"_id": system_id})
|
||||
if existing_system is None:
|
||||
abort(404, f"System with ID '{system_id}' not found")
|
||||
|
||||
@@ -175,7 +173,7 @@ async def assign_client_to_system_route(system_id: str):
|
||||
update_query = {"_id": system_id}
|
||||
update_data = {"$addToSet": {"avail_on_nodes": client_id}}
|
||||
|
||||
update_result = await db_h.update_system(update_query, update_data)
|
||||
update_result = await current_app.sys_db_h.update_system(update_query, update_data)
|
||||
|
||||
if update_result > 0:
|
||||
print(f"Client '{client_id}' assigned to system '{system_id}'.")
|
||||
@@ -184,7 +182,7 @@ async def assign_client_to_system_route(system_id: str):
|
||||
print(f"Client '{client_id}' was already assigned to system '{system_id}'.")
|
||||
status = "already_assigned"
|
||||
|
||||
updated_system = await db_h.find_system({"_id": system_id})
|
||||
updated_system = await current_app.sys_db_h.find_system({"_id": system_id})
|
||||
if updated_system:
|
||||
return jsonify({
|
||||
"status": status,
|
||||
@@ -222,7 +220,7 @@ async def dismiss_client_from_system_route(system_id: str):
|
||||
abort(400, "'client_id' must be a non-empty string")
|
||||
|
||||
# First, check if the system exists
|
||||
existing_system = await db_h.find_system({"_id": system_id})
|
||||
existing_system = await current_app.sys_db_h.find_system({"_id": system_id})
|
||||
if existing_system is None:
|
||||
abort(404, f"System with ID '{system_id}' not found")
|
||||
|
||||
@@ -231,7 +229,7 @@ async def dismiss_client_from_system_route(system_id: str):
|
||||
update_query = {"_id": system_id}
|
||||
update_data = {"$pull": {"avail_on_nodes": client_id}}
|
||||
|
||||
update_result = await db_h.update_system(update_query, update_data)
|
||||
update_result = await current_app.sys_db_h.update_system(update_query, update_data)
|
||||
|
||||
if update_result > 0:
|
||||
print(f"Client '{client_id}' dismissed from system '{system_id}'.")
|
||||
@@ -242,7 +240,7 @@ async def dismiss_client_from_system_route(system_id: str):
|
||||
# Note: update_result.matched_count will be 1 even if modified_count is 0
|
||||
|
||||
# Optionally fetch the updated document to return its current state
|
||||
updated_system = await db_h.find_system({"_id": system_id})
|
||||
updated_system = await current_app.sys_db_h.find_system({"_id": system_id})
|
||||
if updated_system:
|
||||
return jsonify({
|
||||
"status": status,
|
||||
@@ -271,7 +269,9 @@ async def search_systems_route():
|
||||
try:
|
||||
query_params = dict(request.args)
|
||||
|
||||
systems = await db_h.find_systems(query_params)
|
||||
systems = await current_app.sys_db_h.find_systems(query_params)
|
||||
|
||||
print("Found systems", systems)
|
||||
|
||||
if systems:
|
||||
# If systems are found, return them as a list of dictionaries
|
||||
|
||||
Reference in New Issue
Block a user