From ef3f6848e019b08a26502af06e46d400a77a9f58 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Thu, 13 Oct 2022 14:22:30 -0500 Subject: [PATCH 1/7] updated --- src/index.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/index.js b/src/index.js index 59fb152..461cd52 100644 --- a/src/index.js +++ b/src/index.js @@ -20,21 +20,7 @@ import Contact from './html/contact.html'; const router = Router(); -router.get('/', () => new Response(Index , { - headers: { 'content-type': 'text/html' }, -})); -router.get('/About', () => new Response(About , { - headers: { 'content-type': 'text/html' }, -})); - -router.get('/Endpoints', () => new Response(Endpoints , { - headers: { 'content-type': 'text/html' }, -})); - -router.get('/Contact', () => new Response(Contact , { - headers: { 'content-type': 'text/html' }, -})); // Test routes router.get('/api/tests', Tests) From 11a0bc04cd6731b26ac8f4d95affa0a9c8d9e1cc Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:06:44 -0500 Subject: [PATCH 2/7] updated --- .gitignore | 3 ++- src/index.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7cd00b4..4b95251 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target - +dist +/dist **/*.rs.bk Cargo.lock bin/ diff --git a/src/index.js b/src/index.js index 461cd52..455a075 100644 --- a/src/index.js +++ b/src/index.js @@ -20,8 +20,22 @@ import Contact from './html/contact.html'; const router = Router(); +router.get('/api', () => new Response(Index , { + headers: { 'content-type': 'text/html' }, +})); +/* router.get('/About', () => new Response(About , { + headers: { 'content-type': 'text/html' }, +})); +router.get('/Endpoints', () => new Response(Endpoints , { + headers: { 'content-type': 'text/html' }, +})); + +router.get('/Contact', () => new Response(Contact , { + headers: { 'content-type': 'text/html' }, +})); + */ // Test routes router.get('/api/tests', Tests) router.get('/api/test/:id', Test ); From e51564eed2b5969d2aa0862ad004878b13d45ba1 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Thu, 8 Dec 2022 23:16:00 -0600 Subject: [PATCH 3/7] create route added for mongodb --- src/handlers/Mongo/MongoBackend.js | 2 +- src/handlers/Mongo/MongoCreate.js | 99 ++++++++++++++++++++++++++++++ src/index.js | 3 + 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/handlers/Mongo/MongoCreate.js diff --git a/src/handlers/Mongo/MongoBackend.js b/src/handlers/Mongo/MongoBackend.js index ac98c5c..b0acd3e 100644 --- a/src/handlers/Mongo/MongoBackend.js +++ b/src/handlers/Mongo/MongoBackend.js @@ -50,7 +50,7 @@ const MongoBackend = async (request, event) => { console.log("MongoData is" , MongoData?.documents[0]) - return new Response(JSON.stringify(MongoData?.documents[0] ?? "Not found"), { + return new Response(JSON.stringify(MongoData?.documents ?? "Not found"), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', diff --git a/src/handlers/Mongo/MongoCreate.js b/src/handlers/Mongo/MongoCreate.js new file mode 100644 index 0000000..58d70e9 --- /dev/null +++ b/src/handlers/Mongo/MongoCreate.js @@ -0,0 +1,99 @@ +//Mongo URL +const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/insertOne'; + +const MongoCreate = async (request, event) => { + + try { + + async function createDocument() { + try { + + const data = { + "collection": "gmwebsite", + "database": "gmadb", + "dataSource": "giobot", + "document": { + "name": "Jacque", + "social": [ + "Jacque.com", + "Stuff.com", + ], + "plug": "jacqueinstagram", + "date": "2022-12-08T01:11:18.965Z", + + } + }; + + let options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Request-Headers': '*', + 'api-key': MDB_TOKEN, + }, + body: JSON.stringify(data), + }; + + console.log("Function is running calling... "); + console.log(options, options.headers, options.body); + + const MongoResponse = await fetch(MongoUrl, options); + + if (!MongoResponse.ok) { + const message = `\n Bad fetch: ${MongoResponse.status}`; + console.log(message, ' Response is: ', MongoResponse.body, MongoResponse.headers, MongoResponse.statusText); + return new Response(`Error : ${message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + else { + const MongoData = await MongoResponse.json(); + console.log(MongoData); + + return MongoData; + } + + } catch (error) { + console.log("useToken() internal error occurred: ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + } + + const MongoData = await createDocument(); + + console.log("MongoCreate sucessfull" , MongoData); + + return new Response(JSON.stringify(MongoData ), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + catch (error) { + console.error("Failed MongoCreate call : ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } +}; +export default MongoCreate; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 455a075..6615708 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ import WebSiteLedToggleON from './handlers/Arduino/webSiteLedToggleON.js'; import CodeLedToggleOFF from './handlers/Arduino/codeLedToggleOFF.js'; import CodeLedSTATUS from './handlers/Arduino/codeLedSTATUS.js'; import MongoBackend from './handlers/Mongo/MongoBackend.js'; +import MongoCreate from './handlers/Mongo/MongoCreate.js'; import Index from './html/index.html'; import About from './html/about.html'; import Endpoints from './html/endpoints.html'; @@ -59,7 +60,9 @@ router.get('/api/Arduino/ToggleOFF', CodeLedToggleOFF ); router.get('/api/Arduino/STATUS', CodeLedSTATUS ); router.get('/api/Arduino/TestConnection', WebSiteLedToggleON ); +// Connect to MongoDB router.get('/api/Mongo', MongoBackend ); +router.get('/api/MongoCreate', MongoCreate ); router.get('*', () => new Response('🌴☀️ ~ Cannot find an endpoint for this 😕 ~ 🌴☀️', { status: 404 })); From b1a527fa5b1d973a32b7201628f9aba8134ccd42 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Thu, 8 Dec 2022 23:34:30 -0600 Subject: [PATCH 4/7] basic routes done crud for mongodb --- src/handlers/Mongo/MongoDelete.js | 87 +++++++++++++++++++++++++++++ src/handlers/Mongo/MongoUpdate.js | 92 +++++++++++++++++++++++++++++++ src/index.js | 4 ++ 3 files changed, 183 insertions(+) create mode 100644 src/handlers/Mongo/MongoDelete.js create mode 100644 src/handlers/Mongo/MongoUpdate.js diff --git a/src/handlers/Mongo/MongoDelete.js b/src/handlers/Mongo/MongoDelete.js new file mode 100644 index 0000000..e179c06 --- /dev/null +++ b/src/handlers/Mongo/MongoDelete.js @@ -0,0 +1,87 @@ +//Mongo URL +const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/deleteOne'; + +const MongoDelete = async (request, event) => { + + try { + + async function deleteDocument() { + try { + + const data = { + "collection": "gmwebsite", + "database": "gmadb", + "dataSource": "giobot", + "filter": { "_id": { "$oid": "6392c40b10494bc10e02316e" } }, + }; + + let options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Request-Headers': '*', + 'api-key': MDB_TOKEN, + }, + body: JSON.stringify(data), + }; + + + const MongoResponse = await fetch(MongoUrl, options); + + if (!MongoResponse.ok) { + const message = `\n Bad fetch: ${MongoResponse.status}`; + console.log(message, ' Response is: ', MongoResponse.body, MongoResponse.headers, MongoResponse.statusText); + return new Response(`Error : ${message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + else { + const MongoData = await MongoResponse.json(); + + return MongoData; + } + + } catch (error) { + console.log("useToken() internal error occurred: ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + } + + const MongoData = await deleteDocument(); + + console.log("MongoDelete sucessfull" , MongoData); + + return new Response(JSON.stringify(MongoData ), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + catch (error) { + console.error("Failed MongoDelete call : ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } +}; +export default MongoDelete; \ No newline at end of file diff --git a/src/handlers/Mongo/MongoUpdate.js b/src/handlers/Mongo/MongoUpdate.js new file mode 100644 index 0000000..ce5b613 --- /dev/null +++ b/src/handlers/Mongo/MongoUpdate.js @@ -0,0 +1,92 @@ +//Mongo URL +const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/updateOne'; + +const MongoUpdate = async (request, event) => { + + try { + + async function updateDocument() { + try { + + const data = { + "collection": "gmwebsite", + "database": "gmadb", + "dataSource": "giobot", + "filter": { "_id": { "$oid": "6392c40b10494bc10e02316e" } }, + "update": { + "$set": { + "name": "billybob", + } + } + }; + + let options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Request-Headers': '*', + 'api-key': MDB_TOKEN, + }, + body: JSON.stringify(data), + }; + + + const MongoResponse = await fetch(MongoUrl, options); + + if (!MongoResponse.ok) { + const message = `\n Bad fetch: ${MongoResponse.status}`; + console.log(message, ' Response is: ', MongoResponse.body, MongoResponse.headers, MongoResponse.statusText); + return new Response(`Error : ${message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + else { + const MongoData = await MongoResponse.json(); + + return MongoData; + } + + } catch (error) { + console.log("useToken() internal error occurred: ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + } + + const MongoData = await updateDocument(); + + console.log("MongoUpdate sucessfull" , MongoData); + + return new Response(JSON.stringify(MongoData ), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } + catch (error) { + console.error("Failed MongoUpdate call : ", error.message); + return new Response(`Error : ${error.message}`, { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', + 'Access-Control-Max-Age': '86400', + } + }); + } +}; +export default MongoUpdate; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 6615708..e1f3408 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,8 @@ import CodeLedToggleOFF from './handlers/Arduino/codeLedToggleOFF.js'; import CodeLedSTATUS from './handlers/Arduino/codeLedSTATUS.js'; import MongoBackend from './handlers/Mongo/MongoBackend.js'; import MongoCreate from './handlers/Mongo/MongoCreate.js'; +import MongoUpdate from './handlers/Mongo/MongoUpdate.js'; +import MongoDelete from './handlers/Mongo/MongoDelete.js'; import Index from './html/index.html'; import About from './html/about.html'; import Endpoints from './html/endpoints.html'; @@ -63,6 +65,8 @@ router.get('/api/Arduino/TestConnection', WebSiteLedToggleON ); // Connect to MongoDB router.get('/api/Mongo', MongoBackend ); router.get('/api/MongoCreate', MongoCreate ); +router.get('/api/MongoUpdate', MongoUpdate ); +router.get('/api/MongoDelete', MongoDelete ); router.get('*', () => new Response('🌴☀️ ~ Cannot find an endpoint for this 😕 ~ 🌴☀️', { status: 404 })); From 616b7065fb66628666b3fb651fdf25b4dbf8a899 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:50:29 -0600 Subject: [PATCH 5/7] Updated create route --- src/handlers/Mongo/MongoCreate.js | 48 ++++++++++++++++++++++++------- src/index.js | 2 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/handlers/Mongo/MongoCreate.js b/src/handlers/Mongo/MongoCreate.js index 58d70e9..2e8ca61 100644 --- a/src/handlers/Mongo/MongoCreate.js +++ b/src/handlers/Mongo/MongoCreate.js @@ -1,8 +1,38 @@ //Mongo URL -const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/insertOne'; +const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/insertOne';\ +let name = 'none'; +let social = ['1', '2']; +let plug = ''; +let date = '2022-12-08T01:11:18.965Z'; + const MongoCreate = async (request, event) => { + if (request.params.name) { + name = request.params.name; + } else { + name = 'none'; + } + + if (request.params.social) { + social = request.params.social; + } else { + social = ['1', '2']; + } + + if (request.params.plug) { + plug = request.params.plug; + } else { + plug = ''; + } + + if (request.params.date) { + date = request.params.date; + } else { + date = '2022-12-08T01:11:18.965Z'; + } + + try { async function createDocument() { @@ -13,14 +43,12 @@ const MongoCreate = async (request, event) => { "database": "gmadb", "dataSource": "giobot", "document": { - "name": "Jacque", + "name": `${name}`, "social": [ - "Jacque.com", - "Stuff.com", + `${social[0]}`, ], - "plug": "jacqueinstagram", - "date": "2022-12-08T01:11:18.965Z", - + "plug": `${plug}`, + "date": `${date}`, } }; @@ -72,10 +100,10 @@ const MongoCreate = async (request, event) => { } const MongoData = await createDocument(); - - console.log("MongoCreate sucessfull" , MongoData); - return new Response(JSON.stringify(MongoData ), { + console.log("MongoCreate sucessfull", MongoData); + + return new Response(JSON.stringify(MongoData), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', diff --git a/src/index.js b/src/index.js index e1f3408..4aea3b2 100644 --- a/src/index.js +++ b/src/index.js @@ -64,7 +64,7 @@ router.get('/api/Arduino/TestConnection', WebSiteLedToggleON ); // Connect to MongoDB router.get('/api/Mongo', MongoBackend ); -router.get('/api/MongoCreate', MongoCreate ); +router.get('/api/MongoCreate/:name/:social/:plug/:date', MongoCreate ); router.get('/api/MongoUpdate', MongoUpdate ); router.get('/api/MongoDelete', MongoDelete ); From 8803963f4ba36c5431d07acfcffd27583c98cca6 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:51:33 -0600 Subject: [PATCH 6/7] Updated create route --- src/handlers/Mongo/MongoCreate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/Mongo/MongoCreate.js b/src/handlers/Mongo/MongoCreate.js index 2e8ca61..f0cf848 100644 --- a/src/handlers/Mongo/MongoCreate.js +++ b/src/handlers/Mongo/MongoCreate.js @@ -1,5 +1,5 @@ //Mongo URL -const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/insertOne';\ +const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/insertOne'; let name = 'none'; let social = ['1', '2']; let plug = ''; From fa069b26ff58a3d02c57a38914bd4ba08d7ccb61 Mon Sep 17 00:00:00 2001 From: Giovanni Moscato <69764323+codingcodewhilegoofin@users.noreply.github.com> Date: Tue, 20 Dec 2022 16:17:49 -0600 Subject: [PATCH 7/7] Updated all crud routes --- src/handlers/Mongo/MongoCreate.js | 2 +- src/handlers/Mongo/MongoDelete.js | 10 +++++- src/handlers/Mongo/MongoUpdate.js | 56 +++++++++++++++++++++++++++---- src/index.js | 4 +-- 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/handlers/Mongo/MongoCreate.js b/src/handlers/Mongo/MongoCreate.js index f0cf848..aef76e0 100644 --- a/src/handlers/Mongo/MongoCreate.js +++ b/src/handlers/Mongo/MongoCreate.js @@ -45,7 +45,7 @@ const MongoCreate = async (request, event) => { "document": { "name": `${name}`, "social": [ - `${social[0]}`, + `${social}`, ], "plug": `${plug}`, "date": `${date}`, diff --git a/src/handlers/Mongo/MongoDelete.js b/src/handlers/Mongo/MongoDelete.js index e179c06..7001ea4 100644 --- a/src/handlers/Mongo/MongoDelete.js +++ b/src/handlers/Mongo/MongoDelete.js @@ -1,8 +1,16 @@ //Mongo URL const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/deleteOne'; +let id = ''; + const MongoDelete = async (request, event) => { + if (request.params.id) { + id = request.params.id; + } else { + id = ''; + } + try { async function deleteDocument() { @@ -12,7 +20,7 @@ const MongoDelete = async (request, event) => { "collection": "gmwebsite", "database": "gmadb", "dataSource": "giobot", - "filter": { "_id": { "$oid": "6392c40b10494bc10e02316e" } }, + "filter": { "_id": { "$oid": `${id}` } }, }; let options = { diff --git a/src/handlers/Mongo/MongoUpdate.js b/src/handlers/Mongo/MongoUpdate.js index ce5b613..bb0ed48 100644 --- a/src/handlers/Mongo/MongoUpdate.js +++ b/src/handlers/Mongo/MongoUpdate.js @@ -1,8 +1,45 @@ //Mongo URL const MongoUrl = 'https://data.mongodb-api.com/app/data-inikb/endpoint/data/v1/action/updateOne'; +let id = ''; +let name = 'none'; +let social = ['1', '2']; +let plug = ''; +let date = '2022-12-08T01:11:18.965Z'; + const MongoUpdate = async (request, event) => { + if (request.params.id) { + id = request.params.id; + } else { + id = ''; + } + + if (request.params.name) { + name = request.params.name; + } else { + name = 'none'; + } + + if (request.params.social) { + social = request.params.social; + } else { + social = ['1', '2']; + } + + if (request.params.plug) { + plug = request.params.plug; + } else { + plug = ''; + } + + if (request.params.date) { + date = request.params.date; + } else { + date = '2022-12-08T01:11:18.965Z'; + } + + try { async function updateDocument() { @@ -12,10 +49,15 @@ const MongoUpdate = async (request, event) => { "collection": "gmwebsite", "database": "gmadb", "dataSource": "giobot", - "filter": { "_id": { "$oid": "6392c40b10494bc10e02316e" } }, + "filter": { "_id": { "$oid": `${id}` } }, "update": { "$set": { - "name": "billybob", + "name": `${name}`, + "social": [ + `${social}`, + ], + "plug": `${plug}`, + "date": `${date}`, } } }; @@ -30,7 +72,7 @@ const MongoUpdate = async (request, event) => { body: JSON.stringify(data), }; - + const MongoResponse = await fetch(MongoUrl, options); if (!MongoResponse.ok) { @@ -47,7 +89,7 @@ const MongoUpdate = async (request, event) => { } else { const MongoData = await MongoResponse.json(); - + return MongoData; } @@ -65,10 +107,10 @@ const MongoUpdate = async (request, event) => { } const MongoData = await updateDocument(); - - console.log("MongoUpdate sucessfull" , MongoData); - return new Response(JSON.stringify(MongoData ), { + console.log("MongoUpdate sucessfull", MongoData); + + return new Response(JSON.stringify(MongoData), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', diff --git a/src/index.js b/src/index.js index 4aea3b2..7228494 100644 --- a/src/index.js +++ b/src/index.js @@ -65,8 +65,8 @@ router.get('/api/Arduino/TestConnection', WebSiteLedToggleON ); // Connect to MongoDB router.get('/api/Mongo', MongoBackend ); router.get('/api/MongoCreate/:name/:social/:plug/:date', MongoCreate ); -router.get('/api/MongoUpdate', MongoUpdate ); -router.get('/api/MongoDelete', MongoDelete ); +router.get('/api/MongoUpdate/:id/:name/:social/:plug/:date', MongoUpdate ); +router.get('/api/MongoDelete/:id', MongoDelete ); router.get('*', () => new Response('🌴☀️ ~ Cannot find an endpoint for this 😕 ~ 🌴☀️', { status: 404 }));