From d4c3868e20a927afc42f22c0d871ce68f923e970 Mon Sep 17 00:00:00 2001 From: davidwells Date: Mon, 11 Jun 2018 17:10:19 -0700 Subject: [PATCH] fix ID reference --- functions/todos-create.js | 18 ++++++++---------- functions/todos-delete.js | 19 +++++++++---------- functions/todos-read-all.js | 16 +++++++--------- functions/todos-read-one.js | 21 +++++++++------------ functions/todos-update.js | 21 ++++++++++----------- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/functions/todos-create.js b/functions/todos-create.js index 5b244a4..198c27a 100644 --- a/functions/todos-create.js +++ b/functions/todos-create.js @@ -1,27 +1,25 @@ -'use strict'; - import faunadb from 'faunadb' -// const faunadb = require('faunadb'); -const q = faunadb.query; + +const q = faunadb.query const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET -}); +}) exports.handler = (event, context, callback) => { - const data = JSON.parse(event.body); - console.log("create todo", data); + const data = JSON.parse(event.body) + console.log("create todo", data) return client.query(q.Create(q.Ref("classes/todos"), {data})) .then((response) => { - console.log("success", response); + console.log("success", response) return callback(null, { statusCode: 200, body: JSON.stringify(response) }) }).catch((error) => { - console.log("error", error); + console.log("error", error) return callback(null, { statusCode: 400, body: JSON.stringify(error) }) }) -}; +} diff --git a/functions/todos-delete.js b/functions/todos-delete.js index 400d197..ea36016 100644 --- a/functions/todos-delete.js +++ b/functions/todos-delete.js @@ -1,26 +1,25 @@ -'use strict'; - import faunadb from 'faunadb' -// const faunadb = require('faunadb'); -const q = faunadb.query; + +const q = faunadb.query const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET -}); +}) exports.handler = (event, context, callback) => { - console.log("delete todo"); - return client.query(q.Delete(q.Ref("classes/todos/"+event.pathParameters.id))) + const id = event.path.replace(/\/\.netlify\/functions\/todos-delete\//, "") + console.log("delete todo", id) + return client.query(q.Delete(q.Ref(`classes/todos/${id}`))) .then((response) => { - console.log("success", response); + console.log("success", response) return callback(null, { statusCode: 200, body: JSON.stringify(response) }) }).catch((error) => { - console.log("error", error); + console.log("error", error) return callback(null, { statusCode: 400, body: JSON.stringify(error) }) }) -}; +} diff --git a/functions/todos-read-all.js b/functions/todos-read-all.js index 53f1104..3815142 100644 --- a/functions/todos-read-all.js +++ b/functions/todos-read-all.js @@ -1,26 +1,24 @@ -'use strict'; - import faunadb from 'faunadb' -// const faunadb = require('faunadb'); -const q = faunadb.query; + +const q = faunadb.query const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET -}); +}) exports.handler = (event, context, callback) => { - console.log("readAll todo"); + console.log("readAll todos") return client.query(q.Paginate(q.Match(q.Ref("indexes/all_todos")))) .then((response) => { - console.log("success", response); + console.log("success", response) return callback(null, { statusCode: 200, body: JSON.stringify(response) }) }).catch((error) => { - console.log("error", error); + console.log("error", error) return callback(null, { statusCode: 400, body: JSON.stringify(error) }) }) -}; +} diff --git a/functions/todos-read-one.js b/functions/todos-read-one.js index 409f8ab..5eadbb6 100644 --- a/functions/todos-read-one.js +++ b/functions/todos-read-one.js @@ -1,28 +1,25 @@ -'use strict'; - import faunadb from 'faunadb' -// const faunadb = require('faunadb'); -const q = faunadb.query; + +const q = faunadb.query const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET -}); +}) exports.handler = (event, context, callback) => { - console.log('event', event) - console.log('context', context) - console.log(`readOne todo ${event.pathParameters.id}`); - return client.query(q.Get(q.Ref("classes/todos/"+event.pathParameters.id))) + const id = event.path.replace(/\/\.netlify\/functions\/todos-read-one\//, "") + console.log(`readOne todo ${id}`) + return client.query(q.Get(q.Ref(`classes/todos/${id}`))) .then((response) => { - console.log("success", response); + console.log("success", response) return callback(null, { statusCode: 200, body: JSON.stringify(response) }) }).catch((error) => { - console.log("error", error); + console.log("error", error) return callback(null, { statusCode: 400, body: JSON.stringify(error) }) }) -}; +} diff --git a/functions/todos-update.js b/functions/todos-update.js index e69dc58..71e9f42 100644 --- a/functions/todos-update.js +++ b/functions/todos-update.js @@ -1,27 +1,26 @@ -'use strict'; - import faunadb from 'faunadb' -// const faunadb = require('faunadb'); -const q = faunadb.query; + +const q = faunadb.query const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET -}); +}) exports.handler = (event, context, callback) => { - const data = JSON.parse(event.body); - console.log("update todo"); - return client.query(q.Update(q.Ref("classes/todos/"+event.pathParameters.id), {data})) + const data = JSON.parse(event.body) + const id = event.path.replace(/\/\.netlify\/functions\/todos-update\//, "") + console.log("update todo", id) + return client.query(q.Update(q.Ref(`classes/todos/${id}`), {data})) .then((response) => { - console.log("success", response); + console.log("success", response) return callback(null, { statusCode: 200, body: JSON.stringify(response) }) }).catch((error) => { - console.log("error", error); + console.log("error", error) return callback(null, { statusCode: 400, body: JSON.stringify(error) }) }) -}; +}