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,"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"}); if (!structKeyExists(data,"ServicePointName") || len(normStr(data.ServicePointName)) EQ 0) { apiAbort({OK=false,ERROR="missing_servicepoint_name",MESSAGE="ServicePointName is required"}); } spid = 0; if (structKeyExists(data,"ServicePointID") && isNumeric(data.ServicePointID) && int(data.ServicePointID) GT 0) { spid = int(data.ServicePointID); } spName = normStr(data.ServicePointName); spTypeID = (structKeyExists(data,"ServicePointTypeID") && isNumeric(data.ServicePointTypeID)) ? int(data.ServicePointTypeID) : 0; spCode = structKeyExists(data,"ServicePointCode") ? normStr(data.ServicePointCode) : ""; descr = structKeyExists(data,"Description") ? normStr(data.Description) : ""; sortOrd = (structKeyExists(data,"SortOrder") && isNumeric(data.SortOrder)) ? int(data.SortOrder) : 0; 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 ServicePointName = , ServicePointTypeID = , ServicePointCode = , Description = , SortOrder = , IsActive = WHERE ServicePointID = AND BusinessID = SELECT ServicePointID FROM ServicePoints WHERE ServicePointID = AND BusinessID = LIMIT 1 #serializeJSON({ OK=false, ERROR="not_found" })# INSERT INTO ServicePoints ( BusinessID, ServicePointName, ServicePointTypeID, ServicePointCode, Description, SortOrder, IsActive ) VALUES ( , , , , , , ) SELECT LAST_INSERT_ID() AS ServicePointID SELECT ServicePointID, BusinessID, ServicePointName, ServicePointTypeID, ServicePointCode, Description, SortOrder, IsActive, CreatedAt, UpdatedAt FROM ServicePoints WHERE ServicePointID = AND BusinessID = LIMIT 1 #serializeJSON({ OK=true, ERROR="", SERVICEPOINT=sp })#