function apiAbort(obj) { writeOutput(serializeJSON(obj)); abort; } function readJsonBody() { raw = toString(getHttpRequestData().content); if (isNull(raw) || len(trim(raw)) EQ 0) return {}; try { parsed = deserializeJSON(raw); } catch(any e) { apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" }); } if (!isStruct(parsed)) return {}; return parsed; } function normStr(v) { if (isNull(v)) return ""; return trim(toString(v)); } data = readJsonBody(); if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) { apiAbort({ OK=false, ERROR="no_business_selected" }); } if (!structKeyExists(data, "Name") || len(normStr(data.Name)) EQ 0) { apiAbort({ OK=false, ERROR="missing_name", MESSAGE="Name is required" }); } servicePointId = 0; if (structKeyExists(data, "ServicePointID") && isNumeric(data.ServicePointID) && int(data.ServicePointID) GT 0) { servicePointId = int(data.ServicePointID); } spName = normStr(data.Name); spCode = structKeyExists(data, "Code") ? normStr(data.Code) : ""; spTypeID = structKeyExists(data, "TypeID") && isNumeric(data.TypeID) ? int(data.TypeID) : 1; sortOrder = structKeyExists(data, "SortOrder") && isNumeric(data.SortOrder) ? int(data.SortOrder) : 0; beaconMinor = structKeyExists(data, "BeaconMinor") && isNumeric(data.BeaconMinor) ? int(data.BeaconMinor) : -1; isActive = 1; if (structKeyExists(data, "IsActive")) { if (isBoolean(data.IsActive)) isActive = (data.IsActive ? 1 : 0); else if (isNumeric(data.IsActive)) isActive = int(data.IsActive); else if (isSimpleValue(data.IsActive)) isActive = (lcase(trim(toString(data.IsActive))) EQ "true" ? 1 : 0); } UPDATE ServicePoints SET Name = , Code = , TypeID = , IsActive = , SortOrder = , BeaconMinor = WHERE ID = AND BusinessID = SELECT ID FROM ServicePoints WHERE ID = AND BusinessID = LIMIT 1 #serializeJSON({ OK=false, ERROR="not_found" })# SELECT COALESCE(MAX(BeaconMinor), -1) AS MaxMinor FROM ServicePoints WHERE BusinessID = INSERT INTO ServicePoints ( BusinessID, Name, Code, TypeID, IsActive, SortOrder, BeaconMinor ) VALUES ( , , , , , , ) SELECT LAST_INSERT_ID() AS ServicePointID SELECT ID, BusinessID, Name, Code, TypeID, IsActive, SortOrder, BeaconMinor FROM ServicePoints WHERE ID = AND BusinessID = LIMIT 1 #serializeJSON({ OK=true, ERROR="", SERVICEPOINT=servicePoint })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#