//////////////////////////////////////////////////// // Code Writen by J.J.M. Lut // Date: 2/6/2021 // Purpose: The code used by the Messaging Lantern. // Released under the Atribution-Noncommercial licence. // Published on https://www.instructables.com/ //////////////////////////////////////////////////// // URL to call this HTTP function from your published site looks like: // Premium site - https://mysite.com/_functions/example/multiply?leftOperand=3&rightOperand=4 // Free site - https://username.wixsite.com/mysite/_functions/example/multiply?leftOperand=3&rightOperand=4 // URL to test this HTTP function from your saved site looks like: // Premium site - https://mysite.com/_functions-dev/example/multiply?leftOperand=3&rightOperand=4 // Free site - https://username.wixsite.com/mysite/_functions-dev/example/multiply?leftOperand=3&rightOperand=4 /*get function https://username.wixsite.com/mysite/_functions/getUser1DisplayMessage/USER_1 https://username.wixsite.com/mysite/_functions/getUser2DisplayMessage/USER_2 set functions https://username.wixsite.com/mysite/_functions/setUser1DisplayMessage?message=Message%20Here https://username.wixsite.com/mysite/_functions/setUser2DisplayMessage?message=Message%20Here Do the following: Create a database. name it: "DATABASE_LANTERN" (or change to your preference will require altering of the code) Set the permissions: - Read: Anyone - Create: Admin - Update: Anyone - Delete: Admin Create 2 entries: - With titles: USER1 and USER2 - add a collum: -fieldname: dsiplayDATA -fieldKey: displayData -field type: text - fill the dsiplayDATA collum with example text. - Copy the ID's and save them for now. Keys should look something like this: Key1 USER1: 1a534425-5a6a-0000-b215-0cdbf3195f30 //they don't work Key2 USER2: 9d6937b5-7f67-0000-b563-ba0495808197 //they don't work In code files create a new backend javascript file: http-functions.js Paste the provided code in there. -Replace the "KEY" in both set functions -get_setUser1DisplayMessage: key of user 1 -get_setUser2DisplayMessage: key of user 2 Now publish your website: Sync the database: DATABASE_LANTERN to live. Everything should be working now and by pasting the provided url's in the search bar of your web browser you should be able to obtain a json with the get_functions and write to the database with the set functions verify this by visiting the live view of your database. KEEP THESE URLS PRIVATE! */ import {ok, notFound, serverError} from 'wix-http-functions'; import wixData from 'wix-data'; ////////////////////////////////////////////////////// //Functions to set the display messages in the database ////////////////////////////////////////////////////// export function get_setUser2DisplayMessage(request) { console.log(request); try { wixData.get("DATABASE_LANTERN","KEY") //change me .then((item) => { item.displayData = request.query.message; console.log(item.displayData ); wixData.update("DATABASE_LANTERN", item); } ) } catch (err) { console.log("error: ",err); } return ok(); } export function get_setUser1DisplayMessage(request) { console.log(request); try { wixData.get("DATABASE_LANTERN","KEY") //change me .then((item) => { item.displayData = request.query.message; console.log(item.displayData); wixData.update("DATABASE_LANTERN", item); } ) } catch (err) { console.log("error: ",err); } return ok(); } ////////////////////////////////////////////////////// //Functions to get the display messages from the database ////////////////////////////////////////////////////// export function get_getUser2DisplayMessage(request) { console.log(request); let options = { "headers": { "Content-Type": "application/json" } }; // query a collection to find matching items return wixData.query("DATABASE_LANTERN") .eq("title", request.path[0]) .find() .then( (results) => { // matching items were found if(results.items.length > 0) { if (results.items[0].displayData.length >=50){ results.items[0].displayData = results.items[0].displayData.slice(0,50); } options.body = { "items": results.items }; return ok(options); } // no matching items found options.body = { "error": `'${request.path[0]} ${request.path[1]}' was not found` }; return notFound(options); } ) // something went wrong .catch( (error) => { console.log("error: ",error); options.body = { "error": error }; return serverError(options); } ); } export function get_getUser1DisplayMessage(request) { console.log(request); let options = { "headers": { "Content-Type": "application/json" } }; // query a collection to find matching items return wixData.query("DATABASE_LANTERN") .eq("title", request.path[0]) .find() .then( (results) => { // matching items were found if(results.items.length > 0) { if (results.items[0].displayData.length >=50){ results.items[0].displayData = results.items[0].displayData.slice(0,50); } options.body = { "items": results.items }; return ok(options); } // no matching items found options.body = { "error": `'${request.path[0]} ${request.path[1]}' was not found` }; return notFound(options); } ) // something went wrong .catch( (error) => { console.log("error: ",error); options.body = { "error": error }; return serverError(options); } ); }