function apiAbort(obj){ writeOutput(serializeJSON(obj)); abort; } function readJsonBody(){ raw = toString(getHttpRequestData().content); if (isNull(raw) || len(trim(raw)) EQ 0){ apiAbort({OK=false,ERROR="missing_body"}); } try { parsed = deserializeJSON(raw); } catch(any e){ apiAbort({OK=false,ERROR="bad_json",MESSAGE="Invalid JSON body"}); } if (!isStruct(parsed)){ apiAbort({OK=false,ERROR="bad_json",MESSAGE="JSON must be an object"}); } return parsed; } function normStr(v){ if (isNull(v)) return ""; return trim(toString(v)); } /* ---------- AUTH CONTEXT ---------- */ if (!structKeyExists(request,"UserID") || !isNumeric(request.UserID) || request.UserID LTE 0){ apiAbort({OK=false,ERROR="not_logged_in"}); } if (!structKeyExists(request,"BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0){ apiAbort({OK=false,ERROR="no_business_selected"}); } /* ---------- INPUT ---------- */ data = readJsonBody(); if (!structKeyExists(data,"BeaconID") || !isNumeric(data.BeaconID) || int(data.BeaconID) LTE 0){ apiAbort({OK=false,ERROR="missing_BeaconID"}); } if (!structKeyExists(data,"ServicePointID") || !isNumeric(data.ServicePointID) || int(data.ServicePointID) LTE 0){ apiAbort({OK=false,ERROR="missing_ServicePointID"}); } BeaconID = int(data.BeaconID); ServicePointID = int(data.ServicePointID); Notes = ""; if (structKeyExists(data,"Notes")){ Notes = left(normStr(data.Notes), 255); } SELECT BeaconID FROM Beacons WHERE BeaconID = AND BusinessID = LIMIT 1 #serializeJSON({OK=false,ERROR="beacon_not_found_for_business"})# SELECT ServicePointID FROM ServicePoints WHERE ServicePointID = AND BusinessID = LIMIT 1 #serializeJSON({OK=false,ERROR="servicepoint_not_found_for_business"})# SELECT lt_Beacon_Businesses_ServicePointID FROM lt_Beacon_Businesses_ServicePoints WHERE BusinessID = AND BeaconID = LIMIT 1 #serializeJSON({OK=false,ERROR="beacon_already_assigned"})# SELECT lt_Beacon_Businesses_ServicePointID FROM lt_Beacon_Businesses_ServicePoints WHERE BusinessID = AND ServicePointID = LIMIT 1 #serializeJSON({OK=false,ERROR="servicepoint_already_assigned"})# INSERT INTO lt_Beacon_Businesses_ServicePoints (BusinessID, BeaconID, ServicePointID, lt_Beacon_Businesses_ServicePointAssignedByUserID, lt_Beacon_Businesses_ServicePointNotes) VALUES ( , , , , ) SELECT LAST_INSERT_ID() AS NewID #serializeJSON({ "OK"=true, "ACTION"="inserted", "lt_Beacon_Businesses_ServicePointID"=qID.NewID, "BeaconID"=BeaconID, "ServicePointID"=ServicePointID, "BusinessID"=(request.BusinessID & "") })#